Skip to content

Commit

Permalink
Merge pull request #117 from mission-apprentissage/demande-opti
Browse files Browse the repository at this point in the history
feat: BCN update
  • Loading branch information
flodlc authored Oct 12, 2023
2 parents e3824d0 + 7aa73f1 commit a7dfdfd
Show file tree
Hide file tree
Showing 17 changed files with 2,216 additions and 1,946 deletions.
979 changes: 512 additions & 467 deletions server/public/files/nFormationDiplome_.csv

Large diffs are not rendered by default.

2,823 changes: 1,475 additions & 1,348 deletions server/public/files/nMef.csv

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions server/src/modules/core/services/slack/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const sendToSlack = async (
});

const sent = await slack.client.chat.postMessage({
text: "",
blocks: main,
channel: config.slack.chanel,
});
if (!answer) return;
await slack.client.chat.postMessage({
text: "",
blocks: answer,
thread_ts: sent.ts,
channel: config.slack.chanel,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { kdb } from "../../../db/db";
import { cleanNull } from "../../../utils/noNull";

export const findOneSimilarDemande = ({
cfd,
uai,
dispositifId,
rentreeScolaire,
notId,
}: {
cfd: string;
uai: string;
dispositifId: string;
rentreeScolaire: number;
notId?: string;
}) =>
kdb
.selectFrom("demande")
.selectAll()
.where("cfd", "=", cfd)
.where("uai", "=", uai)
.where("dispositifId", "=", dispositifId)
.where("rentreeScolaire", "=", rentreeScolaire)
.$call((q) => {
if (!notId) return q;
return q.where("id", "!=", notId);
})
.limit(1)
.executeTakeFirst()
.then(cleanNull);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const valideDeps = {
codeRegion: "codeRegion",
createurId: "user-id",
} as AwaitedResult<Deps["findOneDemande"]>),
};
findOneSimilarDemande: () => Promise.resolve(),
} as Deps;

const demande = {
id: "demande-id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RequestUser } from "../../../core/model/User";
import { findOneDataEtablissement } from "../../repositories/findOneDataEtablissement.query";
import { findOneDataFormation } from "../../repositories/findOneDataFormation.query";
import { findOneDemande } from "../../repositories/findOneDemande.query";
import { findOneSimilarDemande } from "../../repositories/findOneSimilarDemande.query";
import { generateId } from "../../utils/generateId";
import { createDemandeQuery } from "./createDemandeQuery.dep";

Expand Down Expand Up @@ -55,6 +56,7 @@ export const [submitDemande, submitDemandeFactory] = inject(
findOneDataEtablissement,
findOneDataFormation,
findOneDemande,
findOneSimilarDemande,
},
(deps) =>
async ({
Expand Down Expand Up @@ -84,6 +86,21 @@ export const [submitDemande, submitDemandeFactory] = inject(
});
if (!isAllowed) throw Boom.forbidden();

const sameDemande = await deps.findOneSimilarDemande({
...demande,
notId: demande.id,
});
if (sameDemande) {
logger.info("Demande similaire existante", { sameDemande, demande });
throw Boom.badRequest("Demande similaire existante", {
id: sameDemande.id,
errors: {
same_demande:
"Une demande similaire existe avec ces mêmes champs: code diplôme, numéro établissement, dispositif et rentrée scolaire.",
},
});
}

const compensationRentreeScolaire =
demande.typeDemande === "augmentation_compensation" ||
demande.typeDemande === "ouverture_compensation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RequestUser } from "../../../core/model/User";
import { findOneDataEtablissement } from "../../repositories/findOneDataEtablissement.query";
import { findOneDataFormation } from "../../repositories/findOneDataFormation.query";
import { findOneDemande } from "../../repositories/findOneDemande.query";
import { findOneSimilarDemande } from "../../repositories/findOneSimilarDemande.query";
import { generateId } from "../../utils/generateId";
import { createDemandeQuery } from "./createDemandeQuery.dep";

Expand Down Expand Up @@ -55,6 +56,7 @@ export const [submitDraftDemande] = inject(
findOneDemande,
findOneDataEtablissement,
findOneDataFormation,
findOneSimilarDemande,
},
(deps) =>
async ({
Expand Down Expand Up @@ -84,6 +86,22 @@ export const [submitDraftDemande] = inject(
});
if (!isAllowed) throw Boom.forbidden();

const sameDemande = await deps.findOneSimilarDemande({
...demande,
notId: demande.id,
});

if (sameDemande) {
logger.info("Demande similaire existante", { sameDemande, demande });
throw Boom.badRequest("Demande similaire existante", {
id: sameDemande.id,
errors: {
same_demande:
"Une demande similaire existe avec ces mêmes champs: code diplôme, numéro établissement, dispositif et rentrée scolaire.",
},
});
}

const compensationRentreeScolaire =
demande.typeDemande === "augmentation_compensation" ||
demande.typeDemande === "ouverture_compensation"
Expand Down
2 changes: 1 addition & 1 deletion shared/demandeValidators/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const demandeValidators: Record<

if (
isPositiveNumber(demande.capaciteApprentissageActuelle) &&
demande.capaciteApprentissage >= demande.capaciteApprentissageActuelle
demande.capaciteApprentissage > demande.capaciteApprentissageActuelle
) {
return "La capacité en apprentissage devrait être inférieure à la capacité actuelle dans le cas d'une diminution";
}
Expand Down
57 changes: 36 additions & 21 deletions ui/app/(wrapped)/intentions/intentionForm/IntentionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const IntentionForm = ({

const [errors, setErrors] = useState<Record<string, string>>();

const { isLoading: isSubmitting, mutateAsync: submit } = useMutation({
const {
isLoading: isSubmittingDemande,
mutateAsync: submitDemande,
isSuccess: isDemandeSuccess,
} = useMutation({
mutationFn: (forms: IntentionForms) =>
api.submitDemande({ body: { demande: { id: formId, ...forms } } }).call(),
onSuccess: () => push("/intentions"),
Expand All @@ -53,27 +57,38 @@ export const IntentionForm = ({
},
});

const { isLoading: isDraftSubmitting, mutateAsync: submitDraft } =
useMutation({
mutationFn: (forms: IntentionForms) =>
api
.submitDraftDemande({ body: { demande: { id: formId, ...forms } } })
.call(),
onSuccess: () => push("/intentions"),
onError: (e: AxiosError<{ errors: Record<string, string> }>) => {
const errors = e.response?.data.errors;
setErrors(errors);
},
});

const { isLoading: isDeleting, mutateAsync: deleteDemande } = useMutation({
const {
isLoading: isSubmittingDraft,
mutateAsync: submitDraft,
isSuccess: isDraftSuccess,
} = useMutation({
mutationFn: (forms: IntentionForms) =>
api
.submitDraftDemande({ body: { demande: { id: formId, ...forms } } })
.call(),
onSuccess: () => push("/intentions"),
onError: (e: AxiosError<{ errors: Record<string, string> }>) => {
const errors = e.response?.data.errors;
setErrors(errors);
},
});

const {
isLoading: isDeleting,
mutateAsync: deleteDemande,
isSuccess: isDeleteSuccess,
} = useMutation({
mutationFn: async () => {
if (!formId) return;
await api.deleteDemande({ params: { id: formId } }).call();
},
onSuccess: () => push("/intentions"),
});

const isSubmitting = isSubmittingDraft || isSubmittingDemande || isDeleting;
const isSuccess = isDraftSuccess || isDeleteSuccess || isDemandeSuccess;
const isActionsDisabled = isSuccess || isSubmitting;

const [isFCIL, setIsFCIL] = useState<boolean>(
formMetadata?.formation?.isFCIL ?? false
);
Expand Down Expand Up @@ -115,7 +130,7 @@ export const IntentionForm = ({
bg="#E2E7F8"
as="form"
noValidate
onSubmit={handleSubmit((values) => submit(values))}
onSubmit={handleSubmit((values) => submitDemande(values))}
>
<Container maxW={"container.xl"} pt="4" mb={24}>
<Breadcrumb
Expand Down Expand Up @@ -165,7 +180,7 @@ export const IntentionForm = ({
color="red"
borderColor="red"
mr="auto"
isDisabled={disabled}
isDisabled={disabled || isActionsDisabled}
isLoading={isDeleting}
variant="secondary"
leftIcon={<DeleteIcon />}
Expand All @@ -177,8 +192,8 @@ export const IntentionForm = ({
)}
<Box justifyContent={"center"}>
<Button
isDisabled={disabled}
isLoading={isDraftSubmitting}
isDisabled={disabled || isActionsDisabled}
isLoading={isSubmittingDraft}
variant="secondary"
onClick={handleSubmit((values) => submitDraft(values))}
>
Expand All @@ -190,8 +205,8 @@ export const IntentionForm = ({
</Box>
<Box justifyContent={"center"}>
<Button
isDisabled={disabled}
isLoading={isSubmitting}
isDisabled={disabled || isActionsDisabled}
isLoading={isSubmittingDemande}
variant="primary"
type="submit"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
FormControl,
FormErrorMessage,
FormLabel,
Input,
NumberInput,
NumberInputField,
} from "@chakra-ui/react";
import { useEffect } from "react";
import { useFormContext } from "react-hook-form";
Expand Down Expand Up @@ -40,22 +41,23 @@ export const CapaciteApprentissageActuelleField = chakra(
isRequired
>
<FormLabel>Capacité actuelle</FormLabel>
<Input
type="number"
{...register("capaciteApprentissageActuelle", {
shouldUnregister: true,
disabled,
setValueAs: safeParseInt,
value: null as unknown as undefined,
validate: (value) => {
if (value === undefined) return "Le champ est obligatoire";
if (Number.isNaN(value))
return "Veuillez remplir un nombre valide.";
if (value < 0) return "Valeurs positives uniquement.";
return;
},
})}
/>
<NumberInput>
<NumberInputField
{...register("capaciteApprentissageActuelle", {
shouldUnregister: true,
disabled,
setValueAs: safeParseInt,
value: null as unknown as undefined,
validate: (value) => {
if (value === undefined) return "Le champ est obligatoire";
if (Number.isNaN(value))
return "Veuillez remplir un nombre valide.";
if (value < 0) return "Valeurs positives uniquement.";
return;
},
})}
/>
</NumberInput>
{errors.capaciteApprentissageActuelle && (
<FormErrorMessage>
{errors.capaciteApprentissageActuelle.message}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
FormControl,
FormErrorMessage,
FormLabel,
Input,
NumberInput,
NumberInputField,
} from "@chakra-ui/react";
import { useEffect } from "react";
import { useFormContext } from "react-hook-form";
Expand Down Expand Up @@ -42,21 +43,22 @@ export const CapaciteApprentissageColoreeField = chakra(
isRequired
>
<FormLabel>Dont places colorées</FormLabel>
<Input
type="number"
{...register("capaciteApprentissageColoree", {
shouldUnregister: true,
setValueAs: safeParseInt,
disabled,
value: null as unknown as undefined,
validate: (value) => {
if (value === undefined) return "Le champ est obligatoire";
if (Number.isNaN(value))
return "Veuillez remplir un nombre valide.";
if (value < 0) return "Valeurs positives uniquement.";
},
})}
/>
<NumberInput>
<NumberInputField
{...register("capaciteApprentissageColoree", {
shouldUnregister: true,
setValueAs: safeParseInt,
disabled,
value: null as unknown as undefined,
validate: (value) => {
if (value === undefined) return "Le champ est obligatoire";
if (Number.isNaN(value))
return "Veuillez remplir un nombre valide.";
if (value < 0) return "Valeurs positives uniquement.";
},
})}
/>
</NumberInput>
{errors.capaciteApprentissageColoree && (
<FormErrorMessage>
{errors.capaciteApprentissageColoree.message}
Expand Down
Loading

0 comments on commit a7dfdfd

Please sign in to comment.