Skip to content

Commit

Permalink
divide remove operations in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Sep 9, 2024
1 parent ef88a45 commit 6120d04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/modules/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand Down
15 changes: 13 additions & 2 deletions src/modules/sync/sync/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down

0 comments on commit 6120d04

Please sign in to comment.