fix(review-workflows): remove workflow validation in stage service

This commit is contained in:
nathan-pichon 2023-07-04 17:08:49 +02:00
parent 0072262a91
commit 05a46a71f2
No known key found for this signature in database
3 changed files with 16 additions and 32 deletions

View File

@ -39,7 +39,7 @@ describeOnCondition(edition === 'EE')('Review workflows - Content Types', () =>
let strapi;
const createWorkflow = async (data) => {
const name = `${data.name}-${Math.random().toString(36)}`;
const name = `workflow-${Math.random().toString(36)}`;
return requests.admin.post('/admin/review-workflows/workflows?populate=*', {
body: { data: { ...baseWorkflow, name, ...data } },
});
@ -108,10 +108,13 @@ describeOnCondition(edition === 'EE')('Review workflows - Content Types', () =>
describe('Create workflow and assign content type', () => {
test('It should create a workflow and assign a content type', async () => {
const res = await createWorkflow({ contentTypes: [productUID] });
const res = await createWorkflow({ name: 'test-workflow', contentTypes: [productUID] });
expect(res.status).toBe(200);
expect(res.body.data).toMatchObject({ contentTypes: [productUID] });
expect(res.body.data).toMatchObject({
name: expect.any(String),
contentTypes: [productUID],
});
workflow1 = res.body.data;
});

View File

@ -4,7 +4,7 @@ const {
mapAsync,
errors: { ApplicationError, ValidationError },
} = require('@strapi/utils');
const { map, pipe, flatMap, groupBy, has, filter, get } = require('lodash/fp');
const { map } = require('lodash/fp');
const { STAGE_MODEL_UID, ENTITY_STAGE_ATTRIBUTE, ERRORS } = require('../../constants/workflows');
const { getService } = require('../../utils');
@ -31,19 +31,6 @@ module.exports = ({ strapi }) => {
async createMany(stagesList, { fields } = {}) {
const params = { select: fields ?? '*' };
const filterStagesWithoutWorkflow = has('workflowId');
const groupByWorkflow = get('workflowId');
const validateEachWorkflow = async (stages, workflowId) => {
await workflowsValidationService.validateWorkflowCountStages(workflowId, stages.length);
};
const validationPipe = pipe(
filter(filterStagesWithoutWorkflow),
groupBy(groupByWorkflow),
flatMap(validateEachWorkflow)
);
// As we can create several stages, we need to make sure that we don't exceed the licence threshold
await Promise.all(validationPipe(stagesList));
const stages = await Promise.all(
stagesList.map((stage) =>
@ -96,8 +83,6 @@ module.exports = ({ strapi }) => {
assertAtLeastOneStageRemain(srcStages || [], { created, deleted });
workflowsValidationService.validateWorkflowStages(destStages);
// Update stages and assign entity stages
return strapi.db.transaction(async ({ trx }) => {
// ⚠️ Delete first as we can have a limit on stages

View File

@ -1,6 +1,6 @@
'use strict';
const { set, isString, omit } = require('lodash/fp');
const { set, isString, map, get } = require('lodash/fp');
const { ApplicationError } = require('@strapi/utils').errors;
const { WORKFLOW_MODEL_UID } = require('../../../constants/workflows');
const { getService } = require('../../../utils');
@ -50,22 +50,17 @@ module.exports = ({ strapi }) => {
* @throws {ValidationError} - If the workflow has no stages.
*/
async create(opts) {
let createOpts = { ...opts, populate: { stages: true } };
workflowsValidationService.validateWorkflowStages(opts.data.stages);
await workflowsValidationService.validateWorkflowCount(1);
return strapi.db.transaction(async () => {
// Create Workflow
const workflow = await strapi.entityService.create(WORKFLOW_MODEL_UID, {
select: '*',
data: omit('stages')(opts.data),
});
const stagesToCreate = opts.data.stages.map((stage) => ({
...stage,
workflow: workflow.id,
}));
// Create stages
const stages = await getService('stages', { strapi }).createMany(stagesToCreate);
const stages = await getService('stages', { strapi }).createMany(opts.data.stages);
const mapIds = map(get('id'));
createOpts = set('data.stages', mapIds(stages), createOpts);
// Update (un)assigned Content Types
if (opts.data.contentTypes) {
@ -75,7 +70,8 @@ module.exports = ({ strapi }) => {
});
}
return { ...workflow, stages };
// Create Workflow
return strapi.entityService.create(WORKFLOW_MODEL_UID, createOpts);
});
},