Skip to content

Commit fb03f11

Browse files
authored
fix: put beforeX hooks actually before the event that's about to happen. (#196)
1 parent 11643ee commit fb03f11

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

server/services/publication-service.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ export default ({ strapi }) => ({
1010
*/
1111
async publish(uid, entityId, { locale }) {
1212
try {
13+
const { hooks } = getPluginService('settingsService').get();
14+
15+
await hooks.beforePublish({ strapi, uid, entity: publishedEntity });
16+
1317
const publishedEntity = await strapi.documents(uid).publish({
1418
documentId: entityId,
1519
locale,
1620
});
17-
strapi.log.info(logMessage(`Successfully published document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
18-
const { hooks } = getPluginService('settingsService').get();
19-
// emit publish event
20-
await hooks.beforePublish({ strapi, uid, entity: publishedEntity });
21+
2122
await getPluginService('emitService').publish(uid, publishedEntity);
23+
24+
strapi.log.info(logMessage(`Successfully published document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
25+
2226
await hooks.afterPublish({ strapi, uid, entity: publishedEntity });
2327
} catch (error) {
2428
strapi.log.error(logMessage(`An error occurred when trying to publish document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}": "${error}"`));
@@ -30,15 +34,19 @@ export default ({ strapi }) => ({
3034
*/
3135
async unpublish(uid, entityId, { locale }) {
3236
try {
37+
const { hooks } = getPluginService('settingsService').get();
38+
39+
await hooks.beforeUnpublish({ strapi, uid, entity: unpublishedEntity });
40+
3341
const unpublishedEntity = await strapi.documents(uid).unpublish({
3442
documentId: entityId,
3543
locale,
3644
});
37-
strapi.log.info(logMessage(`Successfully unpublished document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
38-
const { hooks } = getPluginService('settingsService').get();
39-
// Emit events
40-
await hooks.beforeUnpublish({ strapi, uid, entity: unpublishedEntity });
45+
4146
await getPluginService('emitService').unpublish(uid, unpublishedEntity);
47+
48+
strapi.log.info(logMessage(`Successfully unpublished document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
49+
4250
await hooks.afterUnpublish({ strapi, uid, entity: unpublishedEntity });
4351
} catch (error) {
4452
strapi.log.error(logMessage(`An error occurred when trying to unpublish document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}": "${error}"`));

0 commit comments

Comments
 (0)