mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
fix: simplify emitEvent logic
This commit is contained in:
parent
9ecb8fe34c
commit
c53b7a7ae4
@ -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);
|
||||
|
||||
|
||||
@ -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;
|
||||
},
|
||||
|
||||
@ -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 } });
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user