fix(ee): retrieve stage name and ID from updated entity

This commit is contained in:
Jamie Howard 2023-06-27 11:35:28 +01:00
parent 2ca802bcbd
commit 5985781a9a
2 changed files with 16 additions and 19 deletions

View File

@ -2,7 +2,7 @@
const { omit } = require('lodash/fp');
const { WORKFLOW_UPDATE_STAGE } = require('../../../constants/webhookEvents');
const { STAGE_MODEL_UID } = require('../../../constants/workflows');
const { ENTITY_STAGE_ATTRIBUTE } = require('../../../constants/workflows');
const { decorator } = require('../entity-service-decorator')();
jest.mock('../../../utils');
@ -109,22 +109,23 @@ describe('Entity service decorator', () => {
const stageToId = 3;
const defaultService = {
update: jest.fn(() => Promise.resolve({ id: entityId })),
update: jest.fn(() =>
Promise.resolve({
id: entityId,
[ENTITY_STAGE_ATTRIBUTE]: {
id: stageToId,
name: `Stage ${stageToId}`,
workflow: { id: workflowId },
},
})
),
};
const emit = jest.fn();
global.strapi = {
...global.strapi,
entityService: {
findOne: jest.fn((uid, id) => {
if (uid === STAGE_MODEL_UID) {
return {
id,
name: `Stage ${id}`,
workflow: { id: workflowId },
};
}
findOne: jest.fn(() => {
return {
strapi_reviewWorkflows_stage: {
id: stageFromId,

View File

@ -1,7 +1,7 @@
'use strict';
const { isNil, isNull } = require('lodash/fp');
const { ENTITY_STAGE_ATTRIBUTE, STAGE_MODEL_UID } = require('../../constants/workflows');
const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows');
const { WORKFLOW_UPDATE_STAGE } = require('../../constants/webhookEvents');
const { hasReviewWorkflow, getDefaultWorkflow } = require('../../utils/review-workflows');
@ -66,15 +66,11 @@ const decorator = (service) => ({
return service.update.call(this, uid, entityId, { ...opts, data });
}
const newStage = await strapi.entityService.findOne(
STAGE_MODEL_UID,
data[ENTITY_STAGE_ATTRIBUTE]
);
const previousStage = await getEntityStage(uid, entityId);
const updatedEntity = await service.update.call(this, uid, entityId, { ...opts, data });
if (previousStage?.id && previousStage.id !== newStage.id) {
if (previousStage?.id && previousStage.id !== updatedEntity[ENTITY_STAGE_ATTRIBUTE].id) {
const model = strapi.getModel(uid);
strapi.eventHub.emit(WORKFLOW_UPDATE_STAGE, {
@ -91,8 +87,8 @@ const decorator = (service) => ({
name: previousStage.name,
},
to: {
id: newStage.id,
name: newStage.name,
id: updatedEntity[ENTITY_STAGE_ATTRIBUTE].id,
name: updatedEntity[ENTITY_STAGE_ATTRIBUTE].name,
},
},
},