Skip to content

Commit

Permalink
fix(contributions): desactive la logique sur les CCs qui ne prévoit r…
Browse files Browse the repository at this point in the history
…ien qui renvoie vers le CDT qui prévoit rien (#1230)

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test
  • Loading branch information
maxgfr authored Jan 17, 2024
1 parent f8843ee commit 85b4e72
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 140 deletions.
1 change: 0 additions & 1 deletion shared/types/export/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export interface ContributionConventionnelInfos {

export interface ContributionGenericInfos {
ccSupported: string[];
ccSupportedNoContent?: string[];
}

export interface ContributionMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("generateContent", () => {
await expect(
generateContent(mockContributions, contribution)
).rejects.toThrowError(
'La contribution [2 - 1516] ne peut pas être de type "Code du travail" parce que la générique n\'a pas de réponse'
"La contribution [2 - 1516] ne peut pas référencer une générique qui n'a pas de réponse"
);
});

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { addGlossaryToContent } from "./addGlossaryToContent";
import { generateMessageBlock } from "./generateMessageBlock";
import { generateLinkedContent } from "./generateLinkedContent";
import pMap from "p-map";
import { getCcSupportedWithNoContent } from "./getCcSupportedWithNoContent";
import { generateReferences } from "./generateReferences";

export type ContributionElasticDocumentLightRelatedContent = Omit<
Expand Down Expand Up @@ -69,9 +68,6 @@ export async function generateContributions(
doc = {
ccSupported: getCcSupported(contributions, contrib),
};
if (contrib.type === "generic-no-cdt") {
doc.ccSupportedNoContent = await getCcSupportedWithNoContent(contrib);
}
} else {
doc = {
...getCcInfos(ccnData, contrib),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const generateContent = async (
}
if (cdtContrib.type === "generic-no-cdt") {
throw new Error(
`La contribution [${contrib.questionIndex} - ${contrib.idcc}] ne peut pas être de type "Code du travail" parce que la générique n'a pas de réponse`
`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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const generateLinkedContent = async (
}
if (genericContribution.type === "generic-no-cdt") {
throw new Error(
`La contribution [${currentContribution.questionIndex} - ${currentContribution.idcc}] ne peut pas être de type "Code du travail" parce que la générique n'a pas de réponse`
`La contribution [${currentContribution.questionIndex} - ${currentContribution.idcc}] ne peut pas référencer une générique qui n'a pas de réponse`
);
}
const genericLinkedContent = await generateLinkedContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const generateReferences = (
}
if (cdtContrib.type === "generic-no-cdt") {
throw new Error(
`La contribution [${contrib.questionIndex} - ${contrib.idcc}] ne peut pas être de type "Code du travail" parce que la générique n'a pas de réponse`
`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];
Expand Down

This file was deleted.

14 changes: 11 additions & 3 deletions targets/frontend/src/components/contributions/answers/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TooltipProps,
Typography,
} from "@mui/material";
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import { useUser } from "src/hooks/useUser";

import { StatusContainer } from "../status";
Expand All @@ -22,6 +22,7 @@ import { Breadcrumb, BreadcrumbLink } from "src/components/utils";
import { AnswerForm } from "./AnswerForm";
import { fr } from "@codegouvfr/react-dsfr";
import { usePublishContributionMutation } from "./usePublishAnswer";
import { useGenericContributionAnswerQuery } from "./answerGeneric.query";

export type ContributionsAnswerProps = {
id: string;
Expand All @@ -31,6 +32,9 @@ export const ContributionsAnswer = ({
id,
}: ContributionsAnswerProps): JSX.Element => {
const answer = useContributionAnswerQuery({ id });
const genericAnswer = useGenericContributionAnswerQuery({
questionId: answer?.questionId,
});
const { user } = useUser() as any;
const updateAnswer = useContributionAnswerUpdateMutation();
const [snack, setSnack] = useState<{
Expand Down Expand Up @@ -124,8 +128,12 @@ export const ContributionsAnswer = ({
</Stack>
<Stack direction="row">
<Box sx={{ width: "70%" }}>
{answer && (
<AnswerForm answer={answer} onSubmit={onSubmit}></AnswerForm>
{answer && genericAnswer && (
<AnswerForm
answer={answer}
genericAnswerContentType={genericAnswer.contentType}
onSubmit={onSubmit}
/>
)}
</Box>
<Box sx={{ width: "30%" }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, FormControl, Stack } from "@mui/material";
import { Button, FormControl, Stack, Alert } from "@mui/material";
import React, { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
Expand Down Expand Up @@ -79,6 +79,7 @@ export type AnswerFormValidation = z.infer<typeof answerFormSchema>;

export type ContributionsAnswerProps = {
answer: AnswerWithStatus;
genericAnswerContentType: Answer["contentType"];
onSubmit: (status: Status, data: Answer) => Promise<void>;
};

Expand All @@ -88,6 +89,7 @@ const isCodeDuTravail = (answer: Answer): boolean =>
export const AnswerForm = ({
answer,
onSubmit,
genericAnswerContentType,
}: ContributionsAnswerProps): JSX.Element => {
const [status, setStatus] = useState<Status>("TODO");
const router = useRouter();
Expand All @@ -97,6 +99,7 @@ export const AnswerForm = ({
setStatus(answer.status.status);
}
}, [answer]);
const isAGenericWithNoCdt = genericAnswerContentType === "GENERIC_NO_CDT";

const {
control,
Expand Down Expand Up @@ -176,15 +179,18 @@ export const AnswerForm = ({
{
label: "La convention collective ne prévoit rien",
value: "NOTHING",
isDisabled: isAGenericWithNoCdt,
},
{
label: "La convention collective renvoie au Code du Travail",
value: "CDT",
isDisabled: isAGenericWithNoCdt,
},
{
label:
"La convention collective intégralement moins favorable que le CDT",
value: "UNFAVOURABLE",
isDisabled: isAGenericWithNoCdt,
},
{
label: "Nous n'avons pas la réponse",
Expand All @@ -204,6 +210,12 @@ export const AnswerForm = ({
return (
<form>
<Stack spacing={5}>
{isAGenericWithNoCdt && answer.agreementId !== "0000" && (
<Alert severity="info">
La contribution générique est de type `Le code du travail ne prévoit
rien`
</Alert>
)}
<FormControl>
<FormTextField
name="updateDate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ const onSubmit = jest.fn(() => Promise.resolve());

describe("AnswerForm", () => {
test("Vérifier l'affichage du contenu", () => {
render(<AnswerForm answer={answerBase} onSubmit={onSubmit} />);
render(
<AnswerForm
answer={answerBase}
genericAnswerContentType={"ANSWER"}
onSubmit={onSubmit}
/>
);
expect(screen.queryByText("content")).toBeInTheDocument();

const spInput = screen.getByLabelText("Fiche service-public");
Expand Down Expand Up @@ -177,7 +183,11 @@ describe("AnswerForm", () => {

test("Vérifier l'affichage des options", () => {
const { rerender } = render(
<AnswerForm answer={answerBase} onSubmit={onSubmit} />
<AnswerForm
answer={answerBase}
genericAnswerContentType={"ANSWER"}
onSubmit={onSubmit}
/>
);
expect(screen.queryByText("Afficher la réponse")).toBeInTheDocument();
expect(
Expand All @@ -192,6 +202,7 @@ describe("AnswerForm", () => {

rerender(
<AnswerForm
genericAnswerContentType={"ANSWER"}
answer={{
...answerBase,
agreementId: "0016",
Expand All @@ -218,15 +229,27 @@ describe("AnswerForm", () => {
});

test("Vérifier que la sauvegarde fonctionne", async () => {
render(<AnswerForm answer={answerBase} onSubmit={onSubmit} />);
render(
<AnswerForm
genericAnswerContentType={"ANSWER"}
answer={answerBase}
onSubmit={onSubmit}
/>
);
fireEvent.click(screen.getByText("Sauvegarder"));
await waitFor(() => {
expect(onSubmit).toHaveBeenCalledTimes(1);
});
});

test("Vérifier l'affichage du message d'erreur pour les fiches SP", async () => {
render(<AnswerForm answer={answerBase} onSubmit={onSubmit} />);
render(
<AnswerForm
genericAnswerContentType={"ANSWER"}
answer={answerBase}
onSubmit={onSubmit}
/>
);
fireEvent.click(screen.getByText("Utiliser la fiche service public"));
userEvent.clear(screen.getByLabelText("Fiche service-public"));
fireEvent.click(screen.getByText("Sauvegarder"));
Expand All @@ -236,7 +259,13 @@ describe("AnswerForm", () => {
});

test("Vérifier l'affichage du message d'erreur pour les références", async () => {
render(<AnswerForm answer={answerBase} onSubmit={onSubmit} />);
render(
<AnswerForm
genericAnswerContentType={"ANSWER"}
answer={answerBase}
onSubmit={onSubmit}
/>
);
fireEvent.click(screen.getByText("Ajouter une référence"));
fireEvent.click(screen.getByText("Sauvegarder"));
const [labelRequired] = await screen.findAllByText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const useContributionAnswerQuery = ({
},
context,
});

if (
!result?.data?.contribution_answers ||
!result?.data?.contribution_answers?.length
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useQuery } from "urql";
import { AnswerWithStatus } from "./answer.query";

const contributionGenericAnswerQuery = `
query contribution_answer($questionId: uuid) {
contribution_answers(where: {question_id: {_eq: $questionId}, agreement_id: {_eq: "0000"}}) {
contentType: content_type
}
}
`;

type QueryProps = {
questionId?: string;
};

type QueryResult = {
contribution_answers: AnswerWithStatus[];
};

export const useGenericContributionAnswerQuery = ({
questionId,
}: QueryProps): Pick<AnswerWithStatus, "contentType"> | undefined => {
const [resultGeneric] = useQuery<QueryResult>({
query: contributionGenericAnswerQuery,
variables: {
questionId,
},
});
const genericAnswer = resultGeneric.data?.contribution_answers[0];
if (!genericAnswer) {
return;
}
return genericAnswer;
};
5 changes: 3 additions & 2 deletions targets/frontend/src/components/forms/RadioGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TitleBox } from "../TitleBox";
type OptionProps = {
label: string;
value: string;
isDisabled?: boolean;
};

export type RadioGroupProps = PropsWithChildren<
Expand Down Expand Up @@ -47,13 +48,13 @@ export const FormRadioGroup = ({
}
name={name}
>
{options.map(({ label, value }) => (
{options.map(({ label, value, isDisabled }) => (
<FormControlLabel
key={value}
value={value}
control={<Radio id={`${name}.${value}`} />}
label={label}
disabled={disabled}
disabled={isDisabled || disabled}
htmlFor={`${name}.${value}`}
/>
))}
Expand Down
Loading

0 comments on commit 85b4e72

Please sign in to comment.