Skip to content

Commit

Permalink
add preview urls import
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Jan 3, 2024
1 parent 4322ffe commit 924f43d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -54,7 +54,7 @@ const entityDefinitions: ReadonlyArray<EntityExportDefinition<any>> = [
spacesEntity,
taxonomiesEntity,
languagesEntity,
previewUrlsExportEntity,
previewUrlsEntity,
rolesExportEntity,
workflowsEntity,
contentTypesSnippetsEntity,
Expand Down
2 changes: 2 additions & 0 deletions src/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -61,6 +62,7 @@ const entityDefinitions: ReadonlyArray<EntityImportDefinition<any>> = [
updateItemAndTypeReferencesInTypesImportEntity,
workflowsEntity,
spacesEntity,
previewUrlsEntity,
];

type ImportEntitiesParams = Readonly<{
Expand Down
48 changes: 42 additions & 6 deletions src/commands/importExportEntities/entities/previewUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,50 @@ import { PreviewContracts } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";

export const previewUrlsExportEntity: EntityDefinition<PreviewContracts.IPreviewConfigurationContract> = {
export const previewUrlsEntity: EntityDefinition<PreviewContracts.IPreviewConfigurationContract> = {
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();
},
};

0 comments on commit 924f43d

Please sign in to comment.