From 52e493e3bb6c5c6e89d8d809b39ffc4b186c75e3 Mon Sep 17 00:00:00 2001 From: LionelB Date: Mon, 23 Nov 2020 13:32:59 +0100 Subject: [PATCH] fix: send diff payload --- targets/alert-cli/package.json | 1 + .../alert-cli/src/exportContributionAlerts.js | 33 ++++++++++++++++++- targets/alert-cli/src/index.d.ts | 4 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/targets/alert-cli/package.json b/targets/alert-cli/package.json index dd2edf097..30322e1c5 100644 --- a/targets/alert-cli/package.json +++ b/targets/alert-cli/package.json @@ -5,6 +5,7 @@ "@shared/graphql-client": "1.0.0", "@socialgouv/cdtn-slugify": "^4.35.0", "@socialgouv/cdtn-sources": "^4.35.0", + "@types/node-fetch": "^2.5.7", "node-fetch": "^2.6.1", "nodegit": "0.27.0", "semver": "^7.3.2", diff --git a/targets/alert-cli/src/exportContributionAlerts.js b/targets/alert-cli/src/exportContributionAlerts.js index 9a630cfff..957759f1e 100644 --- a/targets/alert-cli/src/exportContributionAlerts.js +++ b/targets/alert-cli/src/exportContributionAlerts.js @@ -24,7 +24,9 @@ export async function exportContributionAlerts(changes) { answer_id: contrib.id, cid: reference.dila_cid, id: reference.dila_id, - value: nodes.find((node) => node.data.cid === reference.dila_cid), + value: computeDiff( + nodes.find((node) => node.data.cid === reference.dila_cid) + ), version: alert.ref, })); }); @@ -37,3 +39,32 @@ export async function exportContributionAlerts(changes) { method: "POST", }); } + +/** + * + * @param {alerts.DilaNodeForDiff} node + */ +function computeDiff(node) { + const textFieldname = + node.context.containerId === "LEGITEXT000006072050" ? "texte" : "content"; + const content = node.data[textFieldname] || ""; + const previousContent = node.previous?.data[textFieldname] || ""; + const showDiff = content !== previousContent; + const showNotaDiff = node.previous.data.nota !== node.data.nota; + const texts = []; + if (showDiff) { + texts.push({ current: content, previous: previousContent }); + } + + if (showNotaDiff) { + texts.push({ + current: node.data.nota, + previous: node.previous.data.nota, + }); + } + + return { + etat: { current: node.data.etat, previous: node.previous.data.etat }, + texts, + }; +} diff --git a/targets/alert-cli/src/index.d.ts b/targets/alert-cli/src/index.d.ts index 420e1fa07..1e589bacf 100644 --- a/targets/alert-cli/src/index.d.ts +++ b/targets/alert-cli/src/index.d.ts @@ -120,6 +120,10 @@ type DilaNodeWithContext = DilaNode & { } } +type DilaNodeForDiff = DilaNodeWithContext & { + previous: DilaNodeWithContext +} + type FicheVddIndex = { id: string date: string