feature(ee): emit workflow stage change webhook event

This commit is contained in:
Jamie Howard 2023-06-08 12:39:52 +01:00
parent 7ec5466c22
commit 0f15960636
3 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = {
WORKFLOW_UPDATE_STAGE: 'workflow.updateEntryStage',
};

View File

@ -2,6 +2,7 @@
const { isNil, isNull } = require('lodash/fp');
const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows');
const { WORKFLOW_UPDATE_STAGE } = require('../../constants/webhookEvents');
const { hasReviewWorkflow, getDefaultWorkflow } = require('../../utils/review-workflows');
/**
@ -45,7 +46,18 @@ const decorator = (service) => ({
delete data[ENTITY_STAGE_ATTRIBUTE];
}
return service.update.call(this, uid, entityId, { ...opts, data });
const entity = await service.findOne.call(this, uid, entityId, {
populate: [ENTITY_STAGE_ATTRIBUTE],
});
const previousStageId = entity?.[ENTITY_STAGE_ATTRIBUTE]?.id ?? null;
const updatedEntity = await service.update.call(this, uid, entityId, { ...opts, data });
if (previousStageId && previousStageId !== data[ENTITY_STAGE_ATTRIBUTE]) {
await service.emitEvent.call(this, uid, WORKFLOW_UPDATE_STAGE, updatedEntity);
}
return updatedEntity;
},
});

View File

@ -11,6 +11,7 @@ const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows');
const { getDefaultWorkflow } = require('../../utils/review-workflows');
const { persistTables, removePersistedTablesWithSuffix } = require('../../utils/persisted-tables');
const webhookEvents = require('../../constants/webhookEvents');
async function initDefaultWorkflow({ workflowsService, stagesService, strapi }) {
const wfCount = await workflowsService.count();
@ -109,7 +110,9 @@ function persistStagesJoinTables({ strapi }) {
}
const registerWebhookEvents = async ({ strapi }) =>
strapi.webhookStore.addAllowedEvent('WORKFLOW_UPDATE_STAGE', 'workflow.updateEntryStage');
Object.entries(webhookEvents).forEach(([eventKey, event]) =>
strapi.webhookStore.addAllowedEvent(eventKey, event)
);
module.exports = ({ strapi }) => {
const workflowsService = getService('workflows', { strapi });