Skip to content

Commit 10a11e9

Browse files
authored
feat(page CC): affichage seulement des liens vers les contribs sur les pages CC (#1404)
1 parent 305cb46 commit 10a11e9

File tree

9 files changed

+437
-124
lines changed

9 files changed

+437
-124
lines changed

shared/types/src/elastic/agreements.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { AgreementDoc } from "../hasura";
22
import { DocumentElasticWithSource } from "./common";
3-
import { ContributionElasticDocument } from "./contributions";
43

54
export type ElasticAgreement = DocumentElasticWithSource<AgreementDoc> & {
65
articlesByTheme: ArticleByTheme[];
76
source: "conventions_collectives";
87
description: string;
9-
answers: ExportAnswer[];
8+
answers: AnswerByTheme[];
109
contributions: boolean;
1110
};
1211

13-
// Type pour les réponses
14-
export type ExportAnswer = ContributionElasticDocument & {
15-
theme?: string;
16-
infoMessage: string;
12+
export type AnswerByTheme = {
13+
theme: string;
14+
answers: ExportAnswer[];
15+
};
16+
17+
export type ExportAnswer = {
18+
theme: string;
19+
slug: string;
20+
question: string;
21+
questionIndex: number;
1722
};
1823

1924
// Types pour les Kali-Blocks

shared/types/src/elastic/contributions.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,3 @@ export type ElasticSearchContributionConventionnelle =
125125
export type ElasticSearchContribution =
126126
| ElasticSearchContributionGeneric
127127
| ElasticSearchContributionConventionnelle;
128-
129-
export type ElasticSearchContributionWithInfoMessage =
130-
ElasticSearchContribution & {
131-
infoMessage: string;
132-
};

targets/export-elasticsearch/src/ingester/__fixtures__/data.ts

Lines changed: 273 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { generateAgreements } from "../index";
2+
import { CCN, contribs } from "../../__fixtures__/data";
3+
4+
jest.mock("@shared/utils");
5+
jest.mock("../getAgreementsArticlesByTheme");
6+
7+
describe("generateAgreements", () => {
8+
it("should map and group contributions", async () => {
9+
// @ts-ignore
10+
const pagesCCs = await generateAgreements(CCN, contribs);
11+
12+
expect(pagesCCs.length).toEqual(2);
13+
expect(pagesCCs[0].slug).toEqual(
14+
"1875-veterinaires-personnel-salarie-des-cabinets-et-cliniques-veterinaires"
15+
);
16+
expect(pagesCCs[0].answers.length).toEqual(0);
17+
18+
expect(pagesCCs[1].slug).toEqual(
19+
"5571-convention-dentreprise-fondation-dauteuil"
20+
);
21+
expect(pagesCCs[1].answers).toEqual([
22+
{
23+
answers: [
24+
{
25+
questionIndex: 44,
26+
slug: "1486-quelle-est-la-duree-du-conge-de-maternite",
27+
theme: "Congés et repos",
28+
question: "Quelle est la durée du congé de maternité ?",
29+
},
30+
],
31+
theme: "Congés et repos",
32+
},
33+
{
34+
answers: [
35+
{
36+
questionIndex: 47,
37+
slug: "1351-en-cas-de-maladie-le-salarie-a-t-il-droit-a-une-garantie-demploi",
38+
theme: "Santé, sécurité et conditions de travail",
39+
question:
40+
"En cas de maladie, le salarié a-t-il droit à une garantie d’emploi ?",
41+
},
42+
],
43+
theme: "Santé, sécurité et conditions de travail",
44+
},
45+
]);
46+
});
47+
});

targets/export-elasticsearch/src/ingester/agreements/__tests__/getInfoMessage.test.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { groupByTheme } from "../groupByTheme";
2+
import { ExportAnswer } from "@socialgouv/cdtn-types";
3+
4+
describe("contributionsByTheme", () => {
5+
it("should throw if a contrib has no theme", () => {
6+
expect(() => {
7+
groupByTheme([
8+
{ slug: "hello", questionIndex: 1, question: "contrib" },
9+
]);
10+
}).toThrow("Contribution [1] - hello has no theme.");
11+
});
12+
13+
it("returns the first breadcrumb label", () => {
14+
const data: ExportAnswer[] = [
15+
{ slug: "contrib-1", questionIndex: 1, theme: "A", question: "contrib" },
16+
{ slug: "contrib-1", questionIndex: 1, theme: "C", question: "contrib" },
17+
{ slug: "contrib-1", questionIndex: 1, theme: "B", question: "contrib" },
18+
{ slug: "contrib-2", questionIndex: 2, theme: "A", question: "contrib" },
19+
];
20+
21+
expect(groupByTheme(data)).toEqual([
22+
{
23+
"answers": [
24+
{
25+
"questionIndex": 1,
26+
"slug": "contrib-1",
27+
"theme": "A",
28+
"question": "contrib"
29+
},
30+
{
31+
"questionIndex": 2,
32+
"slug": "contrib-2",
33+
"theme": "A",
34+
"question": "contrib"
35+
}
36+
],
37+
"theme": "A"
38+
},
39+
{
40+
"answers": [
41+
{
42+
"questionIndex": 1,
43+
"slug": "contrib-1",
44+
"theme": "B",
45+
"question": "contrib"
46+
}
47+
],
48+
"theme": "B"
49+
},
50+
{
51+
"answers": [
52+
{
53+
"questionIndex": 1,
54+
"slug": "contrib-1",
55+
"theme": "C",
56+
"question": "contrib"
57+
}
58+
],
59+
"theme": "C"
60+
}
61+
]);
62+
});
63+
});
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import {
22
AgreementDoc,
3-
ElasticAgreement,
43
ContributionElasticDocument,
5-
ExportAnswer,
64
DocumentElasticWithSource,
5+
ElasticAgreement,
76
} from "@socialgouv/cdtn-types";
87
import { SOURCES } from "@socialgouv/cdtn-sources";
98
import { getIDCCs } from "./getIdcc";
109
import getAgreementsArticlesByTheme from "./getAgreementsArticlesByTheme";
1110
import { getTheme } from "./getTheme";
12-
import { getInfoMessage } from "./getInfoMessage";
1311
import pMap from "p-map";
12+
import { groupByTheme } from "./groupByTheme";
1413

1514
const DESCRIPTION =
1615
"Retrouvez les questions-réponses les plus fréquentes organisées par thème et élaborées par le ministère du Travail vous concernant.";
@@ -30,35 +29,30 @@ export const generateAgreements = async (
3029
})
3130
.filter((item) => item.contentType !== "UNKNOWN");
3231

33-
const answers: ExportAnswer[] = contributionByIdccNotUnknown
34-
.map((data) => {
35-
return {
36-
...data,
37-
theme: getTheme(data),
38-
infoMessage: getInfoMessage(data),
39-
};
40-
})
41-
.sort(
42-
// On ordonne les questions par index
43-
(a: ExportAnswer, b: ExportAnswer) =>
44-
a.questionIndex - b.questionIndex
45-
);
32+
const answers = contributionByIdccNotUnknown.map((data) => {
33+
return {
34+
slug: data.slug,
35+
question: data.questionName,
36+
questionIndex: data.questionIndex,
37+
theme: getTheme(data),
38+
};
39+
});
4640

4741
const articlesByTheme = await getAgreementsArticlesByTheme(cc.num);
4842

4943
const agreementGenerated: ElasticAgreement = {
5044
...cc,
51-
answers,
45+
answers: groupByTheme(answers),
5246
articlesByTheme,
5347
contributions: contribIDCCs.has(cc.num),
54-
description: DESCRIPTION, // On affiche la nouvelle description s'il n'y a plus d'anciennes réponses conventionnelles
48+
description: DESCRIPTION,
5549
source: SOURCES.CCN,
5650
};
5751

5852
return agreementGenerated;
5953
},
6054
{
61-
concurrency: 5,
55+
concurrency: 10,
6256
}
6357
);
6458
};

targets/export-elasticsearch/src/ingester/agreements/getInfoMessage.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AnswerByTheme, ExportAnswer } from "@socialgouv/cdtn-types";
2+
3+
export function groupByTheme(
4+
contributions: (Omit<ExportAnswer, "theme"> & { theme?: string })[]
5+
): AnswerByTheme[] {
6+
return contributions
7+
.reduce((grouped: AnswerByTheme[], answer): AnswerByTheme[] => {
8+
if (!answer.theme) {
9+
throw new Error(
10+
`Contribution [${answer.questionIndex}] - ${answer.slug} has no theme.`
11+
);
12+
}
13+
const group = grouped.find((g) => g.theme === answer.theme);
14+
if (group) {
15+
group.answers.push(answer as ExportAnswer);
16+
} else {
17+
grouped.push({ theme: answer.theme, answers: [answer as ExportAnswer] });
18+
}
19+
return grouped;
20+
}, [])
21+
.map((group) => {
22+
group.answers = group.answers.sort(
23+
(a, b) => a.questionIndex - b.questionIndex
24+
);
25+
return group;
26+
})
27+
.sort((a, b) => {
28+
return a.theme.localeCompare(b.theme);
29+
});
30+
}

0 commit comments

Comments
 (0)