diff --git a/src/modules/sync/sync.ts b/src/modules/sync/sync.ts index 7e585560..a495ee85 100644 --- a/src/modules/sync/sync.ts +++ b/src/modules/sync/sync.ts @@ -3,7 +3,7 @@ import { ManagementClient } from "@kontent-ai/management-sdk"; import { logInfo, LogOptions } from "../../log.js"; import { serially } from "../../utils/requests.js"; import { syncAssetFolders } from "./sync/assetFolders.js"; -import { syncCollections } from "./sync/collections.js"; +import { syncAddAndReplaceCollections, syncRemoveCollections } from "./sync/collections.js"; import { addElementsIntoSnippetsWithoutReferences, addSnippetsReferences, @@ -22,7 +22,9 @@ export const sync = async (client: ManagementClient, diff: DiffModel, logOptions await syncAssetFolders(client, diff.assetFolders, logOptions); logInfo(logOptions, "standard", "Syncing Collections"); - await syncCollections(client, diff.collections); + await syncAddAndReplaceCollections(client, diff.collections); + + await syncRemoveCollections(client, diff.collections); await syncTaxonomies(client, diff.taxonomyGroups, logOptions); diff --git a/src/modules/sync/sync/collections.ts b/src/modules/sync/sync/collections.ts index 0cd2c389..975f919e 100644 --- a/src/modules/sync/sync/collections.ts +++ b/src/modules/sync/sync/collections.ts @@ -4,14 +4,25 @@ import { omit } from "../../../utils/object.js"; import { DiffModel } from "../types/diffModel.js"; import { getTargetCodename, PatchOperation } from "../types/patchOperation.js"; -export const syncCollections = (client: ManagementClient, collections: DiffModel["collections"]) => { +export const syncAddAndReplaceCollections = (client: ManagementClient, collections: DiffModel["collections"]) => { if (!collections.length) { return Promise.resolve(); } return client .setCollections() - .withData(collections.map(transformCollectionsReferences)) + .withData(collections.filter(op => op.op !== "remove").map(transformCollectionsReferences)) + .toPromise(); +}; + +export const syncRemoveCollections = (client: ManagementClient, collections: DiffModel["collections"]) => { + if (!collections.length) { + return Promise.resolve(); + } + + return client + .setCollections() + .withData(collections.filter(op => op.op === "remove").map(transformCollectionsReferences)) .toPromise(); };