mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
refactor(review-workflow): use services in rw bootstrap
This commit is contained in:
parent
ee2b6afc57
commit
4f756d166d
@ -1,54 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
const { WORKFLOW_MODEL_UID, STAGE_MODEL_UID } = require('../../constants/workflows');
|
||||
const { getService } = require('../../utils');
|
||||
|
||||
module.exports = ({ strapi }) => ({
|
||||
async bootstrap() {
|
||||
const wfCount = await strapi.entityService.count(WORKFLOW_MODEL_UID);
|
||||
const stagesCount = await strapi.entityService.count(STAGE_MODEL_UID);
|
||||
// Map every stage in the array to be ordered in the relation
|
||||
function buildStagesConnectArray(stages) {
|
||||
return stages.map((stage, index) => {
|
||||
const connect = {
|
||||
id: stage.id,
|
||||
position: {},
|
||||
};
|
||||
|
||||
if (wfCount + stagesCount === 0) {
|
||||
const defaultStages = require('../../constants/default-stages.json');
|
||||
const defaultWorkflow = require('../../constants/default-workflow.json');
|
||||
|
||||
await strapi.query('admin::workflow-stage').createMany({ data: defaultStages });
|
||||
|
||||
const stages = await strapi
|
||||
.query('admin::workflow-stage')
|
||||
.findMany({ limit: 4, select: ['id'] });
|
||||
|
||||
const workflow = {
|
||||
...defaultWorkflow,
|
||||
stages: {
|
||||
connect: [
|
||||
{
|
||||
id: stages[0].id,
|
||||
position: {
|
||||
start: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: stages[1].id,
|
||||
position: {
|
||||
after: stages[0].id,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: stages[2].id,
|
||||
position: {
|
||||
after: stages[1].id,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: stages[3].id,
|
||||
position: {
|
||||
after: stages[2].id,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
await strapi.query('admin::workflow').create({ data: workflow });
|
||||
if (index === 0) {
|
||||
connect.position.start = true;
|
||||
} else {
|
||||
connect.position.after = stages[index - 1].id;
|
||||
}
|
||||
},
|
||||
});
|
||||
return connect;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = ({ strapi }) => {
|
||||
const workflowsService = getService('workflows', { strapi });
|
||||
const stagesService = getService('stages', { strapi });
|
||||
|
||||
return {
|
||||
async bootstrap() {
|
||||
const wfCount = await workflowsService.count();
|
||||
const stagesCount = await stagesService.count();
|
||||
|
||||
// Check if there is nothing about review-workflow in DB
|
||||
// If any, the feature has already been initialized with a workflow and stages
|
||||
if (wfCount === 0 && stagesCount === 0) {
|
||||
const defaultStages = require('../../constants/default-stages.json');
|
||||
const defaultWorkflow = require('../../constants/default-workflow.json');
|
||||
|
||||
const stages = await stagesService.createMany(defaultStages, { fields: ['id'] });
|
||||
const workflow = {
|
||||
...defaultWorkflow,
|
||||
stages: {
|
||||
connect: buildStagesConnectArray(stages),
|
||||
},
|
||||
};
|
||||
|
||||
await workflowsService.create(workflow);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@ -18,4 +18,19 @@ module.exports = ({ strapi }) => ({
|
||||
};
|
||||
return strapi.entityService.findOne(STAGE_MODEL_UID, id, params);
|
||||
},
|
||||
|
||||
createMany(stagesList, { fields }) {
|
||||
const params = {
|
||||
select: fields,
|
||||
};
|
||||
return Promise.all(
|
||||
stagesList.map((stage) =>
|
||||
strapi.entityService.create(STAGE_MODEL_UID, { data: stage, ...params })
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
count() {
|
||||
return strapi.entityService.count(STAGE_MODEL_UID);
|
||||
},
|
||||
});
|
||||
|
||||
@ -10,4 +10,12 @@ module.exports = ({ strapi }) => ({
|
||||
findById(id, opts) {
|
||||
return strapi.entityService.findOne(WORKFLOW_MODEL_UID, id, opts);
|
||||
},
|
||||
|
||||
create(workflowData) {
|
||||
return strapi.entityService.create(WORKFLOW_MODEL_UID, { data: workflowData });
|
||||
},
|
||||
|
||||
count() {
|
||||
return strapi.entityService.count(WORKFLOW_MODEL_UID);
|
||||
},
|
||||
});
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const getService = (name) => {
|
||||
const getService = (name, { strapi } = { strapi: global.strapi }) => {
|
||||
return strapi.service(`admin::${name}`);
|
||||
};
|
||||
module.exports = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user