refactor(ee): improve how the default workflow is assigned

This commit is contained in:
Jamie Howard 2023-03-22 15:07:38 +00:00
parent d60e8097d9
commit 9a4341639c
3 changed files with 21 additions and 45 deletions

View File

@ -2,8 +2,6 @@
const { decorator } = require('../entity-service-decorator')();
const { getService } = require('../../../utils');
jest.mock('../../../utils');
const rwModel = {
@ -29,6 +27,12 @@ describe('Entity service decorator', () => {
getModel(uid) {
return models[uid || 'test-model'];
},
query: () => ({
findOne: () => ({
id: 1,
stages: [{ id: 1 }],
}),
}),
};
});
@ -51,9 +55,6 @@ describe('Entity service decorator', () => {
});
test('Assigns default stage to new review workflow entity', async () => {
const assignSpy = jest.fn();
getService.mockImplementation(() => ({ assignEntityDefaultStage: assignSpy }));
const entry = {
id: 1,
};
@ -67,8 +68,13 @@ describe('Entity service decorator', () => {
const input = { data: { title: 'title ' } };
await service.create('test-model', input);
expect(defaultService.create).toHaveBeenCalledWith('test-model', input);
expect(assignSpy).toHaveBeenCalledWith('test-model', expect.anything());
expect(defaultService.create).toHaveBeenCalledWith('test-model', {
...input,
data: {
...input.data,
strapi_reviewWorkflows_stage: 1,
},
});
});
});
});

View File

@ -1,7 +1,6 @@
'use strict';
const { hasRWEnabled } = require('../../utils/review-workflows');
const { getService } = require('../../utils');
const { hasRWEnabled, getDefaultWorkflow } = require('../../utils/review-workflows');
/**
* Decorates the entity service with RW business logic
@ -12,16 +11,17 @@ const decorator = (service) => ({
const model = strapi.getModel(uid);
const hasRW = hasRWEnabled(model);
const entity = await service.create.call(this, uid, opts);
if (!hasRW) {
return entity;
return service.create.call(this, uid, opts);
}
// Assign this entity to the default workflow stage
const { assignEntityDefaultStage } = getService('review-workflows');
await assignEntityDefaultStage(uid, entity.id);
const defaultWorkFlow = await getDefaultWorkflow({ strapi });
return entity;
return service.create.call(this, uid, {
...opts,
// Assign this entity to the default workflow stage
data: { ...opts.data, strapi_reviewWorkflows_stage: defaultWorkFlow.stages[0].id },
});
},
});

View File

@ -157,41 +157,11 @@ function enableReviewWorkflow({ strapi }) {
};
}
/**
* Assigns an entity to the first stage of the default workflow.
* @param {string} uid of the model
* @param {number} entityID
* @returns
*/
async function assignEntityDefaultStage(uid, entityID) {
const defaultWorkflow = await getDefaultWorkflow({ strapi });
if (!defaultWorkflow) {
return;
}
const firstStage = defaultWorkflow.stages[0];
const contentTypeMetadata = strapi.db.metadata.get(uid);
const { target, morphBy } = contentTypeMetadata.attributes[ENTITY_STAGE_ATTRIBUTE];
const { joinTable } = strapi.db.metadata.get(target).attributes[morphBy];
const { idColumn, typeColumn } = joinTable.morphColumn;
const connection = strapi.db.getConnection();
await connection(joinTable.name).insert({
[idColumn.name]: entityID,
field: connection.raw('?', [ENTITY_STAGE_ATTRIBUTE]),
order: 1,
[joinTable.joinColumn.name]: firstStage.id,
[typeColumn.name]: connection.raw('?', [uid]),
});
}
module.exports = ({ strapi }) => {
const workflowsService = getService('workflows', { strapi });
const stagesService = getService('stages', { strapi });
return {
assignEntityDefaultStage,
async bootstrap() {
await initDefaultWorkflow({ workflowsService, stagesService, strapi });
},