fix: create more than one stage at a time

This commit is contained in:
Marc-Roig 2023-04-14 14:32:28 +02:00
parent 29b2032aa1
commit cd5c508cd5

View File

@ -54,17 +54,16 @@ module.exports = ({ strapi }) => {
return strapi.entityService.count(STAGE_MODEL_UID); return strapi.entityService.count(STAGE_MODEL_UID);
}, },
async replaceWorkflowStages(workflowId, stages) { async replaceWorkflowStages(workflowId, newStages) {
const workflow = await workflowsService.findById(workflowId, { populate: ['stages'] }); const workflow = await workflowsService.findById(workflowId, { populate: ['stages'] });
const { created, updated, deleted } = getDiffBetweenStages(workflow.stages, stages); const { created, updated, deleted } = getDiffBetweenStages(workflow.stages, newStages);
assertAtLeastOneStageRemain(workflow.stages, { created, deleted }); assertAtLeastOneStageRemain(workflow.stages, { created, deleted });
return strapi.db.transaction(async () => { return strapi.db.transaction(async () => {
const defaultWorkflow = await getDefaultWorkflow({ strapi }); const createdStages = await this.createMany(created, { fields: ['id'] });
const newStages = await this.createMany(created, { fields: ['id'] }); const stages = newStages.map((stage) => (stage.id ? stage : createdStages.shift()));
const stagesIds = stages.map((stage) => stage.id ?? [...newStages].shift().id);
const contentTypes = getContentTypeUIDsWithActivatedReviewWorkflows(strapi.contentTypes); const contentTypes = getContentTypeUIDsWithActivatedReviewWorkflows(strapi.contentTypes);
await mapAsync(updated, (stage) => this.update(stage.id, stage)); await mapAsync(updated, (stage) => this.update(stage.id, stage));
@ -73,8 +72,8 @@ module.exports = ({ strapi }) => {
// Find the nearest stage in the workflow and newly created stages // Find the nearest stage in the workflow and newly created stages
// that is not deleted, prioritizing the previous stages // that is not deleted, prioritizing the previous stages
const nearestStage = findNearestMatchingStage( const nearestStage = findNearestMatchingStage(
[...defaultWorkflow.stages, ...newStages], stages,
defaultWorkflow.stages.findIndex((s) => s.id === stage.id), stages.findIndex((s) => s.id === stage.id),
(targetStage) => { (targetStage) => {
return !deleted.find((s) => s.id === targetStage.id); return !deleted.find((s) => s.id === targetStage.id);
} }
@ -92,7 +91,7 @@ module.exports = ({ strapi }) => {
}); });
return workflowsService.update(workflowId, { return workflowsService.update(workflowId, {
stages: stagesIds, stages: stages.map((stage) => stage.id),
}); });
}); });
}, },