Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(alerts): fix format of alert sent to contribution api #267

Merged
merged 2 commits into from Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

This file was deleted.

82 changes: 40 additions & 42 deletions targets/alert-cli/src/__test__/exportContributionAlerts.test.js
@@ -1,61 +1,32 @@
/* eslint-disable */
jest.mock("node-fetch");

import fetch from "node-fetch";

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

jest.mock("node-fetch");

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: 55,
},
{
dila_cid: 45,
Expand All @@ -79,9 +50,36 @@ describe("exportContributionAlerts", () => {
],
},
],
modified: [
{
context: {
containerId: "LEGITEXT000006072050",
},
data: {
cid: 45,
etat: "NON VIGUEUR",
nota: "nota 1",
texte: "new text",
},
previous: {
data: {
etat: "VIGUEUR",
nota: "nota 2",
texte: "old text",
},
},
},
],
removed: [
{
data: {
cid: 55,
},
},
],
type: "dila",
},
{
type: "not-dila",
documents: [
{
document: {
Expand Down Expand Up @@ -110,32 +108,32 @@ describe("exportContributionAlerts", () => {
],
},
],
type: "vdd",
},
];

expect(await exportContributionAlerts(changes)).toMatchSnapshot();
await exportContributionAlerts(changes);
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,
dila_cid: 45,
dila_id: 46,
value: {
etat: { current: "NON VIGUEUR", previous: "VIGUEUR" },
texts: [
{ current: "new text", previous: "old text" },
{ current: "nota 1", previous: "nota 2" },
],
type: "modified",
},
},
]),
headers: {
"Content-Type": "application/json",
Prefer: "merge-duplicates",
},
method: "POST",
});
});
});
53 changes: 25 additions & 28 deletions targets/alert-cli/src/exportContributionAlerts.js
Expand Up @@ -5,33 +5,44 @@ export const contribApiUrl =

/**
*
* @param {alerts.AlertChanges[]} changes
* @param {Pick<alerts.AlertChanges, "type" | "modified" | "added" | "removed">[]} 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"
({ document }) => document.source == "contributions"
);
if (targetedContribs.length === 0) {
return [];
}
return targetedContribs.flatMap(({ references, document: contrib }) => {
return references.map((reference) => ({
answer_id: contrib.id,
cid: reference.dila_cid,
id: reference.dila_id,
value: computeDiff(reference, alert),
version: alert.ref,
}));
return references.flatMap((reference) => {
const modifiedNode = alert.modified.find(
({ data: { cid } }) => reference.dila_cid === cid
);
if (!modifiedNode) {
return [];
}
return {
answer_id: contrib.id,
dila_cid: reference.dila_cid,
dila_container_id: reference.dila_container_id,
dila_id: reference.dila_id,
value: computeDiff(reference, modifiedNode),
version: alert.ref,
};
});
});
});
console.log(`Sending ${contributions} contrib alert(s) to contribution api`);
await fetch(contribApiUrl, {
body: JSON.stringify(contributions),
headers: {
"Content-Type": "application/json",
Prefer: "merge-duplicates",
},
method: "POST",
});
Expand All @@ -40,25 +51,12 @@ export async function exportContributionAlerts(changes) {
/**
*
* @param {import("@shared/types").ParseDilaReference} reference
* @param {alerts.DilaAlertChanges} nodes
* @param {alerts.DilaNodeForDiff} modifiedNode
*/
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)
function computeDiff(reference, modifiedNode) {
const textFieldname = /^KALITEXT\d+$/.test(
modifiedNode.context.containerId || ""
)
? "content"
: "texte";
const content = modifiedNode.data[textFieldname] || "";
Expand Down Expand Up @@ -86,6 +84,5 @@ function computeDiff(reference, { added, removed, modified }) {
previous: modifiedNode.previous.data.etat,
},
texts,
type: "modified",
};
}
7 changes: 2 additions & 5 deletions targets/alert-cli/src/relevantContent.js
Expand Up @@ -4,7 +4,7 @@ import getTravailEmploiReferences from "./extractDilaReferences/ficheTravailEmpl
/**
* @param {alerts.AstChanges} changes
*/
export async function getRelevantDocuments({ added, modified, removed }) {
export async function getRelevantDocuments({ modified, removed }) {
const contribReferences = await getContribReferences();
const travailEmploiReferences = await getTravailEmploiReferences();

Expand All @@ -19,10 +19,7 @@ export async function getRelevantDocuments({ added, modified, removed }) {
removed.find(
(node) =>
node.data.id === ref.dila_id || node.data.cid === ref.dila_cid
) ||
// added articles can be an old article which version has bumped
// so id is new but cid hasn't change
added.find((node) => node.data.cid === ref.dila_cid)
)
);

if (references.length) {
Expand Down