-
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
28 changed files
with
394 additions
and
38 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
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,21 @@ | ||
import { zip } from "../../../utils/array.js"; | ||
import { SpaceSyncModel } from "../types/syncModel.js"; | ||
import { | ||
baseHandler, | ||
Handler, | ||
makeLeafObjectHandler, | ||
makeObjectHandler, | ||
makeWholeObjectsHandler, | ||
optionalHandler, | ||
} from "./combinators.js"; | ||
|
||
export const spaceHandler: Handler<SpaceSyncModel> = makeObjectHandler({ | ||
name: baseHandler, | ||
web_spotlight_root_item: optionalHandler(makeLeafObjectHandler({})), | ||
collections: (source, target) => | ||
source.length !== target.length || zip(source, target).some(([s, t]) => s.codename !== t.codename) | ||
? [{ op: "replace", path: "", value: source, oldValue: target }] | ||
: [], | ||
}); | ||
|
||
export const wholeSpacesHandler: Handler<ReadonlyArray<SpaceSyncModel>> = makeWholeObjectsHandler("name"); |
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,24 @@ | ||
import { throwError } from "../../../utils/error.js"; | ||
import { omit } from "../../../utils/object.js"; | ||
import { EnvironmentModel } from "../generateSyncModel.js"; | ||
import { SpaceSyncModel } from "../types/syncModel.js"; | ||
|
||
export const transformSpacesModel = ( | ||
environmentModel: EnvironmentModel, | ||
): ReadonlyArray<SpaceSyncModel> => | ||
environmentModel.spaces.map(space => ({ | ||
...omit(space, ["id"]), | ||
web_spotlight_root_item: space.web_spotlight_root_item | ||
? { | ||
codename: environmentModel.items.find(i => i.id === space.web_spotlight_root_item?.id)?.codename | ||
?? throwError( | ||
`Cannot find web spotlight root item { id: ${space.web_spotlight_root_item.id} } for space { codename: ${space.codename}}.`, | ||
), | ||
} | ||
: undefined, | ||
collections: space.collections | ||
?.map(collection => ({ | ||
codename: environmentModel.collections.find(i => i.id === collection.id)?.codename | ||
?? throwError(`Cannot find collection { id: ${collection.id} } for space { codename: ${space.codename}}.`), | ||
})) ?? [], | ||
})); |
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,57 @@ | ||
import { ManagementClient, SpaceModels } from "@kontent-ai/management-sdk"; | ||
import { match, P } from "ts-pattern"; | ||
|
||
import { logInfo, LogOptions } from "../../../log.js"; | ||
import { throwError } from "../../../utils/error.js"; | ||
import { serially } from "../../../utils/requests.js"; | ||
import { DiffModel } from "../types/diffModel.js"; | ||
import { PatchOperation } from "../types/patchOperation.js"; | ||
|
||
export const syncSpaces = async ( | ||
client: ManagementClient, | ||
model: DiffModel["spaces"], | ||
logOptions: LogOptions, | ||
) => { | ||
logInfo(logOptions, "standard", "Adding new spaces"); | ||
|
||
await serially(model.added.map(space => () => | ||
client | ||
.addSpace() | ||
.withData(space as SpaceModels.IAddSpaceData) | ||
.toPromise() | ||
)); | ||
|
||
logInfo(logOptions, "standard", "Updating spaces"); | ||
|
||
await serially([...model.updated].map(([spaceCodename, operations]) => () => | ||
client | ||
.modifySpace() | ||
.bySpaceCodename(spaceCodename) | ||
.withData(operations.map(convertOperation)) | ||
.toPromise() | ||
)); | ||
|
||
logInfo(logOptions, "standard", "Removing spaces"); | ||
|
||
await serially([...model.deleted].map(spaceCodename => () => | ||
client | ||
.deleteSpace() | ||
.bySpaceCodename(spaceCodename) | ||
.toPromise() | ||
)); | ||
}; | ||
|
||
const convertOperation = (operation: PatchOperation): SpaceModels.IModifySpaceData => | ||
match(operation) | ||
.with({ op: "replace" }, op => | ||
match(op.path.split("/").pop()) | ||
.with( | ||
P.union("name", "web_spotlight_root_item", "collections"), | ||
pName => ({ op: "replace", property_name: pName, value: op.value as any }) as const, | ||
) | ||
.otherwise(() => | ||
throwError(`Patch operation "${JSON.stringify(op)}" has missing or invalid property name in path.`) | ||
)) | ||
.with({ op: P.union("addInto", "remove", "move") }, () => | ||
throwError(`Patch operation "${operation.op}" is not supported for spaces.`)) | ||
.exhaustive(); |
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.