From 924f43d163b553c195b30f27bb785cad47c63e79 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Wed, 3 Jan 2024 13:18:07 +0100 Subject: [PATCH] add preview urls import --- src/commands/export.ts | 4 +- src/commands/import.ts | 2 + .../entities/previewUrls.ts | 48 ++++++++++++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index dee350d0..70cdc8ed 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -13,7 +13,7 @@ import { contentTypesEntity } from "./importExportEntities/entities/contentTypes import { contentTypesSnippetsEntity } from "./importExportEntities/entities/contentTypesSnippets.js"; import { languagesEntity } from "./importExportEntities/entities/languages.js"; import { languageVariantsExportEntity } from "./importExportEntities/entities/languageVariants.js"; -import { previewUrlsExportEntity } from "./importExportEntities/entities/previewUrls.js"; +import { previewUrlsEntity } from "./importExportEntities/entities/previewUrls.js"; import { rolesExportEntity } from "./importExportEntities/entities/roles.js"; import { spacesEntity } from "./importExportEntities/entities/spaces.js"; import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js"; @@ -54,7 +54,7 @@ const entityDefinitions: ReadonlyArray> = [ spacesEntity, taxonomiesEntity, languagesEntity, - previewUrlsExportEntity, + previewUrlsEntity, rolesExportEntity, workflowsEntity, contentTypesSnippetsEntity, diff --git a/src/commands/import.ts b/src/commands/import.ts index 7838ce34..ff36d7af 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -17,6 +17,7 @@ import { updateItemAndTypeReferencesInSnippetsImportEntity, } from "./importExportEntities/entities/contentTypesSnippets.js"; import { languagesEntity } from "./importExportEntities/entities/languages.js"; +import { previewUrlsEntity } from "./importExportEntities/entities/previewUrls.js"; import { spacesEntity } from "./importExportEntities/entities/spaces.js"; import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js"; import { workflowsEntity } from "./importExportEntities/entities/workflows.js"; @@ -61,6 +62,7 @@ const entityDefinitions: ReadonlyArray> = [ updateItemAndTypeReferencesInTypesImportEntity, workflowsEntity, spacesEntity, + previewUrlsEntity, ]; type ImportEntitiesParams = Readonly<{ diff --git a/src/commands/importExportEntities/entities/previewUrls.ts b/src/commands/importExportEntities/entities/previewUrls.ts index 29521772..7baf2e3c 100644 --- a/src/commands/importExportEntities/entities/previewUrls.ts +++ b/src/commands/importExportEntities/entities/previewUrls.ts @@ -2,14 +2,50 @@ import { PreviewContracts } from "@kontent-ai/management-sdk"; import { EntityDefinition } from "../entityDefinition.js"; -export const previewUrlsExportEntity: EntityDefinition = { +export const previewUrlsEntity: EntityDefinition = { name: "previewUrls", fetchEntities: client => client.getPreviewConfiguration().toPromise().then(res => res.rawData), serializeEntities: collections => JSON.stringify(collections), - importEntities: () => { - throw new Error("Not supported yet."); - }, - deserializeEntities: () => { - throw new Error("Not supported yet."); + deserializeEntities: JSON.parse, + importEntities: async (client, previews, context) => { + await client + .modifyPreviewConfiguration() + .withData({ + space_domains: previews.space_domains.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 { ...s, space: { id: newSpaceId } }; + }), + preview_url_patterns: previews.preview_url_patterns.map(p => { + const newContentTypeId = context.contentTypeIdsWithElementsByOldIds.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; + } + + const newSpaceId = context.spaceIdsByOldIds.get(u.space.id); + + if (!newSpaceId) { + throw new Error(`Could not find new space id for old space id ${u.space.id}. This should never happen`); + } + + return { ...u, space: { id: newSpaceId } }; + }); + + return { content_type: { id: newContentTypeId }, url_patterns: newUrlPatterns }; + }), + }) + .toPromise(); }, };