From eae07e555f11a86a0c4f536ff2cec0bef841f764 Mon Sep 17 00:00:00 2001 From: Jiri Lojda Date: Mon, 8 Jan 2024 14:31:01 +0100 Subject: [PATCH] Don't import preview urls for non-existent types or spaces --- .../entities/previewUrls.ts | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/commands/importExportEntities/entities/previewUrls.ts b/src/commands/importExportEntities/entities/previewUrls.ts index ace3c766..330fbbdc 100644 --- a/src/commands/importExportEntities/entities/previewUrls.ts +++ b/src/commands/importExportEntities/entities/previewUrls.ts @@ -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 = { @@ -15,37 +16,33 @@ export const previewUrlsEntity: EntityDefinition { 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(); },