diff --git a/targets/alert-cli/package.json b/targets/alert-cli/package.json index d9f3b9301..30322e1c5 100644 --- a/targets/alert-cli/package.json +++ b/targets/alert-cli/package.json @@ -5,6 +5,8 @@ "@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", "unist-util-parents": "^1.0.3", @@ -15,11 +17,12 @@ "@babel/preset-env": "^7.12.1", "@shared/types": "1.0.0", "@socialgouv/contributions-data-types": "^3.0.0", + "@socialgouv/datafiller-data-types": "^2.6.0", "@socialgouv/eslint-config-recommended": "^1.46.0", "@socialgouv/fiches-travail-data-types": "^4.6.0", - "@socialgouv/datafiller-data-types": "^2.6.0", "@types/jest": "^26.0.15", "@types/node": "^14.14.6", + "@types/node-fetch": "^2.5.7", "@types/nodegit": "^0.26.12", "@types/semver": "^7.3.4", "@types/unist": "^2.0.3", diff --git a/targets/alert-cli/src/__test__/relevantContent.test.js b/targets/alert-cli/src/__test__/relevantContent.test.js index 9d30c28ba..e18a1a8fe 100644 --- a/targets/alert-cli/src/__test__/relevantContent.test.js +++ b/targets/alert-cli/src/__test__/relevantContent.test.js @@ -78,14 +78,16 @@ describe("getRelevantContent", () => { source: "contributions", title: "question1", }, - reference: { - category: "agreement", - dila_cid: "c123", - dila_container_id: "kalicont123", - dila_id: "125", - title: "accord c123", - url: "url/c123", - }, + references: [ + { + category: "agreement", + dila_cid: "c123", + dila_container_id: "kalicont123", + dila_id: "125", + title: "accord c123", + url: "url/c123", + }, + ], }, ]; expect(await getRelevantDocuments(changes)).toEqual(expected); @@ -103,14 +105,16 @@ describe("getRelevantContent", () => { source: "contributions", title: "question1", }, - reference: { - category: "agreement", - dila_cid: "3", - dila_container_id: "kalicont42", - dila_id: "3", - title: "Accord du 3 novembre", - url: "legifrance.url/kalicont42", - }, + references: [ + { + category: "agreement", + dila_cid: "3", + dila_container_id: "kalicont42", + dila_id: "3", + title: "Accord du 3 novembre", + url: "legifrance.url/kalicont42", + }, + ], }, ]; expect(await getRelevantDocuments(changes)).toEqual(expected); diff --git a/targets/alert-cli/src/exportContributionAlerts.js b/targets/alert-cli/src/exportContributionAlerts.js new file mode 100644 index 000000000..957759f1e --- /dev/null +++ b/targets/alert-cli/src/exportContributionAlerts.js @@ -0,0 +1,70 @@ +import fetch from "node-fetch"; + +const contribApi = + "https://contributions-api.codedutravail.fabrique.social.gouv.fr/alerts"; + +/** + * + * @param {alerts.AlertChanges[]} changes + */ +export async function exportContributionAlerts(changes) { + const dilaAlertChanges = /** @type {alerts.DilaAlertChanges[]} */ (changes.filter( + (change) => change.type === "dila" + )); + const contributions = dilaAlertChanges.flatMap((alert) => { + const targetedContribs = alert.documents.filter( + (targetDoc) => targetDoc.document.source == "contributions" + ); + if (targetedContribs.length === 0) { + return []; + } + const nodes = [...alert.modified, ...alert.removed, ...alert.added]; + return targetedContribs.flatMap(({ references, document: contrib }) => { + return references.map((reference) => ({ + answer_id: contrib.id, + cid: reference.dila_cid, + id: reference.dila_id, + value: computeDiff( + nodes.find((node) => node.data.cid === reference.dila_cid) + ), + version: alert.ref, + })); + }); + }); + await fetch(contribApi, { + body: JSON.stringify(contributions), + headers: { + "Content-Type": "application/json", + }, + 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 2e2a4ed12..1e589bacf 100644 --- a/targets/alert-cli/src/index.d.ts +++ b/targets/alert-cli/src/index.d.ts @@ -28,7 +28,7 @@ type AstChanges = { } type Changes = AstChanges & { - documents: { document: DocumentInfo, reference: ParseDilaReference }[] + documents: { document: DocumentInfo, references: ParseDilaReference[] }[] } type DilaAlertChanges = { @@ -120,6 +120,10 @@ type DilaNodeWithContext = DilaNode & { } } +type DilaNodeForDiff = DilaNodeWithContext & { + previous: DilaNodeWithContext +} + type FicheVddIndex = { id: string date: string diff --git a/targets/alert-cli/src/index.js b/targets/alert-cli/src/index.js index af1b435e0..a681668c6 100644 --- a/targets/alert-cli/src/index.js +++ b/targets/alert-cli/src/index.js @@ -7,6 +7,7 @@ import { ccns } from "./ccn-list.js"; import { compareArticles } from "./compareTree.js"; import { processTravailDataDiff } from "./diff/fiches-travail-data"; import { processVddDiff } from "./diff/fiches-vdd"; +import { exportContributionAlerts } from "./exportContributionAlerts"; import { getFicheServicePublicIds } from "./getFicheServicePublicIds"; import { createToJson, getFilename } from "./node-git.helpers"; import { openRepo } from "./openRepo"; @@ -356,6 +357,9 @@ async function main() { if (result.changes.length === 0) { console.log(`no update for ${result.repository}`); } else { + // forward alert to contributions + exportContributionAlerts(result.changes); + const inserts = await batchPromises( result.changes, (diff) => insertAlert(result.repository, diff), @@ -380,8 +384,8 @@ async function main() { ); process.exit(-1); } - const update = await updateSource(result.repository, result.newRef); - console.log(`update source ${update.repository} to ${update.tag}`); + // const update = await updateSource(result.repository, result.newRef); + // console.log(`update source ${update.repository} to ${update.tag}`); } } } diff --git a/targets/alert-cli/src/relevantContent.js b/targets/alert-cli/src/relevantContent.js index 752c3da09..fb54a9015 100644 --- a/targets/alert-cli/src/relevantContent.js +++ b/targets/alert-cli/src/relevantContent.js @@ -10,7 +10,7 @@ export async function getRelevantDocuments({ added, modified, removed }) { const references = contribReferences.concat(travailEmploiReferences); const documents = references.flatMap((item) => { - const reference = item.references.find( + const references = item.references.filter( (ref) => modified.find( (node) => @@ -25,8 +25,8 @@ export async function getRelevantDocuments({ added, modified, removed }) { added.find((node) => node.data.cid === ref.dila_cid) ); - if (reference) { - return { document: item.document, reference }; + if (references.length) { + return { document: item.document, references }; } return []; }); diff --git a/yarn.lock b/yarn.lock index fef2dc78e..18d3542db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3302,6 +3302,14 @@ resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6" integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY= +"@types/node-fetch@^2.5.7": + version "2.5.7" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.7.tgz#20a2afffa882ab04d44ca786449a276f9f6bbf3c" + integrity sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw== + dependencies: + "@types/node" "*" + form-data "^3.0.0" + "@types/node@*", "@types/node@>= 8", "@types/node@^14.14.6": version "14.14.6" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f" @@ -5084,7 +5092,7 @@ columnify@^1.5.4: strip-ansi "^3.0.0" wcwidth "^1.0.0" -combined-stream@^1.0.6, combined-stream@~1.0.6: +combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== @@ -7306,6 +7314,15 @@ forever-agent@~0.6.1: resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= +form-data@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682" + integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + form-data@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"