diff --git a/src/commands/import.ts b/src/commands/import.ts index e0cfb948..67f9cfa5 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -54,6 +54,8 @@ const importEntities = async (params: ImportEntitiesParams) => { let context: ImportContext = { collectionIdsByOldIds: new Map(), + taxonomyGroupIdsByOldIds: new Map(), + taxonomyTermIdsByOldIds: new Map(), }; await serially(entityDefinitions.map(def => async () => { diff --git a/src/commands/importExportEntities/entities/taxonomies.ts b/src/commands/importExportEntities/entities/taxonomies.ts index acb86ee4..5b705f70 100644 --- a/src/commands/importExportEntities/entities/taxonomies.ts +++ b/src/commands/importExportEntities/entities/taxonomies.ts @@ -1,5 +1,6 @@ import { TaxonomyContracts } from "@kontent-ai/management-sdk"; +import { zip } from "../../../utils/array.js"; import { serially } from "../../../utils/requests.js"; import { EntityDefinition } from "../entityDefinition.js"; @@ -7,12 +8,21 @@ export const taxonomiesEntity: EntityDefinition client.listTaxonomies().toAllPromise().then(res => res.data.items.map(t => t._raw)), serializeEntities: taxonomies => JSON.stringify(taxonomies), - importEntities: async (client, fileTaxonomies) => { - await serially(fileTaxonomies.map(taxonomy => () => - client - .addTaxonomy() - .withData(addExternalIds(taxonomy)) - .toPromise())); + importEntities: async (client, fileTaxonomies, context) => { + const projectTaxonomies = await serially Promise>>( + fileTaxonomies.map(taxonomy => () => + client + .addTaxonomy() + .withData(addExternalIds(taxonomy)) + .toPromise() + .then(res => res.data._raw)) + ); + + return { + ...context, + taxonomyGroupIdsByOldIds: new Map(zip(fileTaxonomies.map(t => t.id), projectTaxonomies.map(t => t.id))), + taxonomyTermIdsByOldIds: new Map(zip(fileTaxonomies.flatMap(t => t.terms), projectTaxonomies.flatMap(t => t.terms)).flatMap(extractTermIdsEntries)), + }; }, deserializeEntities: JSON.parse, }; @@ -22,3 +32,8 @@ const addExternalIds = (taxonomy: TaxonomyContracts.ITaxonomyContract): Taxonomy external_id: taxonomy.external_id ?? taxonomy.codename, terms: taxonomy.terms.map(addExternalIds), }); + +const extractTermIdsEntries = ([fileTaxonomy, projectTaxonomy]: readonly [TaxonomyContracts.ITaxonomyContract, TaxonomyContracts.ITaxonomyContract]): ReadonlyArray => [ + [fileTaxonomy.id, projectTaxonomy.id] as const, + ...zip(fileTaxonomy.terms, projectTaxonomy.terms).flatMap(extractTermIdsEntries), +]; diff --git a/src/commands/importExportEntities/entityDefinition.ts b/src/commands/importExportEntities/entityDefinition.ts index d43410a7..d73db3da 100644 --- a/src/commands/importExportEntities/entityDefinition.ts +++ b/src/commands/importExportEntities/entityDefinition.ts @@ -11,5 +11,9 @@ export type EntityDefinition = Readonly<{ }>; export type ImportContext = Readonly<{ - collectionIdsByOldIds: ReadonlyMap; + collectionIdsByOldIds: IdsMap; + taxonomyGroupIdsByOldIds: IdsMap; + taxonomyTermIdsByOldIds: IdsMap; }>; + +type IdsMap = ReadonlyMap; diff --git a/src/utils/array.ts b/src/utils/array.ts new file mode 100644 index 00000000..692188cb --- /dev/null +++ b/src/utils/array.ts @@ -0,0 +1,22 @@ +export const zip = , T2 extends ReadonlyArray>(arr1: T1, arr2: T2): Zip => + arr1 + .slice(0, Math.min(arr1.length, arr2.length)) + .map((el1, i) => [el1, arr2[i]] as const) as unknown as Zip; + +type Zip, T2 extends ReadonlyArray> = + true extends IsEmptyTuple | IsEmptyTuple + ? readonly [] + : [IsNonEmptyTuple, IsNonEmptyTuple] extends [true, true] + ? ZipTuples + : ZipArrays; + +type IsEmptyTuple> = T extends readonly [] ? true : false; + +type IsNonEmptyTuple> = T extends readonly [any, ...ReadonlyArray] ? true : false; + +type ZipArrays, T2 extends ReadonlyArray> = ReadonlyArray; + +type ZipTuples, T2 extends ReadonlyArray, Accum extends ReadonlyArray = readonly []> = + [T1, T2] extends [readonly [infer First1, ...infer Rest1 extends ReadonlyArray], readonly [infer First2, ...infer Rest2 extends ReadonlyArray]] + ? ZipTuples + : Accum;