mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
refactor(ee): improve how the default workflow is assigned
This commit is contained in:
parent
d60e8097d9
commit
9a4341639c
@ -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,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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 },
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -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 });
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user