Skip to content

Commit

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

Expand Down Expand Up @@ -115,5 +117,4 @@ const createInitialContext = (): ImportContext => ({
workflowIdsByOldIds: new Map(),
worfklowStepsIdsWithTransitionsByOldIds: new Map(),
spaceIdsByOldIds: new Map(),
contentTypeIdsWithElementsByOldIds: new Map(),
});
49 changes: 43 additions & 6 deletions src/commands/importExportEntities/entities/previewUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,51 @@ 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.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();
},
};
1 change: 0 additions & 1 deletion src/commands/importExportEntities/entityDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export type ImportContext = Readonly<{
string,
Readonly<{ selfId: string; oldTransitionIds: ReadonlyArray<string> }>
>;
contentTypeIdsWithElementsByOldIds: ReadonlyMap<string, Readonly<{ selfId: string; elementIdsByOldIds: IdsMap }>>;
spaceIdsByOldIds: IdsMap;
}>;

Expand Down

0 comments on commit db9e56c

Please sign in to comment.