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

feat(contribs): publish contributions in the new format #1025

Merged
merged 26 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6190d8c
feat(contribs): publish contributions in the new format
carolineBda Sep 25, 2023
c74822d
Merge branch 'master' into carolineBda/publish-contrib
carolineBda Sep 25, 2023
c1167c3
move more types
carolineBda Sep 25, 2023
e52d278
put back dependency
carolineBda Sep 25, 2023
5e4308b
fix types
carolineBda Sep 25, 2023
9133bbb
Merge branch 'master' into carolineBda/publish-contrib
carolineBda Sep 27, 2023
f786876
fix all types
carolineBda Sep 27, 2023
c60da6b
Merge branch 'master' into carolineBda/publish-contrib
carolineBda Sep 27, 2023
fe84e90
move agreements ingest
carolineBda Sep 27, 2023
ad7ecc5
remove un-used dependencies
carolineBda Sep 28, 2023
fd6dfc5
Merge branch 'master' into carolineBda/publish-contrib
carolineBda Sep 28, 2023
56f95c3
format data after review with Martial
carolineBda Sep 28, 2023
b268368
Merge branch 'master' into carolineBda/publish-contrib
carolineBda Sep 28, 2023
75ac877
Get content from fiche SP
carolineBda Sep 28, 2023
64b6756
handle fiche ids in fiche sp publish
carolineBda Sep 28, 2023
57fbb2c
clean-up
carolineBda Sep 28, 2023
9959dde
clean-up après reviews des devs
carolineBda Oct 6, 2023
b191fb1
Merge branch 'deploy-new-contribs' into carolineBda/publish-contrib
carolineBda Oct 12, 2023
59e35bb
fix(comments ui): meilleur affiche des commentaires & changement de s…
carolineBda Oct 12, 2023
845a4b2
Merge branch 'master' into carolineBda/publish-contrib
maxgfr Oct 13, 2023
7bbdb95
feat(contrib): désactivation du formulaire quand on soumet (#1083)
carolineBda Oct 16, 2023
9503f87
feat(admin): ajout de la suppression d'un commentaire (#1081)
maxgfr Oct 18, 2023
733b372
fix(alert-cli): mise à jour des identifiants PISTE (#1084)
maxgfr Oct 18, 2023
8cdb494
fix(frontend): ajout du titre dans le header (#1085)
maxgfr Oct 18, 2023
d4b5e2d
Merge branch 'master' into carolineBda/publish-contrib
maxgfr Oct 18, 2023
13fdafd
fix(alert-cli): add references from contributions (#1076)
maxgfr Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shared/graphql-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const client = createClient({
"x-hasura-admin-secret": HASURA_GRAPHQL_ADMIN_SECRET,
},
},
maskTypename: true,
requestPolicy: "network-only",
url: HASURA_GRAPHQL_ENDPOINT,
});
7 changes: 3 additions & 4 deletions shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@ export type Question = {
export type Answer = {
id: string;
idcc: string;
markdown: string;
content: string;
references: ContributionReference[];
};

export type GenericAnswer = {
id: string;
markdown: string;
content: string;
description: string;
text: string;
references: ContributionReference[];
Expand All @@ -457,7 +457,6 @@ export type GenericAnswer = {
export type ContributionReference = BaseRef | DilaRef;

export type DilaRef = {
category: "agreement" | "labor_code";
url: string;
title: string;
dila_id: string;
Expand All @@ -468,7 +467,7 @@ export type DilaRef = {
export type BaseRef = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je trouve BaseRef pas adapté. Ce sont nos contenus et ce ne sont pas des références mais des contenus liés. On pourrait le renommer CdtnRelatedContent ?

category: null;
title: string;
url: string | null;
url?: string | null;
carolineBda marked this conversation as resolved.
Show resolved Hide resolved
};

export type State =
Expand Down
4 changes: 2 additions & 2 deletions targets/ingester/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const config = {
coverageDirectory: "coverage",
preset: "ts-jest/presets/js-with-ts-esm",
collectCoverageFrom: ["src/**/*.ts", "!**/node_modules/**"],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
},
roots: ["<rootDir>/src"],
testEnvironment: "node",
Expand Down
144 changes: 68 additions & 76 deletions targets/ingester/src/lib/fetchContributions/AnswerExtractor.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

top le refacto ! le code est beaucoup plus simple

Original file line number Diff line number Diff line change
@@ -1,116 +1,111 @@
import type {
Answer,
BaseRef,
ContributionReference,
GenericAnswer,
} from "@shared/types";
import remark from "remark";
import strip from "strip-markdown";

import type { AgreementAnswerRaw, AnswerRaw } from "./types";
// Unified works only with require
// eslint-disable-next-line @typescript-eslint/no-require-imports
import unified = require("unified");
import { IndexedAgreement } from "@socialgouv/kali-data-types";
import type { AnswerRaw } from "./types"; // Unified works only with require
import { IndexedAgreement } from "@socialgouv/kali-data-types"; // eslint-disable-next-line @typescript-eslint/no-require-imports
import slugify from "@socialgouv/cdtn-slugify";

export class AnswerExtractor {
constructor(agreements: IndexedAgreement[]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.mdStriper = remark().use(strip as any) as any;
this.agreements = agreements;
}

public extractGenericAnswer(answers: AnswerRaw[]): GenericAnswer | null {
const genericAnswer = answers.find((answer) => answer.agreement === null);
const genericAnswer = answers.find(
(answer) => answer.agreement.id === "0000"
);
if (!genericAnswer) {
return null;
}
const genericTextAnswer = this.genericTextAnswer(genericAnswer);
const genericTextAnswer = this.toText(genericAnswer);
return {
description:
genericTextAnswer.slice(0, genericTextAnswer.indexOf(" ", 150)) + "…",
id: genericAnswer.id,
markdown: genericAnswer.markdown,
references: genericAnswer.references,
text: genericTextAnswer,
content: genericAnswer.content,
references: this.aggregateReferences(genericAnswer),
text: genericTextAnswer, // Utilisé pour générer la page contribution TODO: à enlever si plus nécessaire
};
}

private mapKaliRefs = (
idcc: string,
references: any[]
): ContributionReference[] => {
if (!references.length) return [];
const agreement = this.agreements.find(
(item) => this.comparableIdcc(item.num) === this.comparableIdcc(idcc)
);
if (!agreement) {
throw new Error(`agreement ${idcc} not found `);
}
return references.map((ref) => {
return {
title: ref.title,
url: `https://legifrance.gouv.fr/conv_coll/id/${ref.kali_article.id}/?idConteneur=${agreement.id}`,
} as BaseRef;
});
};

private mapLegiRefs = (references: any[]): ContributionReference[] => {
if (!references.length) return [];

return references.map((ref) => {
return {
title: ref.legi_article.title,
url: "/code-du-travail/" + slugify(ref.legi_article.title),
m-maillot marked this conversation as resolved.
Show resolved Hide resolved
} as BaseRef;
});
};

private mapOtherRefs = (references: any[]): ContributionReference[] => {
if (!references.length) return [];

return references.map((ref) => {
if (!ref.url) delete ref.url;
return ref as BaseRef;
});
};

private aggregateReferences(answer: AnswerRaw): ContributionReference[] {
return this.mapKaliRefs(answer.agreement.id, answer.kali_references)
.concat(this.mapLegiRefs(answer.legi_references))
.concat(this.mapOtherRefs(answer.other_references))
.concat(answer.cdtn_references); // je n'ai pas fait le mapping parce qu'on a besoin du package packages/code-du-travail-utils/src/sources.ts utilisé seulement dans le front
}

public extractAgreementAnswers(answers: AnswerRaw[]): Answer[] {
return this.filterAgreementAnswers(answers)
.map((answer) => ({
id: answer.id,
idcc: answer.agreement.idcc,
markdown: answer.markdown,
references: answer.references
.map(this.createGetRefUrl(answer.agreement.idcc))
.sort(this.sortBy("title")),
idcc: answer.agreement.id,
content: answer.content,
otherAnswer: answer.otherAnswer, // on renomerait pas ce champs ici ?
references: this.aggregateReferences(answer).sort(this.sortBy("title")),
}))
.sort(this.sortBy("idcc"));
}

private filterAgreementAnswers(answers: AnswerRaw[]): AgreementAnswerRaw[] {
return answers.filter(
(answer) =>
answer.agreement !== null && this.hasAgreement(answer.agreement.idcc)
) as AgreementAnswerRaw[];
// est-ce toujours nécessaire??
// on check que toutes les contrib pointent vers un agreement qui existe
private filterAgreementAnswers(answers: AnswerRaw[]): AnswerRaw[] {
return answers.filter((answer) => this.hasAgreement(answer.agreement.id));
}

private hasAgreement(idcc: string): boolean {
const agreement = this.agreements.find(
(convention) =>
this.comparableIdcc(convention.num) === this.comparableIdcc(idcc)
);
return agreement ? true : false;
return !!agreement;
}

private createGetRefUrl(
idcc: string
): (ref: ContributionReference) => ContributionReference {
const agreement = this.agreements.find(
(item) => this.comparableIdcc(item.num) === this.comparableIdcc(idcc)
);
if (!agreement) {
throw new Error(`agreement ${idcc} not found `);
}
return function getRefUrl(reference) {
switch (reference.category) {
case "agreement": {
if (reference.dila_id) {
reference.url = `https://legifrance.gouv.fr/conv_coll/id/${reference.dila_id}/?idConteneur=${agreement.id}`;
} else if (agreement.url) {
reference.url = agreement.url;
}
return reference;
}
case "labor_code": {
if (reference.dila_id) {
reference.url = `https://legifrance.gouv.fr/codes/id/${reference.dila_id}`;
} else {
reference.url =
"https://www.legifrance.gouv.fr/codes/id/LEGITEXT000006072050";
}
return reference;
}
case null: /* Nothing to do */
}

return reference;
};
}

private genericTextAnswer(genericAnswer: AnswerRaw): string {
try {
return this.mdStriper
.processSync(genericAnswer.markdown)
.toString()
.replace(/(\s)\s+/, "$1")
.trim() as unknown as string;
} catch (e) {
console.error(genericAnswer);
console.error(this.mdStriper.processSync(genericAnswer.markdown));
throw e;
}
}
private toText = (answer: AnswerRaw): string =>
(answer.content || "").replace(/<[^>]*>?/gm, "").replace(/&nbsp;/gm, " ");
Fixed Show fixed Hide fixed
carolineBda marked this conversation as resolved.
Show resolved Hide resolved

private readonly comparableIdcc = (num: number | string): number =>
parseInt(num.toString(), 10);
Expand All @@ -120,8 +115,5 @@
(a: T, b: T): number =>
`${a[key]}`.localeCompare(`${b[key]}`);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly mdStriper: unified.Processor<any>;

private readonly agreements: IndexedAgreement[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export class ContributionDatabase implements ContributionRepository {

public async fetchAll(): Promise<QuestionRaw[]> {
const res = await client
.query<{ questions: QuestionRaw[] }>(fetchAllContributions)
.query<{ contribution_questions: QuestionRaw[] }>(fetchAllContributions)
.toPromise();
if (res.error) {
throw res.error;
}
if (!res.data?.questions) {
throw new Error("Failed to get, undefined object");
if (!res.data?.contribution_questions) {
throw new Error("Failed to get contribution_questions");
}
return res.data.questions;
return res.data.contribution_questions;
}

private readonly client: Client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
{
"active": true,
"id": "CODE_DU_TRAVAIL",
"num": "0000",
"title": "Code du travail"
},
{
"active": true,
"date_publi": "2019-02-15T00:00:00.000Z",
Expand Down Expand Up @@ -4793,4 +4799,4 @@
"title": "Convention collective nationale de l'industrie de la fabrication des ciments du 2 octobre 2019 - Etendue par arrêté du 30 juillet 2021 JORF 17 août 2021, modifié par arrêté du 17 sept. 2021 JORF 28 sept. 2021.",
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000043996969"
}
]
]
Loading
Loading