Skip to content

Commit

Permalink
refactor: remove commodo (#4436)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored Dec 16, 2024
1 parent 76ed2a8 commit 4dddcea
Show file tree
Hide file tree
Showing 42 changed files with 730 additions and 659 deletions.
6 changes: 2 additions & 4 deletions packages/api-admin-users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
],
"license": "MIT",
"dependencies": {
"@commodo/fields": "1.1.2-beta.20",
"@webiny/api": "0.0.0",
"@webiny/api-security": "0.0.0",
"@webiny/api-tenancy": "0.0.0",
"@webiny/error": "0.0.0",
"@webiny/handler-graphql": "0.0.0",
"@webiny/pubsub": "0.0.0",
"@webiny/utils": "0.0.0",
"@webiny/validation": "0.0.0",
"commodo-fields-object": "^1.0.6",
"dataloader": "^2.2.2",
"lodash": "^4.17.21",
"md5": "^2.3.0"
"md5": "^2.3.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.2",
Expand Down
55 changes: 28 additions & 27 deletions packages/api-admin-users/src/createAdminUsers/users.validation.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
// @ts-expect-error Package @commodo/fields does not have types.
import { string, withFields } from "@commodo/fields";
// @ts-expect-error Package commodo-fields-object does not have types.
import { object } from "commodo-fields-object";
import { validation } from "@webiny/validation";
import zod from "zod";
import { AdminUsers } from "~/types";
import { createZodError } from "@webiny/utils";

const CreateUserDataModel = withFields({
id: string({ validation: validation.create("minLength:1") }),
displayName: string({ validation: validation.create("minLength:1") }),

const createUserDataValidation = zod.object({
id: zod.string().min(1).optional(),
displayName: zod.string().min(1).optional(),
// We did not use an e-mail validator here, just because external
// IdPs (Okta, Auth0) do not require e-mail to be present. When creating
// admin users, they're actually passing the user's ID as the e-mail.
// For example: packages/api-security-okta/src/createAdminUsersHooks.ts:13
// In the future, we might want to rename this field to `idpId` or similar.
email: string({ validation: validation.create("required") }),

firstName: string({ validation: validation.create("minLength:1") }),
lastName: string({ validation: validation.create("minLength:1") }),
avatar: object()
})();
email: zod.string(),
firstName: zod.string().min(1).optional(),
lastName: zod.string().min(1).optional(),
avatar: zod.object({}).passthrough()
});

const UpdateUserDataModel = withFields({
displayName: string({ validation: validation.create("minLength:1") }),
avatar: object(),
firstName: string({ validation: validation.create("minLength:1") }),
lastName: string({ validation: validation.create("minLength:1") }),
group: string(),
team: string()
})();
const updateUserDataValidation = zod.object({
displayName: zod.string().min(1).optional(),
avatar: zod.object({}).passthrough().optional(),
firstName: zod.string().min(1).optional(),
lastName: zod.string().min(1).optional(),
group: zod.string().optional(),
team: zod.string().optional()
});

export const attachUserValidation = (
params: Pick<AdminUsers, "onUserBeforeCreate" | "onUserBeforeUpdate">
) => {
const { onUserBeforeCreate, onUserBeforeUpdate } = params;
onUserBeforeCreate.subscribe(async ({ inputData }) => {
const model = await new CreateUserDataModel().populate(inputData);
await model.validate();
const validation = createUserDataValidation.safeParse(inputData);
if (validation.success) {
return;
}
throw createZodError(validation.error);
});

onUserBeforeUpdate.subscribe(async ({ inputData }) => {
const model = await new UpdateUserDataModel().populate(inputData);
await model.validate();
const validation = updateUserDataValidation.safeParse(inputData);
if (validation.success) {
return;
}
throw createZodError(validation.error);
});
};
1 change: 0 additions & 1 deletion packages/api-admin-users/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
{ "path": "../handler-graphql/tsconfig.build.json" },
{ "path": "../pubsub/tsconfig.build.json" },
{ "path": "../utils/tsconfig.build.json" },
{ "path": "../validation/tsconfig.build.json" },
{ "path": "../api-i18n/tsconfig.build.json" },
{ "path": "../api-wcp/tsconfig.build.json" }
],
Expand Down
3 changes: 0 additions & 3 deletions packages/api-admin-users/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
{ "path": "../handler-graphql" },
{ "path": "../pubsub" },
{ "path": "../utils" },
{ "path": "../validation" },
{ "path": "../api-i18n" },
{ "path": "../api-wcp" }
],
Expand All @@ -34,8 +33,6 @@
"@webiny/pubsub": ["../pubsub/src"],
"@webiny/utils/*": ["../utils/src/*"],
"@webiny/utils": ["../utils/src"],
"@webiny/validation/*": ["../validation/src/*"],
"@webiny/validation": ["../validation/src"],
"@webiny/api-i18n/*": ["../api-i18n/src/*"],
"@webiny/api-i18n": ["../api-i18n/src"],
"@webiny/api-wcp/*": ["../api-wcp/src/*"],
Expand Down
5 changes: 2 additions & 3 deletions packages/api-apw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"author": "Webiny Ltd",
"license": "MIT",
"dependencies": {
"@commodo/fields": "1.1.2-beta.20",
"@webiny/api": "0.0.0",
"@webiny/api-admin-settings": "0.0.0",
"@webiny/api-headless-cms": "0.0.0",
Expand All @@ -35,9 +34,9 @@
"@webiny/plugins": "0.0.0",
"@webiny/pubsub": "0.0.0",
"@webiny/utils": "0.0.0",
"@webiny/validation": "0.0.0",
"dayjs": "^1.11.13",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"zod": "^3.23.8"
},
"devDependencies": {
"@webiny/cli": "0.0.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/api-apw/src/crud/createContentReviewMethods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from "lodash/get";
import { createTopic } from "@webiny/pubsub";
import Error from "@webiny/error";
import {
Expand Down Expand Up @@ -524,7 +523,7 @@ export function createContentReviewMethods(
},
async deleteScheduledAction(id) {
const contentReview = await this.get(id);
const scheduledActionId = get(contentReview, "content.scheduledActionId");
const scheduledActionId = contentReview.content?.scheduledActionId;

/**
* Check if there is any action scheduled for this "content review".
Expand Down
9 changes: 2 additions & 7 deletions packages/api-apw/src/plugins/cms/linkWorkflowToEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import {
} from "~/plugins/cms/utils";
import { HeadlessCms } from "@webiny/api-headless-cms/types";

interface Value {
id: string;
modelId: string;
}

interface LinkWorkflowToEntryParams {
apw: AdvancedPublishingWorkflow;
cms: HeadlessCms;
Expand Down Expand Up @@ -107,8 +102,8 @@ export const linkWorkflowToEntry = (params: LinkWorkflowToEntryParams) => {

const models = await cms.listModels();

const values: Value[] | undefined = scope.data?.entries;
if (!values || Array.isArray(values) === false || values.length === 0) {
const values = scope.data?.entries;
if (!values?.length) {
return;
}

Expand Down
31 changes: 15 additions & 16 deletions packages/api-apw/src/plugins/pageBuilder/linkWorkflowToPage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import get from "lodash/get";
import set from "lodash/set";
import {
ApwWorkflowApplications,
ApwOnPageBeforeCreateTopicParams,
AdvancedPublishingWorkflow,
ApwOnPageBeforeCreateFromTopicParams,
ApwOnPageBeforeCreateTopicParams,
ApwOnPageBeforeUpdateTopicParams,
AdvancedPublishingWorkflow
ApwWorkflowApplications
} from "~/types";
import {
assignWorkflowToPage,
getPagesDiff,
hasPages,
updatePageSettings,
shouldUpdatePages,
assignWorkflowToPage
updatePageSettings
} from "./utils";
import { PageBuilderContextObject } from "@webiny/api-page-builder/graphql/types";

Expand All @@ -34,7 +33,7 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
* If the previous revision(original) already had the "contentReviewId",
* we need to unlink it so that new "contentReview" can be request for the new revision.
*/
const previousContentReviewId = get(original, "settings.apw.contentReviewId");
const previousContentReviewId = original.settings.apw?.contentReviewId;
if (previousContentReviewId) {
page.settings.apw.contentReviewId = null;
}
Expand All @@ -43,7 +42,7 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
* If the previous revision(original) already had the "workflowId",
* we don't need to do anything we'll just let it be copied over.
*/
const previousWorkflowId = get(original, "settings.apw.workflowId");
const previousWorkflowId = original.settings.apw?.workflowId;
if (previousWorkflowId) {
return;
}
Expand All @@ -55,8 +54,8 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
);
pageBuilder.onPageBeforeUpdate.subscribe<ApwOnPageBeforeUpdateTopicParams>(async params => {
const { page, original } = params;
const prevApwWorkflowId = get(original, "settings.apw");
const currentApwWorkflowId = get(page, "settings.apw");
const prevApwWorkflowId = original.settings?.apw;
const currentApwWorkflowId = page.settings?.apw;
/**
* Make sure the apw property doesn't get lost between updates.
* It can happen because we run modal validation in "onBeforePageUpdate" event,
Expand All @@ -69,9 +68,9 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
* If there is a linked "contentReview" for this page and the page "title" has changed.
* Let's update the "title" field in "contentReview".
*/
const linkedContentReviewId = get(page, "settings.apw.contentReviewId");
const prevTitle = get(original, "title");
const newTitle = get(page, "title");
const linkedContentReviewId = page.settings.apw?.contentReviewId;
const prevTitle = original.title;
const newTitle = page.title;

if (linkedContentReviewId && prevTitle !== newTitle) {
await apw.contentReview.update(linkedContentReviewId, { title: newTitle });
Expand All @@ -91,7 +90,7 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
if (hasPages(workflow) === false) {
return;
}
const pages = get(scope, "data.pages") as unknown as string[];
const pages = scope.data?.pages || [];

for (const pid of pages) {
await updatePageSettings({
Expand Down Expand Up @@ -121,8 +120,8 @@ export const linkWorkflowToPage = (params: LinkWorkflowToPageParams) => {
return;
}

const previousPages = get(prevScope, "data.pages", []);
const currentPages = get(scope, "data.pages", []);
const previousPages = prevScope.data?.pages || [];
const currentPages = scope.data?.pages || [];

const { removedPages, addedPages } = getPagesDiff(currentPages, previousPages);
for (const pid of addedPages) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from "lodash/get";
import Error from "@webiny/error";
import {
AdvancedPublishingWorkflow,
Expand All @@ -17,7 +16,7 @@ export const triggerContentReview = (params: TriggerContentReviewParams) => {

pageBuilder.onPageBeforePublish.subscribe<ApwOnPageBeforePublishTopicParams>(
async ({ page }) => {
const contentReviewId = get(page, "settings.apw.contentReviewId");
const contentReviewId = page.settings.apw?.contentReviewId;
if (contentReviewId) {
const contentReview = await apw.contentReview.get(contentReviewId);

Expand All @@ -34,7 +33,7 @@ export const triggerContentReview = (params: TriggerContentReviewParams) => {
return;
}

const workflowId = get(page, "settings.apw.workflowId");
const workflowId = page.settings.apw?.workflowId;

if (workflowId) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from "lodash/get";
import {
AdvancedPublishingWorkflow,
ApwContentReviewStatus,
Expand All @@ -19,7 +18,7 @@ export const updateContentReviewStatus = (params: UpdateContentReviewStatusParam

pageBuilder.onPageAfterPublish.subscribe<ApwOnPageBeforePublishTopicParams>(
async ({ page }) => {
const contentReviewId = get(page, "settings.apw.contentReviewId");
const contentReviewId = page.settings.apw?.contentReviewId;
/**
* Bail out if there is no "content review" linked.
*/
Expand All @@ -46,7 +45,7 @@ export const updateContentReviewStatus = (params: UpdateContentReviewStatusParam
);
pageBuilder.onPageAfterUnpublish.subscribe<ApwOnPageBeforePublishTopicParams>(
async ({ page }) => {
const contentReviewId = get(page, "settings.apw.contentReviewId");
const contentReviewId = page.settings.apw?.contentReviewId;
/**
* Bail out if there is no "content review" linked.
*/
Expand Down
9 changes: 4 additions & 5 deletions packages/api-apw/src/plugins/pageBuilder/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from "lodash/get";
import WebinyError from "@webiny/error";
import {
ApwWorkflow,
Expand All @@ -22,13 +21,13 @@ const isWorkflowApplicable = (page: PageWithWorkflow, workflow: ApwWorkflow) =>
if (scopeType === WorkflowScopeTypes.DEFAULT) {
return true;
} else if (scopeType === WorkflowScopeTypes.CUSTOM) {
const categories = get(workflow, "scope.data.categories");
const categories = workflow.scope.data?.categories;

if (Array.isArray(categories) && categories.includes(page.category)) {
return true;
}

const pages = get(workflow, "scope.data.pages");
const pages = workflow.scope.data?.pages;
if (Array.isArray(pages) && pages.includes(page.pid)) {
return true;
}
Expand Down Expand Up @@ -105,8 +104,8 @@ export const shouldUpdatePages = (
if (prevScope.type !== WorkflowScopeTypes.CUSTOM) {
return true;
}
const prevScopePages: string[] = get(prevScope, "data.pages") as unknown as string[];
const currentScopePages: string[] = get(scope, "data.pages") as unknown as string[];
const prevScopePages = prevScope.data.pages || [];
const currentScopePages = scope.data.pages || [];
/**
* Bail out early if there were no pages assigned previously.
*/
Expand Down
10 changes: 6 additions & 4 deletions packages/api-apw/src/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from "lodash/get";
import { CmsModelField } from "@webiny/api-headless-cms/types";
import { SecurityIdentity } from "@webiny/api-security/types";
import {
Expand Down Expand Up @@ -41,7 +40,10 @@ export const hasReviewer = async (params: HasReviewersParams): Promise<boolean>
};

export const getValue = (object: Record<string, any>, key: string) => {
return get(object, `values.${key}`);
if (!object.values) {
return undefined;
}
return object.values[key];
};

export const getContentReviewStepInitialStatus = (
Expand Down Expand Up @@ -189,8 +191,8 @@ export const workflowByPrecedenceDesc = (a: ApwWorkflow, b: ApwWorkflow) => {
};

export const workflowByCreatedOnDesc = (a: ApwWorkflow, b: ApwWorkflow) => {
const createdOnA = get(a, "createdOn");
const createdOnB = get(b, "createdOn");
const createdOnA = a.createdOn;
const createdOnB = b.createdOn;
/**
* In descending order of workflow createdOn i.e. the most recent one first.
*/
Expand Down
Loading

0 comments on commit 4dddcea

Please sign in to comment.