From 2b7f796b998b5e38c5c1559f7e473bb764defec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Fri, 9 Dec 2022 04:43:11 +0400 Subject: [PATCH] Add difference when updating transcription --- .../parole/content-types/parole/lifecycles.js | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/api/parole/content-types/parole/lifecycles.js b/src/api/parole/content-types/parole/lifecycles.js index f6a12db..f5c3335 100644 --- a/src/api/parole/content-types/parole/lifecycles.js +++ b/src/api/parole/content-types/parole/lifecycles.js @@ -114,7 +114,7 @@ module.exports = { strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription) const userAdmin = await jwennUserAdminEpiId(data?.createdBy) - + if (userAdmin) { data.userAdmin = userAdmin } @@ -131,8 +131,21 @@ module.exports = { } }, beforeUpdate: async event => { - let {data} = event.params + const {state} = event + let {data} = event.params const {where} = event.params + const {id} = where + + const previousParoles = await strapi.db.query('api::parole.parole').findOne({ + where: {id}, + populate: {difference: true} + }) + + if (data.transcription) { + const difference = strapi.service('api::parole.parole').parolesDiff(data.titre, previousParoles.transcription, data.transcription) + + state.diff = difference + } if(!data.publishedAt && data.titre && data.transcription) { strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription) @@ -184,6 +197,24 @@ module.exports = { } } }, + afterUpdate: async event => { + const {result, state} = event + + if (state.diff) { + await strapi.entityService.update('api::parole.parole', result.id, { + data: { + difference: [ + ...result.difference, + { + admin_user: result.updatedBy.id, + paroles: state.diff, + date: new Date(), + sources: 'transcription' + }] + } + }) + } + }, afterCreate: async event => { const {data} = event.params const user = await jwennUserEpiId(data?.user?.id) @@ -210,7 +241,7 @@ module.exports = { traductions } }) - } + } } if (user) {