Skip to content

Commit

Permalink
fix: taux de transformation page pilotage de la réforme (#507)
Browse files Browse the repository at this point in the history
* fix: taux de transformation page pilotage de la réforme

* fix: nom de fichier d'export trop long

* fix: ajout des enums de demande

---------

Co-authored-by: Gaetan BUSATO <[email protected]>
  • Loading branch information
LucasDetre and gBusato authored Dec 16, 2024
1 parent db3e04d commit 4ae9228
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sql } from "kysely";
import { NEXT_RENTREE } from "shared/time/NEXT_RENTREE";

import { getKbdClient } from "@/db/db";
import { getMillesimeFromRentreeScolaire } from "@/modules/data/services/getMillesime";
Expand All @@ -13,12 +12,13 @@ import {
notHistoriqueIndicateurRegionSortie,
} from "@/modules/data/utils/notHistorique";
import { genericOnConstatRentree } from "@/modules/data/utils/onConstatDeRentree";
import { genericOnDemandes } from "@/modules/data/utils/onDemande";
import { selectTauxInsertion6moisAgg } from "@/modules/data/utils/tauxInsertion6mois";
import { selectTauxPoursuiteAgg } from "@/modules/data/utils/tauxPoursuite";
import { countPlacesTransformeesParCampagne } from "@/modules/utils/countCapacite";
import { isDemandeNotAjustementRentree, isDemandeNotDeletedOrRefused } from "@/modules/utils/isDemandeSelectable";
import { cleanNull } from "@/utils/noNull";

import type { Filters } from "./getPilotageReformeStats.usecase";

const getRentreesScolaires = async () => {
return await getKbdClient()
.selectFrom("indicateurEntree")
Expand Down Expand Up @@ -167,34 +167,19 @@ const findFiltersInDb = async () => {
};
};

const getTauxTransformationData = async (filters: { codeNiveauDiplome?: string[]; codeRegion?: string }) => {
const getTauxTransformationData = async (filters: Filters) => {
return getKbdClient()
.selectFrom("latestDemandeIntentionView as demande")
.leftJoin("dataFormation", "dataFormation.cfd", "demande.cfd")
.leftJoin("dataEtablissement", "dataEtablissement.uai", "demande.uai")
.leftJoin("campagne", "campagne.id", "demande.campagneId")
.selectFrom(genericOnDemandes({ ...filters, rentreeScolaire: ["2025"] }).as("demande"))
.leftJoin(
genericOnConstatRentree(filters)
.select((eb) => [sql<number>`SUM(${eb.ref("constatRentree.effectif")})`.as("effectif")])
.as("effectifs"),
(join) => join.onTrue()
)
.select((eb) => [
eb.fn.coalesce(eb.fn.sum<number>(countPlacesTransformeesParCampagne(eb)), sql`0`).as("transformes"),
genericOnConstatRentree({ ...filters })
.select((eb) => eb.fn.coalesce(eb.fn.sum<number>("effectif"), eb.val(0)).as("effectif"))
.as("effectif"),
eb.fn.coalesce("effectifs.effectif", eb.val(0)).as("effectif"),
eb.fn.coalesce("placesTransformees", eb.val(0)).as("placesTransformees"),
])
.where(isDemandeNotDeletedOrRefused)
.where(isDemandeNotAjustementRentree)
.$call((eb) => {
return eb.where("demande.rentreeScolaire", "in", [parseInt(NEXT_RENTREE)]);
})
.$call((eb) => {
if (filters.codeNiveauDiplome)
return eb.where("dataFormation.codeNiveauDiplome", "in", filters.codeNiveauDiplome);
return eb;
})
.$call((eb) => {
if (filters.codeRegion) {
return eb.where("dataEtablissement.codeRegion", "=", filters.codeRegion);
}
return eb;
})
.execute()
.then(cleanNull);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import type { getPilotageReformeStatsSchema } from "shared/routes/schemas/get.pilotage-reforme.stats.schema";
import type { z } from "zod";

import { getCurrentCampagneQuery } from "@/modules/data/queries/getCurrentCampagne/getCurrentCampagne.query";
import { formatTauxTransformation } from "@/modules/data/utils/formatTauxTransformation";

import { dependencies } from "./dependencies";

export interface Filters extends z.infer<typeof getPilotageReformeStatsSchema.querystring> {
campagne: string;
}
const getPilotageReformeStatsFactory =
(
deps = {
getCurrentCampagneQuery,
getStats: dependencies.getStats,
findFiltersInDb: dependencies.findFiltersInDb,
getTauxTransformationData: dependencies.getTauxTransformationData,
}
) =>
async (activeFilters: { codeNiveauDiplome?: string[]; orderBy?: { order: "asc" | "desc"; column: string } }) => {
const currentCampagne = await deps.getCurrentCampagneQuery();
const anneeCampagne = currentCampagne.annee;
const [stats, filters] = await Promise.all([deps.getStats(activeFilters), deps.findFiltersInDb()]);

const [{ transformes, effectif }] = await deps.getTauxTransformationData(activeFilters);
const [{ placesTransformees, effectif }] = await deps.getTauxTransformationData({
...activeFilters,
campagne: anneeCampagne,
});

const tauxTransformation = formatTauxTransformation(transformes, effectif);
const tauxTransformation = formatTauxTransformation(placesTransformees, effectif);

return {
...stats,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sql } from "kysely";
import { ScopeEnum } from "shared";
import type { getStatsPilotageIntentionsSchema } from "shared/routes/schemas/get.pilotage-intentions.stats.schema";
import type z from "zod";
import type { z } from "zod";

import { getKbdClient } from "@/db/db";
import {
Expand Down
6 changes: 6 additions & 0 deletions server/src/modules/data/utils/onDemande.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expressionBuilder, sql } from "kysely";
import type { DemandeStatutType } from "shared/enum/demandeStatutEnum";
import { DemandeStatutEnum } from "shared/enum/demandeStatutEnum";
import { DemandeTypeEnum } from "shared/enum/demandeTypeEnum";
import { CURRENT_ANNEE_CAMPAGNE } from "shared/time/CURRENT_ANNEE_CAMPAGNE";
import { getMillesimeFromCampagne } from "shared/time/millesimes";
Expand Down Expand Up @@ -28,6 +29,7 @@ import {
countPlacesTransformeesParCampagne,
} from "@/modules/utils/countCapacite";
import { isDemandeProjetOrValidee } from "@/modules/utils/isDemandeProjetOrValidee";
import { isDemandeNotAjustementRentree } from "@/modules/utils/isDemandeSelectable";

import { isInPerimetreIJDataEtablissement } from "./isInPerimetreIJ";

Expand All @@ -43,6 +45,7 @@ export const genericOnDemandes = ({
codeAcademie,
codeDepartement,
withColoration,
withAjustementRentree = true,
}: {
statut?: Array<DemandeStatutType>;
rentreeScolaire?: string[];
Expand All @@ -55,6 +58,7 @@ export const genericOnDemandes = ({
codeAcademie?: string;
codeDepartement?: string;
withColoration?: string;
withAjustementRentree?: boolean;
}) =>
expressionBuilder<DB, keyof DB>()
.selectFrom("latestDemandeIntentionView as demande")
Expand Down Expand Up @@ -117,6 +121,8 @@ export const genericOnDemandes = ({
eb.fn.sum<number>(countPlacesTransformeesParCampagne(eb)).as("placesTransformees"),
])
.where(isInPerimetreIJDataEtablissement)
.$if(withAjustementRentree, (eb) => eb.where(isDemandeNotAjustementRentree))
.where("demande.statut", "in", [DemandeStatutEnum["projet de demande"], DemandeStatutEnum["demande validée"]])
.$call((eb) => {
if (campagne) return eb.where("campagne.annee", "=", campagne);
return eb;
Expand Down
15 changes: 4 additions & 11 deletions ui/app/(wrapped)/console/etablissements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Icon } from "@iconify/react";
import { useRouter, useSearchParams } from "next/navigation";
import { usePlausible } from "next-plausible";
import qs from "qs";
import { Fragment, useContext, useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";

import { client } from "@/api.client";
import { CreateRequeteEnregistreeModal } from "@/app/(wrapped)/console/components/CreateRequeteEnregistreeModal";
Expand Down Expand Up @@ -218,11 +218,7 @@ export default function Etablissements() {

const { columns, etablissements } = getDataForExport(data);

downloadCsv(
formatExportFilename("etablissement_export", isFiltered ? filters : undefined),
etablissements,
columns
);
downloadCsv(formatExportFilename("etablissement_export"), etablissements, columns);
};

const onExportExcel = async (isFiltered?: boolean) => {
Expand All @@ -233,11 +229,7 @@ export default function Etablissements() {

const { columns, etablissements } = getDataForExport(data);

downloadExcel(
formatExportFilename("etablissement_export", isFiltered ? filters : undefined),
etablissements,
columns
);
downloadExcel(formatExportFilename("etablissement_export"), etablissements, columns);
};

const [colonneFilters, setColonneFilters] = useState<(keyof typeof FORMATION_ETABLISSEMENT_COLUMNS)[]>(
Expand Down Expand Up @@ -357,6 +349,7 @@ export default function Etablissements() {
filters.uai = uaisFilter;
setSearchParams({ filters: filters });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
Expand Down
12 changes: 2 additions & 10 deletions ui/app/(wrapped)/console/formations/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,7 @@ export default function Formations() {

const filteredColumns = canShowQuadrantPosition ? columns : _.omit(columns, "positionQuadrant");

downloadCsv(
formatExportFilename("formation_export", isFiltered ? filters : undefined),
formations,
filteredColumns
);
downloadCsv(formatExportFilename("formation_export"), formations, filteredColumns);
};

const onExportExcel = async (isFiltered?: boolean) => {
Expand All @@ -238,11 +234,7 @@ export default function Formations() {

const filteredColumns = canShowQuadrantPosition ? columns : _.omit(columns, "positionQuadrant");

downloadExcel(
formatExportFilename("formation_export", isFiltered ? filters : undefined),
formations,
filteredColumns
);
downloadExcel(formatExportFilename("formation_export"), formations, filteredColumns);
};

const canShowQuadrantPosition = filters.codeRegion?.length === 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Header = ({
query: isFiltered ? getIntentionsQueryParameters() : {},
});
downloadCsv(
formatExportFilename("recueil_demandes", isFiltered ? activeFilters : {}),
formatExportFilename("recueil_demandes"),
[
...data.intentions.map((intention) => ({
...intention,
Expand All @@ -83,7 +83,7 @@ export const Header = ({
query: isFiltered ? getIntentionsQueryParameters() : {},
});
downloadExcel(
formatExportFilename("recueil_demandes", isFiltered ? activeFilters : {}),
formatExportFilename("recueil_demandes"),
[
...data.intentions.map((intention) => ({
...intention,
Expand Down
6 changes: 4 additions & 2 deletions ui/app/(wrapped)/intentions/restitution/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export default () => {
query: isFiltered ? getIntentionsStatsQueryParameters() : {},
});
downloadCsv(
formatExportFilename("restitution_export", isFiltered ? filters : undefined),
formatExportFilename("restitution_export"),
data.demandes.map((demande) => ({
...demande,
createdAt: new Date(demande.createdAt).toLocaleDateString("fr-FR", {
Expand Down Expand Up @@ -318,7 +318,7 @@ export default () => {
query: isFiltered ? getIntentionsStatsQueryParameters() : {},
});
downloadExcel(
formatExportFilename("restitution_export", isFiltered ? filters : undefined),
formatExportFilename("restitution_export"),

data.demandes.map((demande) => ({
...demande,
Expand Down Expand Up @@ -358,6 +358,7 @@ export default () => {

useEffect(() => {
setDefaultFilters();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const onClickSearch = () => {
Expand All @@ -371,6 +372,7 @@ export default () => {
useEffect(() => {
const campagneFilterNumber = parseInt(searchParams.filters?.campagne ?? "");
handleFilters("rentreeScolaire", campagneFilterNumber + 1);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchParams.filters?.campagne]);

return (
Expand Down
12 changes: 2 additions & 10 deletions ui/app/(wrapped)/intentions/saisie/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,15 @@ export const Header = ({
const data = await client.ref("[GET]/demandes").query({
query: isFiltered ? getDemandesQueryParameters() : {},
});
downloadCsv(
formatExportFilename("recueil_demandes", isFiltered ? activeFilters : undefined),
data.demandes,
DEMANDES_COLUMNS
);
downloadCsv(formatExportFilename("recueil_demandes"), data.demandes, DEMANDES_COLUMNS);
};

const onExportExcel = async (isFiltered?: boolean) => {
trackEvent("saisie_demandes:export-excel");
const data = await client.ref("[GET]/demandes").query({
query: isFiltered ? getDemandesQueryParameters() : {},
});
downloadExcel(
formatExportFilename("recueil_demandes", isFiltered ? activeFilters : undefined),
data.demandes,
DEMANDES_COLUMNS
);
downloadExcel(formatExportFilename("recueil_demandes"), data.demandes, DEMANDES_COLUMNS);
};
return (
<Flex gap={2} mb={2}>
Expand Down

0 comments on commit 4ae9228

Please sign in to comment.