Skip to content

Commit

Permalink
fix scopes handler, adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Sep 24, 2024
1 parent 3c39b6e commit bb8d8ca
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 38 deletions.
38 changes: 16 additions & 22 deletions src/modules/sync/diff/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { zip } from "../../../utils/array.js";
import { WorkflowStepSyncModel, WorkflowSyncModel } from "../types/syncModel.js";
import {
baseHandler,
constantHandler,
Handler,
makeAdjustEntityHandler,
makeArrayHandler,
makeLeafObjectHandler,
makeObjectHandler,
Expand Down Expand Up @@ -35,28 +37,20 @@ export const workflowHandler: Handler<WorkflowSyncModel> = makeObjectHandler({
name: baseHandler,
role_ids: constantHandler,
}),
scopes: (sourceValue, targetValue) => {
const scopeRefsHandler = makeObjectHandler({
codename: baseHandler,
});

const emptyScope = {
content_types: [],
collections: [],
};

const contentTypesOps = sourceValue.flatMap((sourceScope, index) => {
const targetScope = targetValue[index] || emptyScope;
return scopeRefsHandler(sourceScope.content_types, targetScope.content_types);
});

const collectionsOps = sourceValue.flatMap((sourceScope, index) => {
const targetScope = targetValue[index] || emptyScope;
return scopeRefsHandler(sourceScope.collections, targetScope.collections);
});

return [...contentTypesOps, ...collectionsOps];
},
scopes: makeAdjustEntityHandler(
(entity) => entity.map((e, index) => ({ ...e, index: index.toString() })),
makeArrayHandler(
el => el.index,
makeLeafObjectHandler({
content_types: (source, target) =>
source.length === target.length
&& zip(source, target).every(([sourceValue, targetValue]) => sourceValue.codename === targetValue.codename),
collections: (source, target) =>
source.length === target.length
&& zip(source, target).every(([sourceValue, targetValue]) => sourceValue.codename === targetValue.codename),
}),
),
),
});

export const wholeWorkflowsHandler: Handler<ReadonlyArray<WorkflowSyncModel>> = makeWholeObjectsHandler();
7 changes: 6 additions & 1 deletion src/modules/sync/types/syncModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ export type LanguageSyncModel = Replace<
export type WorkflowSyncModel = Replace<
Omit<WorkflowContracts.IWorkflowContract, "id" | "scheduled_step">,
Readonly<{
scopes: Array<{ content_types: Array<CodenameReference>; collections: Array<CodenameReference> }>;
scopes: Array<ScopeSyncModel>;
steps: WorkflowStepSyncModel[];
published_step: Omit<WorkflowContracts.IWorkflowPublishedStepContract, "id">;
archived_step: Omit<WorkflowContracts.IWorkflowArchivedStepContract, "id">;
}>
>;

type ScopeSyncModel = {
content_types: Array<CodenameReference>;
collections: Array<CodenameReference>;
};

export type WorkflowStepSyncModel = Replace<
Omit<WorkflowContracts.IWorkflowStepNewContract, "id">,
Readonly<{ transitions_to: Array<{ step: CodenameReference }> }>
Expand Down
51 changes: 50 additions & 1 deletion tests/unit/syncModel/diff/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe("workflowHandler", () => {
};

const result = workflowHandler(source, target);

expect(result).toStrictEqual(
[
{
Expand Down Expand Up @@ -115,6 +114,56 @@ describe("workflowHandler", () => {
],
},
},
{
oldValue: {
collections: [
{
codename: "collection_1",
},
],
content_types: [
{
codename: "type_1",
},
],
index: "0",
},
op: "replace",
path: "/scopes/codename:0",
value: {
collections: [
{
codename: "collection_1",
},
],
content_types: [
{
codename: "type_1",
},
{
codename: "type_2",
},
],
index: "0",
},
},
{
op: "addInto",
path: "/scopes",
value: {
collections: [
{
codename: "collection_2",
},
],
content_types: [
{
codename: "type_3",
},
],
index: "1",
},
},
],
);
});
Expand Down
15 changes: 1 addition & 14 deletions tests/unit/syncModel/workflowTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,7 @@ describe("transformWorkflowModel", () => {
id: "1cdb6e21-3330-4f0f-88cd-171098950e4f",
name: "type 1",
codename: "type_codename_1",
elements: [
{
id: "6e72b773-ec9b-464c-90cd-4eda8b10c266",
codename: "text_codename1",
name: "element 1",
type: "text",
},
{
id: "45d12a44-42df-485d-94a0-7692bcbde8d9",
codename: "text_codename2",
name: "element2 ",
type: "text",
},
],
elements: [],
last_modified: "",
},
] as const satisfies ReadonlyArray<ContentTypeContracts.IContentTypeContract>;
Expand Down

0 comments on commit bb8d8ca

Please sign in to comment.