From 2e3947f1dc432e2d0e2e2deb05f7c94b9082178c Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 7 Sep 2024 01:34:56 +0200 Subject: [PATCH] =?UTF-8?q?Montrer=20les=20stats=20sur=20les=20amendement?= =?UTF-8?q?=20propos=C3=A9=20et=20cosign=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amendements/AmendementsStatistics.tsx | 106 ++++++++++++++++++ app/depute/[slug]/amendements/page.tsx | 5 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 app/depute/[slug]/amendements/AmendementsStatistics.tsx diff --git a/app/depute/[slug]/amendements/AmendementsStatistics.tsx b/app/depute/[slug]/amendements/AmendementsStatistics.tsx new file mode 100644 index 0000000..57ea4ea --- /dev/null +++ b/app/depute/[slug]/amendements/AmendementsStatistics.tsx @@ -0,0 +1,106 @@ +import React from "react"; + +import Stack from "@mui/material/Stack"; + +import { prisma } from "@/prisma"; + +async function getDeputeAmendementStatsUnCached(uid: string) { + try { + const etatAmendementsCosignes = await prisma.amendement.groupBy({ + by: ["sortAmendement", "etatLibelle"], + where: { + coSignataires: { + some: { + acteurRefUid: uid, + }, + }, + }, + _count: { uid: true }, + }); + + const etatAmendementsProposes = await prisma.amendement.groupBy({ + by: ["sortAmendement", "etatLibelle"], + where: { + acteurRefUid: uid, + }, + _count: { uid: true }, + }); + + return { etatAmendementsCosignes, etatAmendementsProposes }; + } catch (error) { + console.error(`Error fetching amendement from depute ${uid}:`, error); + throw error; + } +} + +const getDeputeStatsAmendement = React.cache(getDeputeAmendementStatsUnCached); + +const sorts = ["Adopté", "Non soutenu", "Rejeté", "Retiré", "Tombé"]; +const etats = ["A discuter", "En traitement", "Irrecevable", "Irrecevable 40"]; + +export async function AmendementsStatistics({ + deputeUid, +}: { + deputeUid: string; +}) { + const { etatAmendementsCosignes, etatAmendementsProposes } = + await getDeputeStatsAmendement(deputeUid); + + const nbCoSigne = etatAmendementsCosignes.reduce<{ [key: string]: number }>( + (acc, item) => { + if (item.etatLibelle && etats.includes(item.etatLibelle)) { + const nb = item._count.uid; + return { ...acc, [item.etatLibelle]: nb, total: (acc.total ?? 0) + nb }; + } + if (item.sortAmendement && sorts.includes(item.sortAmendement)) { + const nb = item._count.uid; + return { + ...acc, + [item.sortAmendement]: nb, + total: (acc.total ?? 0) + nb, + }; + } + return acc; + }, + {} + ); + + const nbPropose = etatAmendementsProposes.reduce<{ [key: string]: number }>( + (acc, item) => { + if (item.etatLibelle && etats.includes(item.etatLibelle)) { + const nb = item._count.uid; + return { ...acc, [item.etatLibelle]: nb, total: (acc.total ?? 0) + nb }; + } + if (item.sortAmendement && sorts.includes(item.sortAmendement)) { + const nb = item._count.uid; + return { + ...acc, + [item.sortAmendement]: nb, + total: (acc.total ?? 0) + nb, + }; + } + return acc; + }, + {} + ); + return ( + + + + + + + + + + {[...sorts, ...etats].map((state) => ( + + + + + + ))} + +
Etatproposéssignés
{state}{nbPropose[state] ?? 0}{nbCoSigne[state] ?? 0}
+ ); +} diff --git a/app/depute/[slug]/amendements/page.tsx b/app/depute/[slug]/amendements/page.tsx index 786d03d..ddf95f9 100644 --- a/app/depute/[slug]/amendements/page.tsx +++ b/app/depute/[slug]/amendements/page.tsx @@ -4,12 +4,14 @@ import Stack from "@mui/material/Stack"; import AmendementCard from "@/components/folders/AmendementCard"; import { prisma } from "@/prisma"; +import { AmendementsStatistics } from "./AmendementsStatistics"; async function getDeputeAmendementUnCached(slug: string) { try { return await prisma.acteur.findFirst({ where: { slug }, select: { + uid: true, amendements: { include: { texteLegislatifRef: { select: { numNotice: true } } }, }, @@ -30,11 +32,12 @@ export default async function Amendements({ }) { const deputeWithAmendements = await getDeputeAmendement(params.slug); - const { amendements } = deputeWithAmendements!; + const { amendements, uid } = deputeWithAmendements!; return (

Amendements

+ {amendements && amendements .sort((a, b) =>