feat: upsert stages when creating a workflow

This commit is contained in:
Marc-Roig 2023-05-12 15:31:50 +02:00
parent e8dd199183
commit aa67d1fba2
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249

View File

@ -1,6 +1,8 @@
'use strict';
const { set } = require('lodash/fp');
const { WORKFLOW_MODEL_UID } = require('../../constants/workflows');
const { getService } = require('../../utils');
module.exports = ({ strapi }) => ({
find(opts) {
@ -11,8 +13,19 @@ module.exports = ({ strapi }) => ({
return strapi.entityService.findOne(WORKFLOW_MODEL_UID, id, opts);
},
create(workflowData) {
return strapi.entityService.create(WORKFLOW_MODEL_UID, { data: workflowData });
async create(opts) {
let createOpts = opts;
// Create stages if provided
if (opts.data.stages) {
const stageIds = await getService('stages', { strapi })
.replaceStages([], opts.data.stages)
.then((stages) => stages.map((stage) => stage.id));
createOpts = set('data.stages', stageIds, opts);
}
return strapi.entityService.create(WORKFLOW_MODEL_UID, createOpts);
},
count() {