-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
280 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AssetFolderSyncModel } from "../types/syncModel.js"; | ||
import { baseHandler, Handler, makeArrayHandler, makeObjectHandler } from "./combinators.js"; | ||
|
||
export const assetFolderHandler: Handler<AssetFolderSyncModel> = makeObjectHandler({ | ||
name: baseHandler, | ||
folders: { contextfulHandler: () => makeArrayHandler(f => f.codename, assetFolderHandler) }, | ||
}); | ||
|
||
export const assetFoldersHandler: Handler<ReadonlyArray<AssetFolderSyncModel>> = makeArrayHandler( | ||
f => f.codename, | ||
assetFolderHandler, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AssetFolderContracts } from "@kontent-ai/management-sdk"; | ||
|
||
import { omit } from "../../../utils/object.js"; | ||
import { AssetFolderSyncModel } from "../types/syncModel.js"; | ||
|
||
export const transformAssetFolderModel = ( | ||
environmentModel: AssetFolderContracts.IAssetFolderContract, | ||
): AssetFolderSyncModel => ({ | ||
...omit(environmentModel, ["id", "external_id"]), | ||
folders: environmentModel.folders.map(transformAssetFolderModel), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { AssetFolderModels, ManagementClient } from "@kontent-ai/management-sdk"; | ||
import { match } from "ts-pattern"; | ||
|
||
import { logInfo, LogOptions } from "../../../log.js"; | ||
import { throwError } from "../../../utils/error.js"; | ||
import { apply } from "../../../utils/function.js"; | ||
import { omit } from "../../../utils/object.js"; | ||
import { DiffModel } from "../types/diffModel.js"; | ||
import { getTargetCodename, PatchOperation } from "../types/patchOperation.js"; | ||
|
||
export const syncAssetFolders = async ( | ||
client: ManagementClient, | ||
operations: DiffModel["assetFolders"], | ||
logOptions: LogOptions, | ||
) => { | ||
logInfo(logOptions, "standard", "Updating asset folders"); | ||
|
||
if (!operations.length) { | ||
return; | ||
} | ||
|
||
await client | ||
.modifyAssetFolders() | ||
.withData(operations.map(convertOperation)) | ||
.toPromise(); | ||
}; | ||
|
||
const convertOperation = (operation: PatchOperation): AssetFolderModels.IModifyAssetFolderData => { | ||
const targetCodename = apply(codename => ({ codename }), getTargetCodename(operation)); | ||
|
||
return match(omit(operation, ["path"])) | ||
.returnType<AssetFolderModels.IModifyAssetFolderData>() | ||
.with({ op: "addInto" }, op => ({ | ||
...op, | ||
value: op.value as AssetFolderModels.IAssetFolderValue, | ||
reference: targetCodename ?? undefined, | ||
})) | ||
.with( | ||
{ op: "move" }, | ||
op => throwError(`Move operation is not supported for asset folders. Found operation: ${JSON.stringify(op)}`), | ||
) | ||
.with({ op: "replace" }, op => ({ | ||
...omit(op, ["oldValue"]), | ||
op: "rename", | ||
reference: targetCodename ?? throwError(`Missing target codename in ${JSON.stringify(operation)}`), | ||
value: typeof op.value === "string" ? op.value : throwError("Invalid value type"), | ||
})) | ||
.otherwise(op => ({ | ||
...omit(op, ["oldValue"]), | ||
reference: targetCodename ?? throwError(`Missing target codename in ${JSON.stringify(operation)}`), | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { AssetFolderContracts } from "@kontent-ai/management-sdk"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { transformAssetFolderModel } from "../../../src/modules/sync/modelTransfomers/assetFolder.js"; | ||
import { AssetFolderSyncModel } from "../../../src/modules/sync/types/syncModel.js"; | ||
|
||
describe("transformAssetFolderModel", () => { | ||
it("correctly transforms asset folder model to sync model", () => { | ||
const input = { | ||
id: "01160b00-9d58-446d-bd13-5d9ceeb55d3c", | ||
name: "folderName", | ||
codename: "folderCodename", | ||
external_id: "externalId", | ||
folders: [ | ||
{ | ||
id: "folderId", | ||
name: "nestedFolder", | ||
codename: "nestedFolderCodename", | ||
external_id: "nestedExternalId", | ||
folders: [], | ||
}, | ||
], | ||
} as const satisfies AssetFolderContracts.IAssetFolderContract; | ||
|
||
const expectedOutput: AssetFolderSyncModel = { | ||
name: "folderName", | ||
codename: "folderCodename", | ||
folders: [ | ||
{ | ||
name: "nestedFolder", | ||
codename: "nestedFolderCodename", | ||
folders: [], | ||
}, | ||
], | ||
}; | ||
|
||
const result = transformAssetFolderModel(input); | ||
|
||
expect(result).toStrictEqual(expectedOutput); | ||
}); | ||
|
||
it("correctly transforms asset folder model to sync model with nested folders", () => { | ||
const input = { | ||
id: "01160b00-9d58-446d-bd13-5d9ceeb55d3c", | ||
name: "folderName", | ||
codename: "folderCodename", | ||
external_id: "externalId", | ||
folders: [ | ||
{ | ||
id: "folderId", | ||
name: "nestedFolder", | ||
codename: "nestedFolderCodename", | ||
external_id: "nestedExternalId", | ||
folders: [ | ||
{ | ||
id: "nestedFolderId", | ||
name: "nestedNestedFolder", | ||
codename: "nestedNestedFolderCodename", | ||
external_id: "nestedNestedExternalId", | ||
folders: [], | ||
}, | ||
], | ||
}, | ||
], | ||
} as const satisfies AssetFolderContracts.IAssetFolderContract; | ||
|
||
const expectedOutput: AssetFolderSyncModel = { | ||
name: "folderName", | ||
codename: "folderCodename", | ||
folders: [ | ||
{ | ||
name: "nestedFolder", | ||
codename: "nestedFolderCodename", | ||
folders: [ | ||
{ | ||
name: "nestedNestedFolder", | ||
codename: "nestedNestedFolderCodename", | ||
folders: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const result = transformAssetFolderModel(input); | ||
|
||
expect(result).toStrictEqual(expectedOutput); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.