Skip to content

Commit

Permalink
Don't import preview urls for non-existent types or spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Jan 8, 2024
1 parent db9e56c commit eae07e5
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/commands/importExportEntities/entities/previewUrls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PreviewContracts } from "@kontent-ai/management-sdk";

import { notNull } from "../../../utils/typeguards.js";
import { EntityDefinition } from "../entityDefinition.js";

export const previewUrlsEntity: EntityDefinition<PreviewContracts.IPreviewConfigurationContract> = {
Expand All @@ -15,37 +16,33 @@ export const previewUrlsEntity: EntityDefinition<PreviewContracts.IPreviewConfig
.map(s => {
const newSpaceId = context.spaceIdsByOldIds.get(s.space.id);

if (!newSpaceId) {
throw new Error(`Could not find new space id for old space id ${s.space.id}. This should never happen`);
}
return newSpaceId ? [s, newSpaceId] as const : null;
})
.filter(notNull)
.map(([s, newSpaceId]) => ({ ...s, space: { id: newSpaceId } })),

return { ...s, space: { id: newSpaceId } };
}),
preview_url_patterns: previews.preview_url_patterns.map(p => {
const newContentTypeId = context.contentTypeContextByOldIds.get(p.content_type.id)?.selfId;

if (!newContentTypeId) {
throw new Error(
`Could not find new content type id for old content type id ${p.content_type.id}. This should never happen`,
);
}

const newUrlPatterns = p.url_patterns.map(u => {
if (!u.space) {
return u;
}
preview_url_patterns: previews.preview_url_patterns
.map(s => {
const newTypeId = context.contentTypeContextByOldIds.get(s.content_type.id)?.selfId;

const newSpaceId = context.spaceIdsByOldIds.get(u.space.id);
return newTypeId ? [s, newTypeId] as const : null;
})
.filter(notNull)
.map(([p, newTypeId]) => {
const newUrlPatterns = p.url_patterns
.map(u => {
if (!u.space) {
return u;
}

if (!newSpaceId) {
throw new Error(`Could not find new space id for old space id ${u.space.id}. This should never happen`);
}
const newSpaceId = context.spaceIdsByOldIds.get(u.space.id);

return { ...u, space: { id: newSpaceId } };
});
return newSpaceId ? { ...u, space: { id: newSpaceId } } : null;
})
.filter(notNull);

return { content_type: { id: newContentTypeId }, url_patterns: newUrlPatterns };
}),
return { content_type: { id: newTypeId }, url_patterns: newUrlPatterns };
}),
})
.toPromise();
},
Expand Down

0 comments on commit eae07e5

Please sign in to comment.