Skip to content

Commit

Permalink
fix(alert): fix forward alert to contribution api (#214)
Browse files Browse the repository at this point in the history
* fix(alert): fix forward alert to contribution api

* tests: add exportContributionAlerts snapshot (#215)

* fix: change containerId text detection

* fix(alert): typo

Co-authored-by: Julien Bouquillon <julien.bouquillon@sg.social.gouv.fr>
  • Loading branch information
lionelB and Julien Bouquillon committed Dec 9, 2020
1 parent 19e68a0 commit 3aec2a4
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 18 deletions.
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`exportContributionAlerts should export changes to contributions API 1`] = `undefined`;
141 changes: 141 additions & 0 deletions targets/alert-cli/src/__test__/exportContributionAlerts.test.js
@@ -0,0 +1,141 @@
/* eslint-disable */
jest.mock("node-fetch");

import fetch from "node-fetch";

import {
contribApiUrl,
exportContributionAlerts,
} from "../exportContributionAlerts";

describe("exportContributionAlerts", () => {
it("should export changes to contributions API", async () => {
const changes = [
{
type: "dila",
added: [
{
data: {
cid: 42,
},
},
],
removed: [
{
data: {
cid: 55,
},
},
],
modified: [
{
context: {
containerId: "LEGITEXT000006072050",
},
previous: {
data: {
etat: "VIGUEUR",
texte: "old text",
nota: "nota 2",
},
},
data: {
etat: "NON VIGUEUR",
cid: 45,
texte: "new text",
nota: "nota 1",
},
},
],
documents: [
{
document: {
source: "contributions",
},
references: [
{
dila_cid: 42,
dila_id: 43,
},
{
dila_cid: 45,
dila_id: 46,
},
],
},
{
document: {
source: "not-contributions",
},
references: [
{
x: 2021,
y: 2022,
},
{
x: 2023,
y: 2024,
},
],
},
],
},
{
type: "not-dila",
documents: [
{
document: {
source: "contributions",
},
references: [
{
dila_cid: 123,
dila_id: 456,
},
{
dila_cid: 789,
dila_id: 987,
},
],
},
{
document: {
source: "not-contributions",
},
references: [
{
id: 99,
x: 66,
},
],
},
],
},
];

expect(await exportContributionAlerts(changes)).toMatchSnapshot();
fetch.mockReturnValue(Promise.resolve());
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(contribApiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify([
{ cid: 42, id: 43, value: { type: "added" } },
{
cid: 45,
id: 46,
value: {
etat: { current: "NON VIGUEUR", previous: "VIGUEUR" },
texts: [
{ current: "new text", previous: "old text" },
{ current: "nota 1", previous: "nota 2" },
],
type: "modified",
},
},
]),
});
});
});
52 changes: 36 additions & 16 deletions targets/alert-cli/src/exportContributionAlerts.js
@@ -1,6 +1,6 @@
import fetch from "node-fetch";

const contribApi =
export const contribApiUrl =
"https://contributions-api.codedutravail.fabrique.social.gouv.fr/alerts";

/**
Expand All @@ -18,20 +18,17 @@ export async function exportContributionAlerts(changes) {
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)
),
value: computeDiff(reference, alert),
version: alert.ref,
}));
});
});
await fetch(contribApi, {
await fetch(contribApiUrl, {
body: JSON.stringify(contributions),
headers: {
"Content-Type": "application/json",
Expand All @@ -42,30 +39,53 @@ export async function exportContributionAlerts(changes) {

/**
*
* @param {alerts.DilaNodeForDiff} node
* @param {import("@shared/types").ParseDilaReference} reference
* @param {alerts.DilaAlertChanges} nodes
*/
function computeDiff(node) {
const textFieldname =
node.context.containerId === "LEGITEXT000006072050" ? "texte" : "content";
const content = node.data[textFieldname] || "";
function computeDiff(reference, { added, removed, modified }) {
const addedNode = added.find((node) => node.data.cid === reference.dila_cid);
if (addedNode) {
return { type: "added" };
}
const removedNode = removed.find(
(node) => node.data.cid === reference.dila_cid
);
if (removedNode) {
return { type: "removed" };
}

const modifiedNode = modified.find(
(node) => node.data.cid === reference.dila_cid
);

const textFieldname = /^KALITEXT\d+$/.test(modifiedNode.context.containerId)
? "content"
: "texte";
const content = modifiedNode.data[textFieldname] || "";

const previousContent =
(node.previous && node.previous.data[textFieldname]) || "";
(modifiedNode.previous && modifiedNode.previous.data[textFieldname]) || "";
const showDiff = content !== previousContent;
const showNotaDiff = node.previous.data.nota !== node.data.nota;
const showNotaDiff =
modifiedNode.previous.data.nota !== modifiedNode.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,
current: modifiedNode.data.nota,
previous: modifiedNode.previous.data.nota,
});
}

return {
etat: { current: node.data.etat, previous: node.previous.data.etat },
etat: {
current: modifiedNode.data.etat,
previous: modifiedNode.previous.data.etat,
},
texts,
type: "modified",
};
}
5 changes: 3 additions & 2 deletions targets/frontend/src/components/changes/index.js
Expand Up @@ -80,8 +80,9 @@ let COLLAPSIBLE_ID = 1;

export function DilaDiffChange({ change }) {
const { data, previous } = change;
const textFieldname =
change.context.containerId === "LEGITEXT000006072050" ? "texte" : "content";
const textFieldname = /^KALITEXT\d+$/.test(change.context.containerId)
? "content"
: "texte";
const content = data[textFieldname] || "";
const previousContent = previous?.data[textFieldname] || "";
const showDiff = previous && content !== previousContent;
Expand Down

0 comments on commit 3aec2a4

Please sign in to comment.