diff --git a/src/commands/export.ts b/src/commands/export.ts index 74d15f3e..a24befd6 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 { languageVariantsEntity } 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 2de069b9..1317484f 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -18,6 +18,7 @@ import { } from "./importExportEntities/entities/contentTypesSnippets.js"; import { languagesEntity } from "./importExportEntities/entities/languages.js"; import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.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"; @@ -62,6 +63,7 @@ const entityDefinitions: ReadonlyArray> = [ updateItemAndTypeReferencesInTypesImportEntity, workflowsEntity, spacesEntity, + previewUrlsEntity, languageVariantsEntity, ]; @@ -115,5 +117,4 @@ const createInitialContext = (): ImportContext => ({ workflowIdsByOldIds: new Map(), worfklowStepsIdsWithTransitionsByOldIds: new Map(), spaceIdsByOldIds: new Map(), - contentTypeIdsWithElementsByOldIds: new Map(), }); diff --git a/src/commands/importExportEntities/entities/previewUrls.ts b/src/commands/importExportEntities/entities/previewUrls.ts index 29521772..ace3c766 100644 --- a/src/commands/importExportEntities/entities/previewUrls.ts +++ b/src/commands/importExportEntities/entities/previewUrls.ts @@ -2,14 +2,51 @@ 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.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; + } + + 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(); }, }; diff --git a/src/commands/importExportEntities/entityDefinition.ts b/src/commands/importExportEntities/entityDefinition.ts index 871a05a4..e918a948 100644 --- a/src/commands/importExportEntities/entityDefinition.ts +++ b/src/commands/importExportEntities/entityDefinition.ts @@ -75,7 +75,6 @@ export type ImportContext = Readonly<{ string, Readonly<{ selfId: string; oldTransitionIds: ReadonlyArray }> >; - contentTypeIdsWithElementsByOldIds: ReadonlyMap>; spaceIdsByOldIds: IdsMap; }>;