fix(review-workflows): workflow creation

Workflows should be created before as stages needs to have a workflow ID to be created and validated
This commit is contained in:
nathan-pichon 2023-07-03 11:52:32 +02:00
parent 30ff81ecbc
commit 9c70fac0ac
No known key found for this signature in database

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { set, isString } = require('lodash/fp'); const { set, isString, omit } = require('lodash/fp');
const { ApplicationError } = require('@strapi/utils').errors; const { ApplicationError } = require('@strapi/utils').errors;
const { WORKFLOW_MODEL_UID } = require('../../../constants/workflows'); const { WORKFLOW_MODEL_UID } = require('../../../constants/workflows');
const { getService } = require('../../../utils'); const { getService } = require('../../../utils');
@ -50,29 +50,32 @@ module.exports = ({ strapi }) => {
* @throws {ValidationError} - If the workflow has no stages. * @throws {ValidationError} - If the workflow has no stages.
*/ */
async create(opts) { async create(opts) {
let createOpts = { ...opts, populate: { stages: true } };
workflowsValidationService.validateWorkflowStages(opts.data.stages); workflowsValidationService.validateWorkflowStages(opts.data.stages);
await workflowsValidationService.validateWorkflowCount(1); await workflowsValidationService.validateWorkflowCount(1);
return strapi.db.transaction(async () => { return strapi.db.transaction(async () => {
// Create stages // Create Workflow
const stageIds = await getService('stages', { strapi }) const workflow = await strapi.entityService.create(WORKFLOW_MODEL_UID, {
.replaceStages([], opts.data.stages) select: '*',
.then((stages) => stages.map((stage) => stage.id)); data: omit('stages')(opts.data),
});
createOpts = set('data.stages', stageIds, createOpts); const stagesToCreate = opts.data.stages.map((stage) => ({
...stage,
workflow: workflow.id,
}));
// Create stages
const stages = await getService('stages', { strapi }).createMany(stagesToCreate);
// Update (un)assigned Content Types // Update (un)assigned Content Types
if (opts.data.contentTypes) { if (opts.data.contentTypes) {
await workflowsContentTypes.migrate({ await workflowsContentTypes.migrate({
destContentTypes: opts.data.contentTypes, destContentTypes: opts.data.contentTypes,
stageId: stageIds[0], stageId: stages[0].id,
}); });
} }
// Create Workflow return { ...workflow, stages };
return strapi.entityService.create(WORKFLOW_MODEL_UID, createOpts);
}); });
}, },