From 7e943ceec4f0d1a3b22fefb1ab3c302f336235e4 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 16 May 2024 15:55:34 +0200 Subject: [PATCH 1/7] implement message block no cdt --- shared/types/src/hasura/contributions.ts | 2 ++ .../__tests__/generateMessageBlock.test.ts | 24 +++++++++++++------ .../contributions/fetchMessageBlock.ts | 2 ++ .../src/ingester/contributions/generate.ts | 2 +- .../contributions/generateMessageBlock.ts | 16 +++++++++++++ .../contribution_question_messages.yaml | 6 +++++ .../down.sql | 2 ++ .../up.sql | 13 ++++++++++ 8 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/down.sql create mode 100644 targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/up.sql diff --git a/shared/types/src/hasura/contributions.ts b/shared/types/src/hasura/contributions.ts index f4e04d202..5e8c99679 100644 --- a/shared/types/src/hasura/contributions.ts +++ b/shared/types/src/hasura/contributions.ts @@ -135,8 +135,10 @@ export type ContributionMessageBlock = { id: string; label: string; contentAgreement: string; + contentAgreementWithoutLegal: string; contentLegal: string; contentNotHandled: string; + contentNotHandledWithoutLegal: string; }; export type ContributionAgreementMessage = { diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts index 0cfeed989..a7861f3d5 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts @@ -1,4 +1,8 @@ -import { ContributionContentType } from "@socialgouv/cdtn-types"; +import { + ContributionContentType, + ContributionDocumentJson, + DocumentElasticWithSource, +} from "@socialgouv/cdtn-types"; import { fetchMessageBlock } from "../fetchMessageBlock"; import { fetchAgreementMessage } from "../fetchAgreementMessage"; import { generateMessageBlock } from "../generateMessageBlock"; @@ -6,6 +10,8 @@ import { generateMessageBlock } from "../generateMessageBlock"; jest.mock("../fetchMessageBlock"); jest.mock("../fetchAgreementMessage"); +const contributions: DocumentElasticWithSource[] = []; + describe("generateMessageBlock", () => { const mockContribution: any = { questionId: "123", @@ -36,6 +42,7 @@ describe("generateMessageBlock", () => { mockContribution.contentType = contentType as ContributionContentType; const result: string | undefined = await generateMessageBlock( + contributions, mockContribution ); @@ -63,6 +70,7 @@ describe("generateMessageBlock", () => { mockContribution.contentType = contentType as ContributionContentType; mockContribution.idcc = "0000"; const result: string | undefined = await generateMessageBlock( + contributions, mockContribution ); @@ -75,24 +83,25 @@ describe("generateMessageBlock", () => { mockContribution.contentType = "UNKNOWN_TYPE"; mockContribution.idcc = "1234"; - await expect(generateMessageBlock(mockContribution)).rejects.toThrowError( - "Unknown content type" - ); + await expect( + generateMessageBlock(contributions, mockContribution) + ).rejects.toThrowError("Unknown content type"); }); it("should throw an error for unknown content type", async () => { mockContribution.contentType = null; mockContribution.idcc = "1234"; - await expect(generateMessageBlock(mockContribution)).rejects.toThrowError( - "Unknown content type" - ); + await expect( + generateMessageBlock(contributions, mockContribution) + ).rejects.toThrowError("Unknown content type"); }); it("should return undefined if no message block setup for the question", async () => { (fetchMessageBlock as jest.Mock).mockResolvedValue(undefined); const result: string | undefined = await generateMessageBlock( + contributions, mockContribution ); @@ -107,6 +116,7 @@ describe("generateMessageBlock", () => { ); const result: string | undefined = await generateMessageBlock( + contributions, mockContribution ); diff --git a/targets/export-elasticsearch/src/ingester/contributions/fetchMessageBlock.ts b/targets/export-elasticsearch/src/ingester/contributions/fetchMessageBlock.ts index 56fbebc81..5b80119c9 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/fetchMessageBlock.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/fetchMessageBlock.ts @@ -11,6 +11,8 @@ query get_question($id: uuid!) { contentAgreement contentLegal contentNotHandled + contentAgreementWithoutLegal + contentNotHandledWithoutLegal } } } diff --git a/targets/export-elasticsearch/src/ingester/contributions/generate.ts b/targets/export-elasticsearch/src/ingester/contributions/generate.ts index 043da3339..0efe17df1 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generate.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generate.ts @@ -55,7 +55,7 @@ export async function generateContributions( const content = await generateContent(contributions, contrib); - const messageBlock = await generateMessageBlock(contrib); + const messageBlock = await generateMessageBlock(contributions, contrib); const references = generateReferences(contributions, contrib); diff --git a/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts b/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts index 836e2ecbd..0d95bdc21 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts @@ -6,6 +6,7 @@ import { fetchMessageBlock } from "./fetchMessageBlock"; import { fetchAgreementMessage } from "./fetchAgreementMessage"; export const generateMessageBlock = async ( + contributions: DocumentElasticWithSource[], contrib: DocumentElasticWithSource ): Promise => { const agreementMessage = await fetchAgreementMessage(contrib.idcc); @@ -13,6 +14,9 @@ export const generateMessageBlock = async ( return agreementMessage; } const messageBlock = await fetchMessageBlock(contrib.questionId); + const contribGeneric = contributions.find( + (c) => c.questionId === contrib.questionId && c.idcc === "0000" + ); if (!messageBlock) { return undefined; } else if ( @@ -21,12 +25,24 @@ export const generateMessageBlock = async ( ) { return messageBlock.contentLegal; } else if (contrib.contentType === "ANSWER" || contrib.contentType === "SP") { + if ( + contribGeneric?.contentType === "GENERIC_NO_CDT" && + messageBlock.contentAgreementWithoutLegal + ) { + return messageBlock.contentAgreementWithoutLegal; + } return messageBlock.contentAgreement; } else if ( contrib.contentType === "NOTHING" || contrib.contentType === "CDT" || contrib.contentType === "UNFAVOURABLE" ) { + if ( + contribGeneric?.contentType === "GENERIC_NO_CDT" && + messageBlock.contentNotHandledWithoutLegal + ) { + return messageBlock.contentNotHandledWithoutLegal; + } return messageBlock.contentNotHandled; } throw new Error("Unknown content type"); diff --git a/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml b/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml index 12e34d656..7e51d3034 100644 --- a/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml +++ b/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml @@ -5,14 +5,20 @@ configuration: column_config: content_agreement: custom_name: contentAgreement + content_agreement_without_legal: + custom_name: contentAgreementWithoutLegal content_legal: custom_name: contentLegal content_not_handled: custom_name: contentNotHandled + content_not_handled_without_legal: + custom_name: contentNotHandledWithoutLegal custom_column_names: content_agreement: contentAgreement + content_agreement_without_legal: contentAgreementWithoutLegal content_legal: contentLegal content_not_handled: contentNotHandled + content_not_handled_without_legal: contentNotHandledWithoutLegal custom_root_fields: {} select_permissions: - role: super diff --git a/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/down.sql b/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/down.sql new file mode 100644 index 000000000..4515cbf58 --- /dev/null +++ b/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/down.sql @@ -0,0 +1,2 @@ +alter table "contribution"."question_messages" drop column "content_not_handled_without_legal"; +alter table "contribution"."question_messages" drop column "content_agreement_without_legal"; diff --git a/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/up.sql b/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/up.sql new file mode 100644 index 000000000..476444743 --- /dev/null +++ b/targets/hasura/migrations/default/1715781737558_alter_table_contribution_question_messages_add_column_content_agreement_without_legal/up.sql @@ -0,0 +1,13 @@ +alter table "contribution"."question_messages" add column "content_agreement_without_legal" text + null; +alter table "contribution"."question_messages" add column "content_not_handled_without_legal" text + null; + +update contribution.question_messages +set content_not_handled_without_legal = '

Ces informations sont issues de l’analyse de votre convention collective de branche étendue.

D’autres textes ou votre contrat de travail peuvent également prévoir des règles spécifiques sur ce sujet. Plusieurs cas de figure peuvent se présenter :

  • Si un accord d’entreprise (ou de groupe ou d’établissement) traite de ce sujet : c’est ce texte qui s’appliquera ;
  • Dans tous les cas, si le contrat de travail prévoit des règles plus favorables : il s’appliquera.

Attention, d’autres règles non étendues peuvent potentiellement vous être applicables.

', +content_agreement_without_legal = '

Ces informations sont issues de l’analyse des règles prévues par votre convention collective de branche étendue. Si une convention ou un accord d’entreprise (ou de groupe, ou d’établissement) existe dans votre entreprise, il s’appliquera, qu’il soit plus ou moins favorable. Toutefois, si votre contrat de travail contient des règles plus favorables, ce sont celles-ci qui s’appliqueront.

Attention, d’autres règles non étendues peuvent potentiellement vous être applicables.

' +where label = 'Message Bloc 3'; +update contribution.question_messages +set content_not_handled_without_legal = '

Ces informations sont issues de l’analyse de votre convention collective de branche étendue.

D’autres textes ou votre contrat de travail peuvent également prévoir des règles spécifiques sur ce sujet.

Plusieurs cas de figure peuvent se présenter :

  • Si un accord d’entreprise (ou de groupe ou d’établissement) traite de ce sujet : c’est ce texte qui s’appliquera ;
  • Dans tous les cas, si le contrat de travail prévoit des règles plus favorables : il s’appliquera.

Attention, d’autres règles non étendues peuvent potentiellement vous être applicables.

', +content_agreement_without_legal = '

Ces informations sont issues de l’analyse des règles prévues par votre convention collective de branche étendue. Si une convention ou un accord d’entreprise (ou de groupe, ou d’établissement) existant dans votre entreprise prévoit des garanties au moins équivalentes sur le même sujet, elles s’appliqueront, sauf si votre contrat de travail contient des règles plus favorables.

Attention, d’autres règles non étendues peuvent potentiellement vous être applicables.

' +where label = 'Message Bloc 2'; From a484f6bf19e962a92577a9d9568b0f5f96d5bf33 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 29 May 2024 12:17:00 +0200 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20ajout=20des=20nouveaux=20messages?= =?UTF-8?q?=C3=A0=20l'admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../questions/EditQuestionForm.tsx | 48 +++++++++++++++++++ .../contributions/questions/Question.query.ts | 2 + .../src/components/contributions/type.ts | 2 + .../contribution_question_messages.yaml | 4 +- .../tables/contribution_questions.yaml | 8 +++- 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/targets/frontend/src/components/contributions/questions/EditQuestionForm.tsx b/targets/frontend/src/components/contributions/questions/EditQuestionForm.tsx index 553adf32a..8fb1b5bda 100644 --- a/targets/frontend/src/components/contributions/questions/EditQuestionForm.tsx +++ b/targets/frontend/src/components/contributions/questions/EditQuestionForm.tsx @@ -143,6 +143,30 @@ export const EditQuestionForm = ({ /> + + } + aria-controls="Texte applicable si la convention collective est non traitée, non sélectionnée ou si la réponse est dépubliée" + id="contentLegal" + > + + Texte applicable si la convention collective est non + traitée et n'a pas de légal + + + + + + + + + + + } @@ -167,6 +191,30 @@ export const EditQuestionForm = ({ /> + + } + aria-controls="Texte applicable si la convention collective est non traitée, non sélectionnée ou si la réponse est dépubliée" + id="contentLegal" + > + + Texte applicable si la convention collective ne prévoit + rien et n'a pas de légal + + + + + + + + + + + } diff --git a/targets/frontend/src/components/contributions/questions/Question.query.ts b/targets/frontend/src/components/contributions/questions/Question.query.ts index 0346723ce..8b6f989f9 100644 --- a/targets/frontend/src/components/contributions/questions/Question.query.ts +++ b/targets/frontend/src/components/contributions/questions/Question.query.ts @@ -37,6 +37,8 @@ query SelectQuestion($questionId: uuid) { contentAgreement contentLegal contentNotHandled + contentNotHandledWithoutLegal + contentAgreementWithoutLegal id label } diff --git a/targets/frontend/src/components/contributions/type.ts b/targets/frontend/src/components/contributions/type.ts index cfccd33a2..430e602e6 100644 --- a/targets/frontend/src/components/contributions/type.ts +++ b/targets/frontend/src/components/contributions/type.ts @@ -40,7 +40,9 @@ export const messageSchema = z.object({ label: z.string(), contentAgreement: z.string(), contentLegal: z.string(), + contentAgreementWithoutLegal: z.string(), contentNotHandled: z.string(), + contentNotHandledWithoutLegal: z.string(), }); export type Message = z.infer; diff --git a/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml b/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml index 7e51d3034..6f1842f8e 100644 --- a/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml +++ b/targets/hasura/metadata/databases/default/tables/contribution_question_messages.yaml @@ -25,8 +25,10 @@ select_permissions: permission: columns: - content_agreement - - content_not_handled + - content_agreement_without_legal - content_legal + - content_not_handled + - content_not_handled_without_legal - id - label filter: {} diff --git a/targets/hasura/metadata/databases/default/tables/contribution_questions.yaml b/targets/hasura/metadata/databases/default/tables/contribution_questions.yaml index e4cb80ac6..4aa1e27d1 100644 --- a/targets/hasura/metadata/databases/default/tables/contribution_questions.yaml +++ b/targets/hasura/metadata/databases/default/tables/contribution_questions.yaml @@ -4,7 +4,13 @@ table: object_relationships: - name: message using: - foreign_key_constraint_on: message_id + manual_configuration: + column_mapping: + message_id: id + insertion_order: null + remote_table: + name: question_messages + schema: contribution array_relationships: - name: answers using: From 3122093337afaca0c917d2328d2c0350a2604115 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 3 Jun 2024 11:32:33 +0200 Subject: [PATCH 3/7] refactor: bouger contrib generic au dessus --- .../src/ingester/contributions/generate.ts | 5 ++++- .../src/ingester/contributions/generateMessageBlock.ts | 5 +---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/targets/export-elasticsearch/src/ingester/contributions/generate.ts b/targets/export-elasticsearch/src/ingester/contributions/generate.ts index 0efe17df1..0ea7b8a92 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generate.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generate.ts @@ -51,11 +51,14 @@ export async function generateContributions( for (let i = 0; i < contributions.length; i++) { const contrib = contributions[i]; + const contribGeneric = contributions.find( + (c) => c.questionId === contrib.questionId && c.idcc === "0000" + ); const highlight = ccnListWithHighlight[parseInt(contrib.idcc)]; const content = await generateContent(contributions, contrib); - const messageBlock = await generateMessageBlock(contributions, contrib); + const messageBlock = await generateMessageBlock(contribGeneric, contrib); const references = generateReferences(contributions, contrib); diff --git a/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts b/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts index 0d95bdc21..c07272a6f 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generateMessageBlock.ts @@ -6,7 +6,7 @@ import { fetchMessageBlock } from "./fetchMessageBlock"; import { fetchAgreementMessage } from "./fetchAgreementMessage"; export const generateMessageBlock = async ( - contributions: DocumentElasticWithSource[], + contribGeneric: DocumentElasticWithSource | undefined, contrib: DocumentElasticWithSource ): Promise => { const agreementMessage = await fetchAgreementMessage(contrib.idcc); @@ -14,9 +14,6 @@ export const generateMessageBlock = async ( return agreementMessage; } const messageBlock = await fetchMessageBlock(contrib.questionId); - const contribGeneric = contributions.find( - (c) => c.questionId === contrib.questionId && c.idcc === "0000" - ); if (!messageBlock) { return undefined; } else if ( From 9f54b5629032527f42e04f0b868ae2cbb38ad11f Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 3 Jun 2024 14:59:22 +0200 Subject: [PATCH 4/7] chore: add TU --- .../__tests__/generateMessageBlock.test.ts | 71 +++++++++++++++++-- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts index a7861f3d5..e0e65fbee 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts @@ -10,7 +10,7 @@ import { generateMessageBlock } from "../generateMessageBlock"; jest.mock("../fetchMessageBlock"); jest.mock("../fetchAgreementMessage"); -const contributions: DocumentElasticWithSource[] = []; +let contributionGeneric: DocumentElasticWithSource | undefined = undefined; describe("generateMessageBlock", () => { const mockContribution: any = { @@ -42,7 +42,7 @@ describe("generateMessageBlock", () => { mockContribution.contentType = contentType as ContributionContentType; const result: string | undefined = await generateMessageBlock( - contributions, + contributionGeneric, mockContribution ); @@ -70,7 +70,7 @@ describe("generateMessageBlock", () => { mockContribution.contentType = contentType as ContributionContentType; mockContribution.idcc = "0000"; const result: string | undefined = await generateMessageBlock( - contributions, + contributionGeneric, mockContribution ); @@ -84,7 +84,7 @@ describe("generateMessageBlock", () => { mockContribution.idcc = "1234"; await expect( - generateMessageBlock(contributions, mockContribution) + generateMessageBlock(contributionGeneric, mockContribution) ).rejects.toThrowError("Unknown content type"); }); @@ -93,7 +93,7 @@ describe("generateMessageBlock", () => { mockContribution.idcc = "1234"; await expect( - generateMessageBlock(contributions, mockContribution) + generateMessageBlock(contributionGeneric, mockContribution) ).rejects.toThrowError("Unknown content type"); }); @@ -101,7 +101,7 @@ describe("generateMessageBlock", () => { (fetchMessageBlock as jest.Mock).mockResolvedValue(undefined); const result: string | undefined = await generateMessageBlock( - contributions, + contributionGeneric, mockContribution ); @@ -116,11 +116,68 @@ describe("generateMessageBlock", () => { ); const result: string | undefined = await generateMessageBlock( - contributions, + contributionGeneric, mockContribution ); expect(fetchAgreementMessage).toHaveBeenCalledWith("1234"); expect(result).toEqual("fetchedAgreementMessage"); }); + describe("Tests avec une contribution generic no cdt", () => { + beforeEach(() => { + contributionGeneric = { + contentType: "GENERIC_NO_CDT", + id: "id", + cdtnId: "cdtnId", + breadcrumbs: [], + title: "", + slug: "", + source: "contributions", + text: "text", + isPublished: true, + excludeFromSearch: false, + metaDescription: "metaDescription", + refs: [], + references: [], + questionIndex: 0, + questionId: "questionId", + questionName: "questionName", + linkedContent: [], + description: "description", + idcc: "0000", + type: "generic-no-cdt", + messageBlockGenericNoCDT: "messageBlockGenericNoCDT", + messageBlockGenericNoCDTUnextendedCC: "messageBlockGenericNoCDTUnextendedCC" + } + }) + it.each(["ANSWER", "SP"])("should throw a contentAgreementWithoutLegal", async (contentType) => { + (fetchMessageBlock as jest.Mock).mockResolvedValue({ + contentAgreementWithoutLegal: "agrement without legal" + }); + mockContribution.contentType = contentType; + mockContribution.idcc = "1234"; + + const messageBloc = await generateMessageBlock(contributionGeneric, mockContribution); + + expect( + messageBloc + ).toEqual("agrement without legal"); + }); + + it.each(["NOTHING", "CDT", "UNFAVOURABLE"])("should throw a contentNotHandledWithoutLegal", async (contentType) => { + (fetchMessageBlock as jest.Mock).mockResolvedValue({ + contentNotHandledWithoutLegal: "content not handled without legal" + }); + mockContribution.contentType = contentType; + mockContribution.idcc = "1234"; + + const messageBloc = await generateMessageBlock(contributionGeneric, mockContribution); + + expect( + messageBloc + ).toEqual("content not handled without legal"); + }); + }) + + }); From 429f5c63c3a38200a55dada1186e3e899a88e0a3 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 3 Jun 2024 16:10:02 +0200 Subject: [PATCH 5/7] refactor: use generic in all generate --- .../__tests__/generateContent.test.ts | 16 ++++++++-------- .../__tests__/generateReferences.test.ts | 6 +++--- .../src/ingester/contributions/generate.ts | 4 ++-- .../ingester/contributions/generateContent.ts | 19 ++++++++----------- .../contributions/generateReferences.ts | 11 ++++------- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateContent.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateContent.test.ts index fc37ac901..70a03a35a 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateContent.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateContent.test.ts @@ -39,7 +39,7 @@ describe("generateContent", () => { }; const result: ContributionContent = await generateContent( - mockContributions, + mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution ); @@ -55,7 +55,7 @@ describe("generateContent", () => { }; const result: ContributionContent = await generateContent( - mockContributions, + mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution ); @@ -73,7 +73,7 @@ describe("generateContent", () => { }; await expect( - generateContent(mockContributions, contribution) + generateContent(mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution) ).rejects.toThrowError( `Aucune contribution générique a été retrouvée pour la contribution [10 - 3239] (id générique non trouvé : unknown-type)` ); @@ -88,7 +88,7 @@ describe("generateContent", () => { }; await expect( - generateContent(mockContributions, contribution) + generateContent(mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution) ).rejects.toThrowError( "La contribution [2 - 1516] ne peut pas référencer une générique qui n'a pas de réponse" ); @@ -110,7 +110,7 @@ describe("generateContent", () => { (fetchFicheSp as jest.Mock).mockResolvedValue(mockFicheSpContent); const result: ContributionContent = await generateContent( - mockContributions, + mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution ); @@ -138,7 +138,7 @@ describe("generateContent", () => { (fetchFicheSp as jest.Mock).mockResolvedValue(mockFicheSpContent); const result: ContributionContent = await generateContent( - mockContributions, + mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution ); @@ -158,7 +158,7 @@ describe("generateContent", () => { }; await expect( - generateContent(mockContributions, contribution) + generateContent(mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution) ).rejects.toThrowError( 'Type de contribution generic inconnu "unknown_type" for [GENERIC_UNKNOWN_TYPE]' ); @@ -171,7 +171,7 @@ describe("generateContent", () => { }; const result: ContributionContent = await generateContent( - mockContributions, + mockContributions.find(({id}) => id === contribution.genericAnswerId), contribution ); diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateReferences.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateReferences.test.ts index b4a8a16d7..1ec49ab3a 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateReferences.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateReferences.test.ts @@ -37,7 +37,7 @@ describe("generateReferences", () => { }, ]; - const result = generateReferences(contributions, contrib); + const result = generateReferences(contributions[0], contrib); expect(result).toEqual([ { @@ -74,7 +74,7 @@ describe("generateReferences", () => { ], }; - const result = generateReferences([], contrib); + const result = generateReferences(undefined, contrib); expect(result).toEqual([ { @@ -92,7 +92,7 @@ describe("generateReferences", () => { const contrib: any = { contentType: "CDT", questionIndex: 1 }; expect(() => { - generateReferences([], contrib); + generateReferences(undefined, contrib); }).toThrowError(); }); diff --git a/targets/export-elasticsearch/src/ingester/contributions/generate.ts b/targets/export-elasticsearch/src/ingester/contributions/generate.ts index 0ea7b8a92..d6f195b31 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generate.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generate.ts @@ -56,11 +56,11 @@ export async function generateContributions( ); const highlight = ccnListWithHighlight[parseInt(contrib.idcc)]; - const content = await generateContent(contributions, contrib); + const content = await generateContent(contribGeneric, contrib); const messageBlock = await generateMessageBlock(contribGeneric, contrib); - const references = generateReferences(contributions, contrib); + const references = generateReferences(contribGeneric, contrib); const ccUnextended = await fetchAgreementUnextended(); diff --git a/targets/export-elasticsearch/src/ingester/contributions/generateContent.ts b/targets/export-elasticsearch/src/ingester/contributions/generateContent.ts index d60f8495e..f3081a08d 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generateContent.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generateContent.ts @@ -6,7 +6,7 @@ import { import { fetchFicheSp } from "./fetchFicheSp"; export const generateContent = async ( - contributions: DocumentElasticWithSource[], + contribGeneric: DocumentElasticWithSource | undefined, contrib: DocumentElasticWithSource ): Promise => { switch (contrib.type) { @@ -29,25 +29,22 @@ export const generateContent = async ( }; } case "cdt": { - const cdtContrib = contributions.find( - (v) => v.id === contrib.genericAnswerId - ); - if (!cdtContrib) { + if (!contribGeneric) { throw new Error( `Aucune contribution générique a été retrouvée pour la contribution [${contrib.questionIndex} - ${contrib.idcc}] (id générique non trouvé : ${contrib.genericAnswerId})` ); } - if (cdtContrib.type === "generic-no-cdt") { + if (contribGeneric.type === "generic-no-cdt") { throw new Error( `La contribution [${contrib.questionIndex} - ${contrib.idcc}] ne peut pas référencer une générique qui n'a pas de réponse` ); } - if (cdtContrib.type === "content") { + if (contribGeneric.type === "content") { return { - content: cdtContrib.content, + content: contribGeneric.content, }; - } else if (cdtContrib.type === "fiche-sp") { - const ficheSpContent = await fetchFicheSp(cdtContrib.ficheSpId); + } else if (contribGeneric.type === "fiche-sp") { + const ficheSpContent = await fetchFicheSp(contribGeneric.ficheSpId); return { url: ficheSpContent.url, date: ficheSpContent.date, @@ -56,7 +53,7 @@ export const generateContent = async ( }; } throw new Error( - `Type de contribution generic inconnu "${cdtContrib.type}" for [${cdtContrib.id}]` + `Type de contribution generic inconnu "${contribGeneric.type}" for [${contribGeneric.id}]` ); } } diff --git a/targets/export-elasticsearch/src/ingester/contributions/generateReferences.ts b/targets/export-elasticsearch/src/ingester/contributions/generateReferences.ts index 72ffc302c..7efbac860 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/generateReferences.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/generateReferences.ts @@ -6,24 +6,21 @@ import { import { isReferencingGenericContribution } from "./helpers"; export const generateReferences = ( - contributions: DocumentElasticWithSource[], + contribGeneric: DocumentElasticWithSource | undefined, contrib: DocumentElasticWithSource ): ContributionRef[] => { if (isReferencingGenericContribution(contrib.contentType)) { - const cdtContrib = contributions.find( - (v) => v.questionIndex === contrib.questionIndex && v.idcc === "0000" - ); - if (!cdtContrib) { + if (!contribGeneric) { throw new Error( `Aucune contribution générique a été retrouvée pour la question ${contrib.questionIndex}` ); } - if (cdtContrib.type === "generic-no-cdt") { + if (contribGeneric.type === "generic-no-cdt") { throw new Error( `La contribution [${contrib.questionIndex} - ${contrib.idcc}] ne peut pas référencer une générique qui n'a pas de réponse` ); } - const result = [...contrib.references, ...cdtContrib.references]; + const result = [...contrib.references, ...contribGeneric.references]; return removeDuplicates(result); } return contrib.references; From 1b5e6994285ef53feffdfeb86899aadfc994a445 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 3 Jun 2024 16:30:55 +0200 Subject: [PATCH 6/7] fix: TI --- .../__tests__/generateMessageBlock.test.ts | 118 +++++++++--------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts index e0e65fbee..e97b6084e 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts @@ -10,14 +10,15 @@ import { generateMessageBlock } from "../generateMessageBlock"; jest.mock("../fetchMessageBlock"); jest.mock("../fetchAgreementMessage"); -let contributionGeneric: DocumentElasticWithSource | undefined = undefined; +const contributionGeneric: DocumentElasticWithSource | undefined = undefined; +const mockContribution: any = { + questionId: "123", + contentType: "ANSWER", + idcc: "1234", +}; describe("generateMessageBlock", () => { - const mockContribution: any = { - questionId: "123", - contentType: "ANSWER", - idcc: "1234", - }; + beforeEach(() => { jest.clearAllMocks(); @@ -123,61 +124,62 @@ describe("generateMessageBlock", () => { expect(fetchAgreementMessage).toHaveBeenCalledWith("1234"); expect(result).toEqual("fetchedAgreementMessage"); }); - describe("Tests avec une contribution generic no cdt", () => { - beforeEach(() => { - contributionGeneric = { - contentType: "GENERIC_NO_CDT", - id: "id", - cdtnId: "cdtnId", - breadcrumbs: [], - title: "", - slug: "", - source: "contributions", - text: "text", - isPublished: true, - excludeFromSearch: false, - metaDescription: "metaDescription", - refs: [], - references: [], - questionIndex: 0, - questionId: "questionId", - questionName: "questionName", - linkedContent: [], - description: "description", - idcc: "0000", - type: "generic-no-cdt", - messageBlockGenericNoCDT: "messageBlockGenericNoCDT", - messageBlockGenericNoCDTUnextendedCC: "messageBlockGenericNoCDTUnextendedCC" - } - }) - it.each(["ANSWER", "SP"])("should throw a contentAgreementWithoutLegal", async (contentType) => { - (fetchMessageBlock as jest.Mock).mockResolvedValue({ - contentAgreementWithoutLegal: "agrement without legal" - }); - mockContribution.contentType = contentType; - mockContribution.idcc = "1234"; - - const messageBloc = await generateMessageBlock(contributionGeneric, mockContribution); - expect( - messageBloc - ).toEqual("agrement without legal"); - }); - it.each(["NOTHING", "CDT", "UNFAVOURABLE"])("should throw a contentNotHandledWithoutLegal", async (contentType) => { - (fetchMessageBlock as jest.Mock).mockResolvedValue({ - contentNotHandledWithoutLegal: "content not handled without legal" - }); - mockContribution.contentType = contentType; - mockContribution.idcc = "1234"; - - const messageBloc = await generateMessageBlock(contributionGeneric, mockContribution); - expect( - messageBloc - ).toEqual("content not handled without legal"); +}); +describe("Tests avec une contribution generic no cdt", () => { + let mockedContributionGeneric: DocumentElasticWithSource | undefined = undefined; + beforeEach(() => { + mockedContributionGeneric = { + contentType: "GENERIC_NO_CDT", + id: "id", + cdtnId: "cdtnId", + breadcrumbs: [], + title: "", + slug: "", + source: "contributions", + text: "text", + isPublished: true, + excludeFromSearch: false, + metaDescription: "metaDescription", + refs: [], + references: [], + questionIndex: 0, + questionId: "questionId", + questionName: "questionName", + linkedContent: [], + description: "description", + idcc: "0000", + type: "generic-no-cdt", + messageBlockGenericNoCDT: "messageBlockGenericNoCDT", + messageBlockGenericNoCDTUnextendedCC: "messageBlockGenericNoCDTUnextendedCC" + }; + (fetchMessageBlock as jest.Mock).mockResolvedValue({ + contentAgreementWithoutLegal: "agrement without legal", + contentNotHandledWithoutLegal: "content not handled without legal" }); }) + it.each(["ANSWER", "SP"])("should throw a contentAgreementWithoutLegal", async (contentType) => { + + mockContribution.contentType = contentType; + mockContribution.idcc = "1234"; - -}); + const messageBloc = await generateMessageBlock(mockedContributionGeneric, mockContribution); + + expect( + messageBloc + ).toEqual("agrement without legal"); + }); + + it.each(["NOTHING", "CDT", "UNFAVOURABLE"])("should throw a contentNotHandledWithoutLegal", async (contentType) => { + mockContribution.contentType = contentType; + mockContribution.idcc = "1234"; + + const messageBloc = await generateMessageBlock(mockedContributionGeneric, mockContribution); + + expect( + messageBloc + ).toEqual("content not handled without legal"); + }); +}) \ No newline at end of file From 6d2213bc8dd312dfced75fdcbcd4d6eb68befdb8 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 3 Jun 2024 16:40:10 +0200 Subject: [PATCH 7/7] fix: TU --- .../contributions/__tests__/generateMessageBlock.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts index e97b6084e..78625f11f 100644 --- a/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts +++ b/targets/export-elasticsearch/src/ingester/contributions/__tests__/generateMessageBlock.test.ts @@ -18,8 +18,6 @@ const mockContribution: any = { }; describe("generateMessageBlock", () => { - - beforeEach(() => { jest.clearAllMocks(); }); @@ -159,6 +157,7 @@ describe("Tests avec une contribution generic no cdt", () => { contentAgreementWithoutLegal: "agrement without legal", contentNotHandledWithoutLegal: "content not handled without legal" }); + (fetchAgreementMessage as jest.Mock).mockResolvedValue(undefined); }) it.each(["ANSWER", "SP"])("should throw a contentAgreementWithoutLegal", async (contentType) => {