From 1cf3da856c64394df9d3f9d8d2be267dde5c123e Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Tue, 21 Mar 2023 14:59:20 +0100 Subject: [PATCH 1/7] feat(review-workflows): add update route on entity for stages --- .../controllers/workflows/stages/index.js | 26 +++++- packages/core/admin/ee/server/routes/index.js | 17 ++++ .../review-workflows/review-workflows.js | 6 +- .../services/review-workflows/stages.js | 17 +++- .../server/tests/review-workflows.test.api.js | 84 ++++++++++++++++--- .../ee/server/validation/review-workflows.js | 7 ++ 6 files changed, 144 insertions(+), 13 deletions(-) diff --git a/packages/core/admin/ee/server/controllers/workflows/stages/index.js b/packages/core/admin/ee/server/controllers/workflows/stages/index.js index 3cecc9430d..f3833bdd90 100644 --- a/packages/core/admin/ee/server/controllers/workflows/stages/index.js +++ b/packages/core/admin/ee/server/controllers/workflows/stages/index.js @@ -1,7 +1,11 @@ 'use strict'; +const { ApplicationError } = require('@strapi/utils/lib/errors'); const { getService } = require('../../../utils'); -const { validateUpdateStages } = require('../../../validation/review-workflows'); +const { + validateUpdateStages, + validateUpdateStageOnEntity, +} = require('../../../validation/review-workflows'); module.exports = { /** @@ -54,4 +58,24 @@ module.exports = { ctx.body = { data }; }, + + async updateEntity(ctx) { + const stagesService = getService('stages'); + const reviewWorkflowsService = getService('review-workflows'); + const { model_uid: modelUID, id: entityId } = ctx.params; + + const { id: stageId } = await validateUpdateStageOnEntity( + ctx.request?.body?.data, + 'You shall pass an id to the body of the put request.' + ); + + if (!reviewWorkflowsService.isReviewWorkflowEnabled({ strapi }, modelUID)) { + throw new ApplicationError(`Review workflows is not activated on ${modelUID}.`); + } + + // TODO When multiple workflows are possible, check if the stage is part of the right one + // Didn't need this today as their can only be one workflow + + ctx.body = await stagesService.updateEntity({ id: entityId, modelUID }, stageId); + }, }; diff --git a/packages/core/admin/ee/server/routes/index.js b/packages/core/admin/ee/server/routes/index.js index f5f1cec2b0..e789639768 100644 --- a/packages/core/admin/ee/server/routes/index.js +++ b/packages/core/admin/ee/server/routes/index.js @@ -209,4 +209,21 @@ module.exports = [ ], }, }, + { + method: 'PUT', + path: '/content-manager/(collection|single)-types/:model_uid/:id/stage', + handler: 'stages.updateEntity', + config: { + middlewares: [enableFeatureMiddleware('review-workflows')], + policies: [ + 'admin::isAuthenticatedAdmin', + { + name: 'admin::hasPermissions', + config: { + actions: ['admin::review-workflows.read'], + }, + }, + ], + }, + }, ]; diff --git a/packages/core/admin/ee/server/services/review-workflows/review-workflows.js b/packages/core/admin/ee/server/services/review-workflows/review-workflows.js index bc1cc613f0..faa4c04d5c 100644 --- a/packages/core/admin/ee/server/services/review-workflows/review-workflows.js +++ b/packages/core/admin/ee/server/services/review-workflows/review-workflows.js @@ -11,7 +11,7 @@ const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows'); const { disableOnContentTypes: disableReviewWorkflows, } = require('../../migrations/review-workflows'); -const { getDefaultWorkflow } = require('../../utils/review-workflows'); +const { getDefaultWorkflow, hasRWEnabled } = require('../../utils/review-workflows'); const getContentTypeUIDsWithActivatedReviewWorkflows = pipe([ // Pick only content-types with reviewWorkflows options set to true @@ -170,5 +170,9 @@ module.exports = ({ strapi }) => { strapi.hook('strapi::content-types.afterSync').register(enableReviewWorkflow({ strapi })); strapi.hook('strapi::content-types.afterSync').register(disableReviewWorkflows); }, + isReviewWorkflowEnabled({ strapi }, modelUID) { + const contentType = strapi.container.get('content-types').get(modelUID); + return hasRWEnabled(contentType); + }, }; }; diff --git a/packages/core/admin/ee/server/services/review-workflows/stages.js b/packages/core/admin/ee/server/services/review-workflows/stages.js index 79adacfaf5..8a24900a7c 100644 --- a/packages/core/admin/ee/server/services/review-workflows/stages.js +++ b/packages/core/admin/ee/server/services/review-workflows/stages.js @@ -5,7 +5,7 @@ const { errors: { ApplicationError }, } = require('@strapi/utils'); -const { STAGE_MODEL_UID } = require('../../constants/workflows'); +const { STAGE_MODEL_UID, ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows'); const { getService } = require('../../utils'); module.exports = ({ strapi }) => { @@ -69,6 +69,21 @@ module.exports = ({ strapi }) => { }); }); }, + + /** + * Update the stage of an entity + * + * @param {object} entityInfo + * @param {string} entityInfo.id - Entity id + * @param {string} entityInfo.modelUID - the content-type of the entity + * @param {string} stageId - The id of the stage to assign to the entity + */ + updateEntity(entityInfo, stageId) { + return strapi.entityService.update(entityInfo.modelUID, entityInfo.id, { + data: { [ENTITY_STAGE_ATTRIBUTE]: Number(stageId) }, + populate: [ENTITY_STAGE_ATTRIBUTE], + }); + }, }; }; diff --git a/packages/core/admin/ee/server/tests/review-workflows.test.api.js b/packages/core/admin/ee/server/tests/review-workflows.test.api.js index ad4296ab0b..ff7fcda880 100644 --- a/packages/core/admin/ee/server/tests/review-workflows.test.api.js +++ b/packages/core/admin/ee/server/tests/review-workflows.test.api.js @@ -101,6 +101,20 @@ describeOnCondition(edition === 'EE')('Review workflows', () => { await builder.cleanup(); }); + beforeEach(async () => { + testWorkflow = await strapi.query(WORKFLOW_MODEL_UID).update({ + where: { id: testWorkflow.id }, + data: { + uid: 'workflow', + stages: [defaultStage.id, secondStage.id], + }, + }); + await updateContentType(productUID, { + components: [], + contentType: model, + }); + }); + describe('Get workflows', () => { test("It shouldn't be available for public", async () => { const res = await requests.public.get('/admin/review-workflows/workflows'); @@ -328,8 +342,6 @@ describeOnCondition(edition === 'EE')('Review workflows', () => { }); describe('Enabling/Disabling review workflows on a content type', () => { - let response; - beforeAll(async () => { await createEntry(productUID, { name: 'Product' }); await createEntry(productUID, { name: 'Product 1' }); @@ -343,18 +355,18 @@ describeOnCondition(edition === 'EE')('Review workflows', () => { }); await restart(); - response = await requests.admin({ + const response = await requests.admin({ method: 'GET', url: `/content-type-builder/content-types/api::product.product`, }); expect(response.body.data.schema.reviewWorkflows).toBeTruthy(); - response = await getRWMorphTableResults(strapi.db.getConnection()); + const morphTableResults = await getRWMorphTableResults(strapi.db.getConnection()); - expect(response.length).toEqual(3); - for (let i = 0; i < response.length; i += 1) { - const entry = response[i]; + expect(morphTableResults.length).toEqual(3); + for (let i = 0; i < morphTableResults.length; i += 1) { + const entry = morphTableResults[i]; expect(entry.related_id).toEqual(i + 1); expect(entry.order).toEqual(1); } @@ -368,14 +380,66 @@ describeOnCondition(edition === 'EE')('Review workflows', () => { await restart(); - response = await requests.admin({ + const response = await requests.admin({ method: 'GET', url: `/content-type-builder/content-types/api::product.product`, }); expect(response.body.data.schema.reviewWorkflows).toBeFalsy(); - response = await getRWMorphTableResults(strapi.db.getConnection()); - expect(response.length).toEqual(0); + const morphTableResults = await getRWMorphTableResults(strapi.db.getConnection()); + expect(morphTableResults.length).toEqual(0); + }); + }); + + describe('update a stage on an entity', () => { + describe('Review Workflow is enabled', () => { + beforeAll(async () => { + await updateContentType(productUID, { + components: [], + contentType: { ...model, reviewWorkflows: true }, + }); + await restart(); + }); + test('Should update the accordingly on an entity', async () => { + const entry = await createEntry(productUID, { name: 'Product' }); + + const response = await requests.admin({ + method: 'PUT', + url: `/admin/content-manager/collection-types/${productUID}/${entry.id}/stage`, + body: { + data: { id: secondStage.id }, + }, + }); + + expect(response.status).toEqual(200); + expect(response.body[ENTITY_STAGE_ATTRIBUTE]).toEqual( + expect.objectContaining({ id: secondStage.id }) + ); + }); + }); + describe('Review Workflow is disabled', () => { + beforeAll(async () => { + await updateContentType(productUID, { + components: [], + contentType: { ...model, reviewWorkflows: false }, + }); + await restart(); + }); + test('Should not update the entity', async () => { + const entry = await createEntry(productUID, { name: 'Product' }); + + const response = await requests.admin({ + method: 'PUT', + url: `/admin/content-manager/collection-types/${productUID}/${entry.id}/stage`, + body: { + data: { id: secondStage.id }, + }, + }); + + expect(response.status).toEqual(400); + expect(response.body.error).toBeDefined(); + expect(response.body.error.name).toBe('ApplicationError'); + }); }); }); diff --git a/packages/core/admin/ee/server/validation/review-workflows.js b/packages/core/admin/ee/server/validation/review-workflows.js index caad7726a7..4f67c8645f 100644 --- a/packages/core/admin/ee/server/validation/review-workflows.js +++ b/packages/core/admin/ee/server/validation/review-workflows.js @@ -8,10 +8,17 @@ const stageObject = yup.object().shape({ }); const validateUpdateStagesSchema = yup.array().of(stageObject).required(); +const validateUpdateStageOnEntity = yup + .object() + .shape({ + id: yup.number().integer().min(1).required(), + }) + .required(); module.exports = { validateUpdateStages: validateYupSchema(validateUpdateStagesSchema, { strict: false, stripUnknown: true, }), + validateUpdateStageOnEntity: validateYupSchema(validateUpdateStageOnEntity), }; From 653f5b0db4f059587c242d6ef9d6770e9ee84184 Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Fri, 24 Mar 2023 15:54:35 +0100 Subject: [PATCH 2/7] feat(review-workflows): update the way to get workflow stages --- .../pages/EditView/InformationBox/InformationBoxEE.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js index 3bc9b3a4a8..d6e60201ac 100644 --- a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js +++ b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js @@ -8,6 +8,7 @@ import { import { Field, FieldLabel, FieldError, Flex } from '@strapi/design-system'; import { useIntl } from 'react-intl'; import { useMutation } from 'react-query'; +import { get } from 'lodash/fp'; import { useReviewWorkflows } from '../../../../pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows'; import Information from '../../../../../../admin/src/content-manager/pages/EditView/Information'; @@ -25,9 +26,7 @@ export function InformationBoxEE() { const { formatMessage } = useIntl(); const { formatAPIError } = useAPIErrorHandler(); - const { - workflows: { data: workflow }, - } = useReviewWorkflows(activeWorkflowStage?.id); + const workflow = get('workflows.data[0]', useReviewWorkflows()); const { error, isLoading, mutateAsync } = useMutation(async ({ entityId, stageId, uid }) => { const { From 54a4e0bc8c85c8e1f90fb7f524989d5b1732b4c3 Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Mon, 27 Mar 2023 10:02:48 +0200 Subject: [PATCH 3/7] feat(review-workflows): remove use of lodash --- .../pages/EditView/InformationBox/InformationBoxEE.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js index d6e60201ac..c75010f775 100644 --- a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js +++ b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js @@ -8,7 +8,6 @@ import { import { Field, FieldLabel, FieldError, Flex } from '@strapi/design-system'; import { useIntl } from 'react-intl'; import { useMutation } from 'react-query'; -import { get } from 'lodash/fp'; import { useReviewWorkflows } from '../../../../pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows'; import Information from '../../../../../../admin/src/content-manager/pages/EditView/Information'; @@ -26,7 +25,10 @@ export function InformationBoxEE() { const { formatMessage } = useIntl(); const { formatAPIError } = useAPIErrorHandler(); - const workflow = get('workflows.data[0]', useReviewWorkflows()); + const { + workflow: { data: workflows }, + } = useReviewWorkflows(activeWorkflowStage?.workflow?.id); + const workflow = workflows?.at(0); const { error, isLoading, mutateAsync } = useMutation(async ({ entityId, stageId, uid }) => { const { From cd785abf8f9161c990bdc4897b095ff7add42602 Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Mon, 27 Mar 2023 11:03:47 +0200 Subject: [PATCH 4/7] feat(review-workflows): add jsdoc - manage types --- .../server/controllers/workflows/stages/index.js | 16 +++++++++++++++- .../server/services/review-workflows/stages.js | 6 +++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/core/admin/ee/server/controllers/workflows/stages/index.js b/packages/core/admin/ee/server/controllers/workflows/stages/index.js index f3833bdd90..14deea2e18 100644 --- a/packages/core/admin/ee/server/controllers/workflows/stages/index.js +++ b/packages/core/admin/ee/server/controllers/workflows/stages/index.js @@ -59,10 +59,24 @@ module.exports = { ctx.body = { data }; }, + /** + * Updates an entity's stage. + * @async + * @param {Object} ctx - The Koa context object. + * @param {Object} ctx.params - An object containing the parameters from the request URL. + * @param {string} ctx.params.model_uid - The model UID of the entity. + * @param {string} ctx.params.id - The ID of the entity to update. + * @param {Object} ctx.request.body.data - Optional data object containing the new stage ID for the entity. + * @param {string} ctx.request.body.data.id - The ID of the new stage for the entity. + * @throws {ApplicationError} If review workflows is not activated on the specified model UID. + * @throws {ValidationError} If the `data` object in the request body fails to pass validation. + * @returns {Promise} A promise that resolves when the entity's stage has been updated. + */ async updateEntity(ctx) { const stagesService = getService('stages'); const reviewWorkflowsService = getService('review-workflows'); - const { model_uid: modelUID, id: entityId } = ctx.params; + const { model_uid: modelUID, id: entityIdString } = ctx.params; + const entityId = Number(entityIdString); const { id: stageId } = await validateUpdateStageOnEntity( ctx.request?.body?.data, diff --git a/packages/core/admin/ee/server/services/review-workflows/stages.js b/packages/core/admin/ee/server/services/review-workflows/stages.js index 8a24900a7c..1fe1f88190 100644 --- a/packages/core/admin/ee/server/services/review-workflows/stages.js +++ b/packages/core/admin/ee/server/services/review-workflows/stages.js @@ -74,13 +74,13 @@ module.exports = ({ strapi }) => { * Update the stage of an entity * * @param {object} entityInfo - * @param {string} entityInfo.id - Entity id + * @param {number} entityInfo.id - Entity id * @param {string} entityInfo.modelUID - the content-type of the entity - * @param {string} stageId - The id of the stage to assign to the entity + * @param {number} stageId - The id of the stage to assign to the entity */ updateEntity(entityInfo, stageId) { return strapi.entityService.update(entityInfo.modelUID, entityInfo.id, { - data: { [ENTITY_STAGE_ATTRIBUTE]: Number(stageId) }, + data: { [ENTITY_STAGE_ATTRIBUTE]: stageId }, populate: [ENTITY_STAGE_ATTRIBUTE], }); }, From 26d66c6a69da3db3be8cd30c2da7069ec3ff9ac2 Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Mon, 27 Mar 2023 11:43:52 +0200 Subject: [PATCH 5/7] fix(review-workflows): add default value on destructuring --- .../pages/EditView/InformationBox/InformationBoxEE.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js index c75010f775..504f72b126 100644 --- a/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js +++ b/packages/core/admin/ee/admin/content-manager/pages/EditView/InformationBox/InformationBoxEE.js @@ -25,10 +25,7 @@ export function InformationBoxEE() { const { formatMessage } = useIntl(); const { formatAPIError } = useAPIErrorHandler(); - const { - workflow: { data: workflows }, - } = useReviewWorkflows(activeWorkflowStage?.workflow?.id); - const workflow = workflows?.at(0); + const { workflows: { data: [workflow] = [] } = {} } = useReviewWorkflows(); const { error, isLoading, mutateAsync } = useMutation(async ({ entityId, stageId, uid }) => { const { From f9507877ecfa741adbd592f3d050bc36f1ee757f Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Mon, 27 Mar 2023 11:58:06 +0200 Subject: [PATCH 6/7] feat(review-workflows): add update route on entity for stages --- .../controllers/workflows/stages/index.js | 4 ++-- .../ee/server/migrations/review-workflows.js | 7 +++++-- .../entity-service-decorator.js | 5 ++--- .../review-workflows/review-workflows.js | 8 ++------ .../admin/ee/server/utils/review-workflows.js | 18 ++++++++++++------ 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/core/admin/ee/server/controllers/workflows/stages/index.js b/packages/core/admin/ee/server/controllers/workflows/stages/index.js index 14deea2e18..2a274e37be 100644 --- a/packages/core/admin/ee/server/controllers/workflows/stages/index.js +++ b/packages/core/admin/ee/server/controllers/workflows/stages/index.js @@ -2,6 +2,7 @@ const { ApplicationError } = require('@strapi/utils/lib/errors'); const { getService } = require('../../../utils'); +const { hasReviewWorkflow } = require('../../../utils/review-workflows'); const { validateUpdateStages, validateUpdateStageOnEntity, @@ -74,7 +75,6 @@ module.exports = { */ async updateEntity(ctx) { const stagesService = getService('stages'); - const reviewWorkflowsService = getService('review-workflows'); const { model_uid: modelUID, id: entityIdString } = ctx.params; const entityId = Number(entityIdString); @@ -83,7 +83,7 @@ module.exports = { 'You shall pass an id to the body of the put request.' ); - if (!reviewWorkflowsService.isReviewWorkflowEnabled({ strapi }, modelUID)) { + if (!hasReviewWorkflow({ strapi }, modelUID)) { throw new ApplicationError(`Review workflows is not activated on ${modelUID}.`); } diff --git a/packages/core/admin/ee/server/migrations/review-workflows.js b/packages/core/admin/ee/server/migrations/review-workflows.js index 8b04d69f79..a86c007a11 100644 --- a/packages/core/admin/ee/server/migrations/review-workflows.js +++ b/packages/core/admin/ee/server/migrations/review-workflows.js @@ -1,6 +1,6 @@ 'use strict'; -const { hasRWEnabled } = require('../utils/review-workflows'); +const { hasReviewWorkflow } = require('../utils/review-workflows'); /** * Remove all stage information for all content types that have had review workflows disabled @@ -16,7 +16,10 @@ const disableOnContentTypes = async ({ oldContentTypes, contentTypes }) => { const oldContentType = oldContentTypes[uid]; const contentType = contentTypes?.[uid]; - if (hasRWEnabled(oldContentType) && !hasRWEnabled(contentType)) { + if ( + hasReviewWorkflow({ strapi }, oldContentType) && + !hasReviewWorkflow({ strapi }, contentType) + ) { // If review workflows has been turned off on a content type // remove stage information from all entities within this CT uidsToRemove.push(uid); diff --git a/packages/core/admin/ee/server/services/review-workflows/entity-service-decorator.js b/packages/core/admin/ee/server/services/review-workflows/entity-service-decorator.js index 121af298e4..01bb383488 100644 --- a/packages/core/admin/ee/server/services/review-workflows/entity-service-decorator.js +++ b/packages/core/admin/ee/server/services/review-workflows/entity-service-decorator.js @@ -2,7 +2,7 @@ const { isNil } = require('lodash/fp'); const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows'); -const { hasRWEnabled, getDefaultWorkflow } = require('../../utils/review-workflows'); +const { hasReviewWorkflow, getDefaultWorkflow } = require('../../utils/review-workflows'); /** * Assigns the entity data to the default workflow stage if no stage is present in the data @@ -23,8 +23,7 @@ const getDataWithStage = async (data) => { */ const decorator = (service) => ({ async create(uid, opts = {}) { - const model = strapi.getModel(uid); - const hasRW = hasRWEnabled(model); + const hasRW = hasReviewWorkflow({ strapi }, uid); if (!hasRW) { return service.create.call(this, uid, opts); diff --git a/packages/core/admin/ee/server/services/review-workflows/review-workflows.js b/packages/core/admin/ee/server/services/review-workflows/review-workflows.js index faa4c04d5c..25726355cd 100644 --- a/packages/core/admin/ee/server/services/review-workflows/review-workflows.js +++ b/packages/core/admin/ee/server/services/review-workflows/review-workflows.js @@ -11,7 +11,7 @@ const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows'); const { disableOnContentTypes: disableReviewWorkflows, } = require('../../migrations/review-workflows'); -const { getDefaultWorkflow, hasRWEnabled } = require('../../utils/review-workflows'); +const { getDefaultWorkflow } = require('../../utils/review-workflows'); const getContentTypeUIDsWithActivatedReviewWorkflows = pipe([ // Pick only content-types with reviewWorkflows options set to true @@ -120,7 +120,7 @@ function enableReviewWorkflow({ strapi }) { [joinTable.joinColumn.name]: firstStage.id, [typeColumn.name]: connection.raw('?', [contentTypeUID]), }) - .leftJoin(`${joinTable.name} AS jointable`, function () { + .leftJoin(`${joinTable.name} AS jointable`, () => { this.on('entity.id', '=', `jointable.${idColumn.name}`).andOn( `jointable.${typeColumn.name}`, '=', @@ -170,9 +170,5 @@ module.exports = ({ strapi }) => { strapi.hook('strapi::content-types.afterSync').register(enableReviewWorkflow({ strapi })); strapi.hook('strapi::content-types.afterSync').register(disableReviewWorkflows); }, - isReviewWorkflowEnabled({ strapi }, modelUID) { - const contentType = strapi.container.get('content-types').get(modelUID); - return hasRWEnabled(contentType); - }, }; }; diff --git a/packages/core/admin/ee/server/utils/review-workflows.js b/packages/core/admin/ee/server/utils/review-workflows.js index ca5ece48e9..5531fd2578 100644 --- a/packages/core/admin/ee/server/utils/review-workflows.js +++ b/packages/core/admin/ee/server/utils/review-workflows.js @@ -3,17 +3,23 @@ const { WORKFLOW_MODEL_UID } = require('../constants/workflows'); /** - * Determine if a content type has the review workflows feature enabled - * @param {Object} contentType - * @returns + * Checks if a content type has review workflows enabled. + * @param {string|Object} contentType - Either the modelUID of the content type, or the content type object. + * @returns {boolean} Whether review workflows are enabled for the specified content type. */ -const hasRWEnabled = (contentType) => contentType?.options?.reviewWorkflows || false; - +function hasReviewWorkflow({ strapi }, contentType) { + if (typeof contentType === 'string') { + // If the input is a string, assume it's the modelUID of the content type and retrieve the corresponding object. + return hasReviewWorkflow({ strapi }, strapi.getModel(contentType)); + } + // Otherwise, assume it's the content type object itself and return its `reviewWorkflows` option if it exists. + return contentType?.options?.reviewWorkflows || false; +} // TODO To be refactored when multiple workflows are added const getDefaultWorkflow = async ({ strapi }) => strapi.query(WORKFLOW_MODEL_UID).findOne({ populate: ['stages'] }); module.exports = { - hasRWEnabled, + hasReviewWorkflow, getDefaultWorkflow, }; From 968078753361505ae796341707e261a224f7e5c5 Mon Sep 17 00:00:00 2001 From: Nathan Pichon Date: Mon, 27 Mar 2023 16:01:54 +0200 Subject: [PATCH 7/7] chore(review-workflows): update error message Co-authored-by: Jamie Howard <48524071+jhoward1994@users.noreply.github.com> --- .../core/admin/ee/server/controllers/workflows/stages/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/admin/ee/server/controllers/workflows/stages/index.js b/packages/core/admin/ee/server/controllers/workflows/stages/index.js index 2a274e37be..5a30dea3fc 100644 --- a/packages/core/admin/ee/server/controllers/workflows/stages/index.js +++ b/packages/core/admin/ee/server/controllers/workflows/stages/index.js @@ -80,7 +80,7 @@ module.exports = { const { id: stageId } = await validateUpdateStageOnEntity( ctx.request?.body?.data, - 'You shall pass an id to the body of the put request.' + 'You should pass an id to the body of the put request.' ); if (!hasReviewWorkflow({ strapi }, modelUID)) {