Skip to content

Commit

Permalink
feat: implement message bloc no cdt (#1394)
Browse files Browse the repository at this point in the history
* implement message block no cdt

* feat: ajout des nouveaux messagesà l'admin

* refactor: bouger contrib generic au dessus

* chore: add TU

* refactor: use generic in all generate

* fix: TI

* fix: TU

---------

Co-authored-by: Victor <[email protected]>
  • Loading branch information
Viczei and Victor authored Jun 4, 2024
1 parent 32e2030 commit 305cb46
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 47 deletions.
2 changes: 2 additions & 0 deletions shared/types/src/hasura/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export type ContributionMessageBlock = {
id: string;
label: string;
contentAgreement: string;
contentAgreementWithoutLegal: string;
contentLegal: string;
contentNotHandled: string;
contentNotHandledWithoutLegal: string;
};

export type ContributionAgreementMessage = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("generateContent", () => {
};

const result: ContributionContent = await generateContent(
mockContributions,
mockContributions.find(({id}) => id === contribution.genericAnswerId),
contribution
);

Expand All @@ -55,7 +55,7 @@ describe("generateContent", () => {
};

const result: ContributionContent = await generateContent(
mockContributions,
mockContributions.find(({id}) => id === contribution.genericAnswerId),
contribution
);

Expand All @@ -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)`
);
Expand All @@ -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"
);
Expand All @@ -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
);

Expand Down Expand Up @@ -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
);

Expand All @@ -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]'
);
Expand All @@ -171,7 +171,7 @@ describe("generateContent", () => {
};

const result: ContributionContent = await generateContent(
mockContributions,
mockContributions.find(({id}) => id === contribution.genericAnswerId),
contribution
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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";

jest.mock("../fetchMessageBlock");
jest.mock("../fetchAgreementMessage");

describe("generateMessageBlock", () => {
const mockContribution: any = {
questionId: "123",
contentType: "ANSWER",
idcc: "1234",
};
const contributionGeneric: DocumentElasticWithSource<ContributionDocumentJson> | undefined = undefined;
const mockContribution: any = {
questionId: "123",
contentType: "ANSWER",
idcc: "1234",
};

describe("generateMessageBlock", () => {
beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -36,6 +41,7 @@ describe("generateMessageBlock", () => {

mockContribution.contentType = contentType as ContributionContentType;
const result: string | undefined = await generateMessageBlock(
contributionGeneric,
mockContribution
);

Expand Down Expand Up @@ -63,6 +69,7 @@ describe("generateMessageBlock", () => {
mockContribution.contentType = contentType as ContributionContentType;
mockContribution.idcc = "0000";
const result: string | undefined = await generateMessageBlock(
contributionGeneric,
mockContribution
);

Expand All @@ -75,24 +82,25 @@ describe("generateMessageBlock", () => {
mockContribution.contentType = "UNKNOWN_TYPE";
mockContribution.idcc = "1234";

await expect(generateMessageBlock(mockContribution)).rejects.toThrowError(
"Unknown content type"
);
await expect(
generateMessageBlock(contributionGeneric, 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(contributionGeneric, 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(
contributionGeneric,
mockContribution
);

Expand All @@ -107,10 +115,70 @@ describe("generateMessageBlock", () => {
);

const result: string | undefined = await generateMessageBlock(
contributionGeneric,
mockContribution
);

expect(fetchAgreementMessage).toHaveBeenCalledWith("1234");
expect(result).toEqual("fetchedAgreementMessage");
});



});
describe("Tests avec une contribution generic no cdt", () => {
let mockedContributionGeneric: DocumentElasticWithSource<ContributionDocumentJson> | 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"
});
(fetchAgreementMessage as jest.Mock).mockResolvedValue(undefined);
})
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");
});
})
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("generateReferences", () => {
},
];

const result = generateReferences(contributions, contrib);
const result = generateReferences(contributions[0], contrib);

expect(result).toEqual([
{
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("generateReferences", () => {
],
};

const result = generateReferences([], contrib);
const result = generateReferences(undefined, contrib);

expect(result).toEqual([
{
Expand All @@ -92,7 +92,7 @@ describe("generateReferences", () => {
const contrib: any = { contentType: "CDT", questionIndex: 1 };

expect(() => {
generateReferences([], contrib);
generateReferences(undefined, contrib);
}).toThrowError();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ query get_question($id: uuid!) {
contentAgreement
contentLegal
contentNotHandled
contentAgreementWithoutLegal
contentNotHandledWithoutLegal
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ 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 content = await generateContent(contribGeneric, contrib);

const messageBlock = await generateMessageBlock(contrib);
const messageBlock = await generateMessageBlock(contribGeneric, contrib);

const references = generateReferences(contributions, contrib);
const references = generateReferences(contribGeneric, contrib);

const ccUnextended = await fetchAgreementUnextended();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { fetchFicheSp } from "./fetchFicheSp";

export const generateContent = async (
contributions: DocumentElasticWithSource<ContributionDocumentJson>[],
contribGeneric: DocumentElasticWithSource<ContributionDocumentJson> | undefined,
contrib: DocumentElasticWithSource<ContributionDocumentJson>
): Promise<ContributionContent> => {
switch (contrib.type) {
Expand All @@ -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,
Expand All @@ -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}]`
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { fetchMessageBlock } from "./fetchMessageBlock";
import { fetchAgreementMessage } from "./fetchAgreementMessage";

export const generateMessageBlock = async (
contribGeneric: DocumentElasticWithSource<ContributionDocumentJson> | undefined,
contrib: DocumentElasticWithSource<ContributionDocumentJson>
): Promise<string | undefined> => {
const agreementMessage = await fetchAgreementMessage(contrib.idcc);
Expand All @@ -21,12 +22,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");
Expand Down
Loading

0 comments on commit 305cb46

Please sign in to comment.