Skip to content

Commit

Permalink
Add support for importing language variants
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Jan 4, 2024
1 parent ba50e12 commit 6071468
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { contentItemsExportEntity } from "./importExportEntities/entities/conten
import { contentTypesEntity } from "./importExportEntities/entities/contentTypes.js";
import { contentTypesSnippetsEntity } from "./importExportEntities/entities/contentTypesSnippets.js";
import { languagesEntity } from "./importExportEntities/entities/languages.js";
import { languageVariantsExportEntity } from "./importExportEntities/entities/languageVariants.js";
import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js";
import { previewUrlsExportEntity } from "./importExportEntities/entities/previewUrls.js";
import { rolesExportEntity } from "./importExportEntities/entities/roles.js";
import { spacesExportEntity } from "./importExportEntities/entities/spaces.js";
Expand Down Expand Up @@ -60,7 +60,7 @@ const entityDefinitions: ReadonlyArray<EntityExportDefinition<any>> = [
contentTypesSnippetsEntity,
contentTypesEntity,
contentItemsExportEntity,
languageVariantsExportEntity,
languageVariantsEntity,
assetFoldersEntity,
assetsEntity,
];
Expand Down
10 changes: 6 additions & 4 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 { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js";
import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsEntity } from "./importExportEntities/entities/workflows.js";
import { EntityImportDefinition, ImportContext } from "./importExportEntities/entityDefinition.js";
Expand Down Expand Up @@ -59,6 +60,7 @@ const entityDefinitions: ReadonlyArray<EntityImportDefinition<any>> = [
updateItemAndTypeReferencesInSnippetsImportEntity,
updateItemAndTypeReferencesInTypesImportEntity,
workflowsEntity,
languageVariantsEntity,
];

type ImportEntitiesParams = Readonly<{
Expand Down Expand Up @@ -105,9 +107,9 @@ const createInitialContext = (): ImportContext => ({
taxonomyTermIdsByOldIds: new Map(),
assetFolderIdsByOldIds: new Map(),
assetIdsByOldIds: new Map(),
contentTypeSnippetIdsWithElementsByOldIds: new Map(),
contentItemIdsByOldIds: new Map(),
contentTypeIdsWithElementsByOldIds: new Map(),
contentTypeSnippetContextWithElementsByOldIds: new Map(),
contentItemContextByOldIds: new Map(),
contentTypeContextWithElementsByOldIds: new Map(),
workflowIdsByOldIds: new Map(),
worfklowStepsIdsByOldIds: new Map(),
worfklowStepsIdsWithTransitionsByOldIds: new Map(),
});
7 changes: 5 additions & 2 deletions src/commands/importExportEntities/entities/contentItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const contentItemsExportEntity: EntityDefinition<ReadonlyArray<ContentIte

return {
...context,
contentItemIdsByOldIds: new Map(zip(fileItems, projectItems).map(([fItem, pItem]) => [fItem.id, pItem.id])),
contentItemContextByOldIds: new Map(
zip(fileItems, projectItems)
.map(([fItem, pItem]) => [fItem.id, { selfId: pItem.id, oldTypeId: fItem.type.id ?? "" }]),
),
};
},
};
Expand All @@ -28,7 +31,7 @@ const createImportItemFetcher =
.addContentItem()
.withData({
...fileItem,
type: { id: context.contentTypeIdsWithElementsByOldIds.get(fileItem.type.id ?? "")?.selfId },
type: { id: context.contentTypeContextWithElementsByOldIds.get(fileItem.type.id ?? "")?.selfId },
collection: { id: context.collectionIdsByOldIds.get(fileItem.collection.id ?? "") },
external_id: fileItem.external_id ?? fileItem.codename,
})
Expand Down
55 changes: 50 additions & 5 deletions src/commands/importExportEntities/entities/contentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContentTypeContracts, ManagementClient } from "@kontent-ai/management-sdk";
import { ContentTypeContracts, ContentTypeElements, ManagementClient } from "@kontent-ai/management-sdk";

import { zip } from "../../../utils/array.js";
import { serially } from "../../../utils/requests.js";
Expand All @@ -18,13 +18,58 @@ export const contentTypesEntity: EntityDefinition<ReadonlyArray<ContentTypeContr

return {
...context,
contentTypeIdsWithElementsByOldIds: new Map(
contentTypeContextWithElementsByOldIds: new Map(
zip(fileTypes, projectTypes)
.map(([fileType, projectType]) => {
const elementIdEntries = zip(fileType.elements, projectType.elements)
.map(([fileEl, projectEl]) => [fileEl.id ?? "", projectEl.id ?? ""] as const);

return [fileType.id, { selfId: projectType.id, elementIdsByOldIds: new Map(elementIdEntries) }];
return [fileType.id, {
selfId: projectType.id,
elementIdsByOldIds: new Map(elementIdEntries),
elementTypeByOldIds: new Map(
fileType.elements.flatMap(el => {
if (el.type === "snippet") {
const typedEl = el as unknown as ContentTypeElements.ISnippetElement;
return [
...context.contentTypeSnippetContextWithElementsByOldIds.get(typedEl.snippet.id ?? "")
?.elementTypeByOldIds ?? [],
];
}

return [[el.id ?? "", el.type]];
}),
),
multiChoiceOptionIdsByOldIdsByOldElementId: new Map(
fileType.elements
.flatMap(el => {
switch (el.type) {
case "snippet": {
const typedEl = el as unknown as ContentTypeElements.ISnippetElement;
return [
...context.contentTypeSnippetContextWithElementsByOldIds.get(typedEl.snippet.id ?? "")
?.multiChoiceOptionIdsByOldIdsByOldElementId ?? [],
];
}
case "multiple_choice": {
const typedEl = el as unknown as ContentTypeElements.IMultipleChoiceElement;
const typedProjectEl = projectType.elements.find(e =>
e.id === el.id
) as ContentTypeElements.IMultipleChoiceElement;

return [[
el.id ?? "",
new Map(
zip(typedEl.options, typedProjectEl.options).map(([fO, pO]) => [fO.id ?? "", pO.id ?? ""]),
),
]];
}
default:
return [];
}
}),
),
}];
}),
),
};
Expand Down Expand Up @@ -82,7 +127,7 @@ const createUpdateTypeItemReferencesFetcher =
.flatMap(
createPatchItemAndTypeReferencesInTypeElement(
params.context,
c => c.contentTypeIdsWithElementsByOldIds.get(type.id)?.elementIdsByOldIds,
c => c.contentTypeContextWithElementsByOldIds.get(type.id)?.elementIdsByOldIds,
),
);

Expand All @@ -92,7 +137,7 @@ const createUpdateTypeItemReferencesFetcher =

return params.client
.modifyContentType()
.byTypeId(params.context.contentTypeIdsWithElementsByOldIds.get(type.id)?.selfId ?? "")
.byTypeId(params.context.contentTypeContextWithElementsByOldIds.get(type.id)?.selfId ?? "")
.withData(patchOps)
.toPromise();
};
37 changes: 32 additions & 5 deletions src/commands/importExportEntities/entities/contentTypesSnippets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContentTypeSnippetContracts, ManagementClient } from "@kontent-ai/management-sdk";
import { ContentTypeElements, ContentTypeSnippetContracts, ManagementClient } from "@kontent-ai/management-sdk";

import { zip } from "../../../utils/array.js";
import { serially } from "../../../utils/requests.js";
Expand All @@ -21,15 +21,42 @@ export const contentTypesSnippetsEntity: EntityDefinition<

return {
...context,
contentTypeSnippetIdsWithElementsByOldIds: new Map(
contentTypeSnippetContextWithElementsByOldIds: new Map(
zip(fileSnippets, projectSnippets)
.map(([fileSnippet, projectSnippet]) => {
const elementIdEntries = zip(fileSnippet.elements, projectSnippet.elements)
.map(([fileEl, projectEl]) => [fileEl.id ?? "", projectEl.id ?? ""] as const);

return [fileSnippet.id, { selfId: projectSnippet.id, elementIdsByOldIds: new Map(elementIdEntries) }];
return [fileSnippet.id, {
selfId: projectSnippet.id,
elementIdsByOldIds: new Map(elementIdEntries),
elementTypeByOldIds: new Map(fileSnippet.elements.map(el => [el.id ?? "", el.type])),
multiChoiceOptionIdsByOldIdsByOldElementId: new Map(
fileSnippet.elements
.flatMap(el => {
switch (el.type) {
case "multiple_choice": {
const typedEl = el as ContentTypeElements.IMultipleChoiceElement;
const projectTypedEl = projectSnippet.elements
.find(e => e.id === el.id) as ContentTypeElements.IMultipleChoiceElement;
return [[
el.id ?? "",
new Map(
zip(typedEl.options, projectTypedEl.options).map(([fO, pO]) => [fO.id ?? "", pO.id ?? ""]),
),
]];
}
default:
return [];
}
}),
),
}];
}),
),
elementTypesByOldElementIdsByOldSnippetIds: new Map(
fileSnippets.map(snippet => [snippet.id, new Map(snippet.elements.map(el => [el.id ?? "", el.type]))]),
),
};
},
};
Expand Down Expand Up @@ -81,7 +108,7 @@ const createUpdateSnippetItemAndTypeReferencesFetcher =
.flatMap(
createPatchItemAndTypeReferencesInTypeElement(
params.context,
c => c.contentTypeSnippetIdsWithElementsByOldIds.get(snippet.id)?.elementIdsByOldIds,
c => c.contentTypeSnippetContextWithElementsByOldIds.get(snippet.id)?.elementIdsByOldIds,
),
);

Expand All @@ -91,7 +118,7 @@ const createUpdateSnippetItemAndTypeReferencesFetcher =

return params.client
.modifyContentTypeSnippet()
.byTypeId(params.context.contentTypeSnippetIdsWithElementsByOldIds.get(snippet.id)?.selfId ?? "")
.byTypeId(params.context.contentTypeSnippetContextWithElementsByOldIds.get(snippet.id)?.selfId ?? "")
.withData(patchOps)
.toPromise();
};
Loading

0 comments on commit 6071468

Please sign in to comment.