From c53b7a7ae4999a6afae158d3b68b0633efc3873f Mon Sep 17 00:00:00 2001 From: Jamie Howard Date: Thu, 1 Jun 2023 15:20:13 +0100 Subject: [PATCH] fix: simplify emitEvent logic --- .../server/services/entity-manager.js | 15 +++++------- .../lib/services/entity-service/index.js | 23 ++++++++----------- .../core/upload/server/services/upload.js | 18 +++++++-------- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/core/content-manager/server/services/entity-manager.js b/packages/core/content-manager/server/services/entity-manager.js index 8886cba549..87224dbb14 100644 --- a/packages/core/content-manager/server/services/entity-manager.js +++ b/packages/core/content-manager/server/services/entity-manager.js @@ -7,19 +7,16 @@ const { ApplicationError } = require('@strapi/utils').errors; const { getDeepPopulate, getDeepPopulateDraftCount } = require('./utils/populate'); const { getDeepRelationsCount } = require('./utils/count'); const { sumDraftCounts } = require('./utils/draft'); -const { ALLOWED_WEBHOOK_EVENTS } = require('../constants'); +const { + ALLOWED_WEBHOOK_EVENTS: { ENTRY_PUBLISH, ENTRY_UNPUBLISH }, +} = require('../constants'); const { hasDraftAndPublish } = strapiUtils.contentTypes; const { PUBLISHED_AT_ATTRIBUTE, CREATED_BY_ATTRIBUTE } = strapiUtils.contentTypes.constants; const omitPublishedAtField = omit(PUBLISHED_AT_ATTRIBUTE); -const emitEvent = async (uid, eventName, entity) => { - const event = ALLOWED_WEBHOOK_EVENTS[eventName]; - if (!event) { - throw new ApplicationError(`The webhook event ${eventName} doesn't exist.`); - } - +const emitEvent = async (uid, event, entity) => { const modelDef = strapi.getModel(uid); const sanitizedEntity = await strapiUtils.sanitize.sanitizers.defaultSanitizeOutput( modelDef, @@ -259,7 +256,7 @@ module.exports = ({ strapi }) => ({ const updatedEntity = await strapi.entityService.update(uid, entity.id, params); - await emitEvent(uid, 'ENTRY_PUBLISH', updatedEntity); + await emitEvent(uid, ENTRY_PUBLISH, updatedEntity); const mappedEntity = await this.mapEntity(updatedEntity, uid); @@ -288,7 +285,7 @@ module.exports = ({ strapi }) => ({ const updatedEntity = await strapi.entityService.update(uid, entity.id, params); - await emitEvent(uid, 'ENTRY_UNPUBLISH', updatedEntity); + await emitEvent(uid, ENTRY_UNPUBLISH, updatedEntity); const mappedEntity = await this.mapEntity(updatedEntity, uid); diff --git a/packages/core/strapi/lib/services/entity-service/index.js b/packages/core/strapi/lib/services/entity-service/index.js index 809431eed4..a12e991b4e 100644 --- a/packages/core/strapi/lib/services/entity-service/index.js +++ b/packages/core/strapi/lib/services/entity-service/index.js @@ -5,7 +5,7 @@ const delegate = require('delegates'); const { InvalidTimeError, InvalidDateError, InvalidDateTimeError, InvalidRelationError } = require('@strapi/database').errors; const { contentTypes: contentTypesUtils, sanitize } = require('@strapi/utils'); -const { ValidationError, ApplicationError } = require('@strapi/utils').errors; +const { ValidationError } = require('@strapi/utils').errors; const { isAnyToMany } = require('@strapi/utils').relations; const { transformParamsToQuery } = require('@strapi/utils').convertQueryParams; const uploadFiles = require('../utils/upload-files'); @@ -62,12 +62,7 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator }) return result; }, - async emitEvent(uid, eventName, entity) { - const event = ALLOWED_WEBHOOK_EVENTS[eventName]; - if (!event) { - throw new ApplicationError(`The webhook event ${eventName} is not supported.`); - } - + async emitEvent(uid, event, entity) { // Ignore audit log events to prevent infinite loops if (uid === 'admin::audit-log') { return; @@ -184,7 +179,8 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator }) entity = await this.wrapResult(entity, { uid, action: 'create' }); - await this.emitEvent(uid, 'ENTRY_CREATE', entity); + const { ENTRY_CREATE } = ALLOWED_WEBHOOK_EVENTS; + await this.emitEvent(uid, ENTRY_CREATE, entity); return entity; }, @@ -237,7 +233,8 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator }) entity = await this.wrapResult(entity, { uid, action: 'update' }); - await this.emitEvent(uid, 'ENTRY_UPDATE', entity); + const { ENTRY_UPDATE } = ALLOWED_WEBHOOK_EVENTS; + await this.emitEvent(uid, ENTRY_UPDATE, entity); return entity; }, @@ -264,7 +261,8 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator }) entityToDelete = await this.wrapResult(entityToDelete, { uid, action: 'delete' }); - await this.emitEvent(uid, 'ENTRY_DELETE', entityToDelete); + const { ENTRY_DELETE } = ALLOWED_WEBHOOK_EVENTS; + await this.emitEvent(uid, ENTRY_DELETE, entityToDelete); return entityToDelete; }, @@ -294,9 +292,8 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator }) entitiesToDelete = await this.wrapResult(entitiesToDelete, { uid, action: 'delete' }); // Trigger webhooks. One for each entity - await Promise.all( - entitiesToDelete.map((entity) => this.emitEvent(uid, 'ENTRY_DELETE', entity)) - ); + const { ENTRY_DELETE } = ALLOWED_WEBHOOK_EVENTS; + await Promise.all(entitiesToDelete.map((entity) => this.emitEvent(uid, ENTRY_DELETE, entity))); return deletedEntities; }, diff --git a/packages/core/upload/server/services/upload.js b/packages/core/upload/server/services/upload.js index 3118f3484c..5ef37c89aa 100644 --- a/packages/core/upload/server/services/upload.js +++ b/packages/core/upload/server/services/upload.js @@ -21,7 +21,10 @@ const { file: { bytesToKbytes }, } = require('@strapi/utils'); -const { FILE_MODEL_UID, ALLOWED_WEBHOOK_EVENTS } = require('../constants'); +const { + FILE_MODEL_UID, + ALLOWED_WEBHOOK_EVENTS: { MEDIA_CREATE, MEDIA_UPDATE, MEDIA_DELETE }, +} = require('../constants'); const { getService } = require('../utils'); const { UPDATED_BY_ATTRIBUTE, CREATED_BY_ATTRIBUTE } = contentTypesUtils.constants; @@ -59,12 +62,7 @@ const createAndAssignTmpWorkingDirectoryToFiles = async (files) => { }; module.exports = ({ strapi }) => ({ - async emitEvent(eventName, data) { - const event = ALLOWED_WEBHOOK_EVENTS[eventName]; - if (!event) { - throw new ApplicationError(`The webhook event ${eventName} doesn't exist.`); - } - + async emitEvent(event, data) { const modelDef = strapi.getModel(FILE_MODEL_UID); const sanitizedData = await sanitize.sanitizers.defaultSanitizeOutput(modelDef, data); @@ -349,7 +347,7 @@ module.exports = ({ strapi }) => ({ const res = await strapi.entityService.update(FILE_MODEL_UID, id, { data: fileValues }); - await this.emitEvent('MEDIA_UPDATE', res); + await this.emitEvent(MEDIA_UPDATE, res); return res; }, @@ -365,7 +363,7 @@ module.exports = ({ strapi }) => ({ const res = await strapi.query(FILE_MODEL_UID).create({ data: fileValues }); - await this.emitEvent('MEDIA_CREATE', res); + await this.emitEvent(MEDIA_CREATE, res); return res; }, @@ -402,7 +400,7 @@ module.exports = ({ strapi }) => ({ where: { id: file.id }, }); - await this.emitEvent('MEDIA_DELETE', media); + await this.emitEvent(MEDIA_DELETE, media); return strapi.query(FILE_MODEL_UID).delete({ where: { id: file.id } }); },