Skip to content

Commit

Permalink
fix(contributions): add slug for all idcc (even when starting with 0) (
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda authored Jun 29, 2023
1 parent 8a85d98 commit 1bb8628
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 46 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ services:
- 9200:9200
volumes:
- elastic_data:/usr/share/elasticsearch/data
environment:
- ES_JAVA_OPTS=-Xmx4g

kibana:
image: docker.elastic.co/kibana/kibana:8.4.2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,99 @@
import { getDuplicateSlugs } from "../cdtnDocuments";
import {
getDuplicateSlugs,
updateContributionsAndGetIDCCs,
} from "../cdtnDocuments";
import { context } from "../context";

jest.mock("@socialgouv/cdtn-logger");

describe("getDuplicateSlug", () => {
test("should return an empty array if there is no duplicate slug", async () => {
context.provide();
const documents = [
[
{ slug: "slug-1", source: "cdt" },
{ slug: "slug-2", source: "cdt" },
],
[
{ slug: "slug-1", source: "contribution" },
{ slug: "slug-2", source: "contribution" },
],
];
const duplicateSlugs = await getDuplicateSlugs(documents);
expect(Object.entries(duplicateSlugs).length).toBe(0);
describe("cdtnDocuments", () => {
describe("getDuplicateSlug", () => {
test("should return an empty array if there is no duplicate slug", async () => {
context.provide();
const documents = [
[
{ slug: "slug-1", source: "cdt" },
{ slug: "slug-2", source: "cdt" },
],
[
{ slug: "slug-1", source: "contribution" },
{ slug: "slug-2", source: "contribution" },
],
];
const duplicateSlugs = await getDuplicateSlugs(documents);
expect(Object.entries(duplicateSlugs).length).toBe(0);
});

test("should return an array of duplicated slug", async () => {
context.provide();
const documents = [
[
{ slug: "slug-1", source: "cdt" },
{ slug: "slug-2", source: "cdt" },
],
[
{ slug: "slug-1", source: "faq" },
{ slug: "slug-1", source: "faq" },
],
[
{ slug: "slug-4", source: "fiche" },
{ slug: "slug-3", source: "fiche" },
],
];
const duplicateSlugs = await getDuplicateSlugs(documents);
expect(Object.entries(duplicateSlugs).length).toBe(1);
});
});

test("should return an array of duplicated slug", async () => {
context.provide();
const documents = [
[
{ slug: "slug-1", source: "cdt" },
{ slug: "slug-2", source: "cdt" },
],
[
{ slug: "slug-1", source: "faq" },
{ slug: "slug-1", source: "faq" },
],
[
{ slug: "slug-4", source: "fiche" },
{ slug: "slug-3", source: "fiche" },
],
];
const duplicateSlugs = await getDuplicateSlugs(documents);
expect(Object.entries(duplicateSlugs).length).toBe(1);
describe("updateContributionsAndGetIDCCs", () => {
test("should return a list of iddc", async () => {
const ccnData = [
{
num: 829,
slug: "829-convention-collective-departementale-des-industries-metallurgiques-et-des-in",
},
{
num: 292,
slug: "292-plasturgie",
},
{
num: 1557,
slug: "1557-commerce-des-articles-de-sport-et-equipements-de-loisirs",
},
{
num: 1909,
slug: "1909-organismes-de-tourisme",
},
];

const contributions: {
answers: {
conventionAnswer: { idcc: string; slug?: string };
};
}[] = [
{ answers: { conventionAnswer: { idcc: "0292" } } },
{ answers: { conventionAnswer: { idcc: "0829" } } },
{ answers: { conventionAnswer: { idcc: "1557" } } },
{ answers: { conventionAnswer: { idcc: "1909" } } },
];
const idccs = await updateContributionsAndGetIDCCs(
contributions,
// @ts-ignore
ccnData
);
expect(Array.from(idccs)).toEqual([292, 829, 1557, 1909]);
expect(contributions[0].answers.conventionAnswer.slug).toBe(
"292-plasturgie"
);
expect(contributions[1].answers.conventionAnswer.slug).toBe(
"829-convention-collective-departementale-des-industries-metallurgiques-et-des-in"
);
expect(contributions[2].answers.conventionAnswer.slug).toBe(
"1557-commerce-des-articles-de-sport-et-equipements-de-loisirs"
);
expect(contributions[3].answers.conventionAnswer.slug).toBe(
"1909-organismes-de-tourisme"
);
});
});
});
33 changes: 21 additions & 12 deletions shared/elasticsearch-document-adapter/src/cdtnDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { markdownTransform } from "./markdown";
import type { ThemeQueryResult } from "./types/themes";
import { keyFunctionParser } from "./utils";
import { getVersions } from "./versions";
import { DocumentElasticWithSource } from "./types/Glossary";

const themesQuery = JSON.stringify({
query: `{
Expand Down Expand Up @@ -74,6 +75,25 @@ export async function getDuplicateSlugs(allDocuments: any) {
);
}

export function updateContributionsAndGetIDCCs(
contributions: any[],
ccnData: DocumentElasticWithSource<AgreementDoc>[]
) {
const contribIDCCs = new Set<number>();
contributions.forEach(({ answers }) => {
if (answers.conventionAnswer) {
const idccNum = parseInt(answers.conventionAnswer.idcc);
contribIDCCs.add(idccNum);

const ccn = ccnData.find((ccn) => ccn.num === idccNum);
if (ccn?.slug) {
answers.conventionAnswer.slug = ccn.slug;
}
}
});
return contribIDCCs;
}

export async function* cdtnDocumentsGen() {
const CDTN_ADMIN_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
Expand Down Expand Up @@ -177,18 +197,7 @@ export async function* cdtnDocumentsGen() {

// we keep track of the idccs used in the contributions
// in order to flag the corresponding conventions collectives below
const contribIDCCs = new Set();
contributions.forEach(({ answers }: any) => {
if (answers.conventionAnswer) {
contribIDCCs.add(parseInt(answers.conventionAnswer.idcc));
const ccn = ccnData.find(
(ccn) => ccn.num.toString() === answers.conventionAnswer.idcc
);
if (ccn && ccn.slug) {
answers.conventionAnswer.slug = ccn.slug;
}
}
});
const contribIDCCs = updateContributionsAndGetIDCCs(contributions, ccnData);

yield {
documents: contributions.map(
Expand Down

0 comments on commit 1bb8628

Please sign in to comment.