diff --git a/src/app/(container)/medicaments/[CIS]/page.tsx b/src/app/(container)/medicaments/[CIS]/page.tsx index 2ce8f10..6e64128 100644 --- a/src/app/(container)/medicaments/[CIS]/page.tsx +++ b/src/app/(container)/medicaments/[CIS]/page.tsx @@ -11,10 +11,6 @@ import JSZIP from "jszip"; // @ts-ignore import * as windows1252 from "windows-1252"; import HTMLParser, { HTMLElement } from "node-html-parser"; -import { Nullable, sql } from "kysely"; - -import { pdbmMySQL } from "@/db/pdbmMySQL"; -import liste_CIS_MVP from "@/liste_CIS_MVP.json"; import DsfrLeafletSection from "./DsfrLeafletSection"; import { isHtmlElement } from "./leafletUtils"; import { @@ -26,18 +22,12 @@ import { } from "@/displayUtils"; import Breadcrumb from "@codegouvfr/react-dsfr/Breadcrumb"; import { - Presentation, PresentationComm, PresentationStat, - PresInfoTarif, - SpecComposant, - SpecDelivrance, - SpecElement, - Specialite, SubstanceNom, } from "@/db/pdbmMySQL/types"; import { atcData, getAtc1, getAtc2 } from "@/data/atc"; -import { notFound } from "next/navigation"; +import { getSpecialite } from "@/db/pdbmMySQL/utils"; export const dynamic = "error"; export const dynamicParams = true; @@ -54,107 +44,6 @@ export async function generateMetadata( }; } -const getSpecialite = cache(async (CIS: string) => { - if (!liste_CIS_MVP.includes(CIS)) notFound(); - - const specialite: Specialite | undefined = await pdbmMySQL - .selectFrom("Specialite") - .where("SpecId", "=", CIS) - .selectAll() - .executeTakeFirst(); - - if (!specialite) return notFound(); - - const elements: SpecElement[] = await pdbmMySQL - .selectFrom("Element") - .where("SpecId", "=", CIS) - .selectAll() - .execute(); - - const composants: Array = ( - await Promise.all( - elements.map((el) => - pdbmMySQL - .selectFrom("Composant") - .where((eb) => - eb.and([eb("SpecId", "=", CIS), eb("ElmtNum", "=", el.ElmtNum)]), - ) - .innerJoin("Subs_Nom", "Composant.NomId", "Subs_Nom.NomId") - .selectAll() - .execute(), - ), - ) - ).flat(); - - const presentations: (Presentation & Nullable)[] = ( - await pdbmMySQL - .selectFrom("Presentation") - .where("SpecId", "=", CIS) - .where(({ eb }) => - eb.or([ - eb("CommId", "=", PresentationComm.Commercialisation), - eb.and([ - eb("CommId", "in", [ - PresentationComm["ArrĂȘt"], - PresentationComm.Suspension, - PresentationComm["Plus d'autorisation"], - ]), - eb( - "PresCommDate", - ">=", - sql`DATE_ADD(NOW(),INTERVAL -730 DAY)`, - ), - ]), - ]), - ) - .where(({ eb }) => - eb.or([ - eb("StatId", "is", null), - eb("StatId", "!=", PresentationStat.Abrogation), - eb( - "PresStatDate", - ">=", - sql`DATE_ADD(NOW(),INTERVAL -730 DAY)`, - ), - ]), - ) - .leftJoin( - "CNAM_InfoTarif", - "Presentation.codeCIP13", - "CNAM_InfoTarif.Cip13", - ) - .selectAll() - .execute() - ).sort((a, b) => - a.Prix && b.Prix - ? parseFloat(a.Prix.replace(",", ".")) - - parseFloat(b.Prix.replace(",", ".")) - : a.Prix - ? -1 - : b.Prix - ? 1 - : 0, - ); - - const delivrance: SpecDelivrance[] = await pdbmMySQL - .selectFrom("Spec_Delivrance") - .where("SpecId", "=", CIS) - .innerJoin( - "DicoDelivrance", - "Spec_Delivrance.DelivId", - "DicoDelivrance.DelivId", - ) - .selectAll() - .execute(); - - return { - specialite, - composants, - presentations, - delivrance, - }; -}); - function getAtcCode(CIS: string) { const atc = atcData.find((row) => row[0] === CIS); diff --git a/src/components/MedGroupSpecList.tsx b/src/components/MedGroupSpecList.tsx index 40d95f1..42e9f62 100644 --- a/src/components/MedGroupSpecList.tsx +++ b/src/components/MedGroupSpecList.tsx @@ -1,12 +1,19 @@ import { getAtcLabels } from "@/data/atc"; import { fr } from "@codegouvfr/react-dsfr"; -import { formatSpecName, MedicamentGroup } from "@/displayUtils"; +import { + displaySimpleComposants, + formatSpecName, + MedicamentGroup, +} from "@/displayUtils"; import { cx } from "@codegouvfr/react-dsfr/tools/cx"; import Tag from "@codegouvfr/react-dsfr/Tag"; import Link from "next/link"; import { parse as csvParse } from "csv-parse/sync"; import { readFileSync } from "node:fs"; import path from "node:path"; +import { SubstanceNom } from "@/db/pdbmMySQL/types"; +import React from "react"; +import { getSpecialite } from "@/db/pdbmMySQL/utils"; const atcData = csvParse( readFileSync( @@ -27,8 +34,9 @@ export default async function MedGroupSpecList({ }) { const [groupName, specialites] = medGroup; const atc = getAtc(specialites[0].SpecId); + const { composants } = await getSpecialite(specialites[0].SpecId); const atcLabels = atc ? await getAtcLabels(atc) : null; - const [, subClass, substance] = atcLabels ? atcLabels : [null, null, null]; + const [, subClass] = atcLabels ? atcLabels : [null, null]; return (
  • @@ -52,15 +60,19 @@ export default async function MedGroupSpecList({ {subClass} )} - {substance && ( - - {substance} - + {displaySimpleComposants(composants).map( + (substance: SubstanceNom) => ( + + {substance.NomLib} + + ), )}
    diff --git a/src/db/pdbmMySQL/utils.ts b/src/db/pdbmMySQL/utils.ts new file mode 100644 index 0000000..5d7c0be --- /dev/null +++ b/src/db/pdbmMySQL/utils.ts @@ -0,0 +1,117 @@ +import { cache } from "react"; +import liste_CIS_MVP from "@/liste_CIS_MVP.json"; +import { notFound } from "next/navigation"; +import { + Presentation, + PresentationComm, + PresentationStat, + PresInfoTarif, + SpecComposant, + SpecDelivrance, + SpecElement, + Specialite, + SubstanceNom, +} from "@/db/pdbmMySQL/types"; +import { pdbmMySQL } from "@/db/pdbmMySQL/index"; +import { Nullable, sql } from "kysely"; + +export const getSpecialite = cache(async (CIS: string) => { + if (!liste_CIS_MVP.includes(CIS)) notFound(); + + const specialite: Specialite | undefined = await pdbmMySQL + .selectFrom("Specialite") + .where("SpecId", "=", CIS) + .selectAll() + .executeTakeFirst(); + + if (!specialite) return notFound(); + + const elements: SpecElement[] = await pdbmMySQL + .selectFrom("Element") + .where("SpecId", "=", CIS) + .selectAll() + .execute(); + + const composants: Array = ( + await Promise.all( + elements.map((el) => + pdbmMySQL + .selectFrom("Composant") + .where((eb) => + eb.and([eb("SpecId", "=", CIS), eb("ElmtNum", "=", el.ElmtNum)]), + ) + .innerJoin("Subs_Nom", "Composant.NomId", "Subs_Nom.NomId") + .selectAll() + .execute(), + ), + ) + ).flat(); + + const presentations: (Presentation & Nullable)[] = ( + await pdbmMySQL + .selectFrom("Presentation") + .where("SpecId", "=", CIS) + .where(({ eb }) => + eb.or([ + eb("CommId", "=", PresentationComm.Commercialisation), + eb.and([ + eb("CommId", "in", [ + PresentationComm["ArrĂȘt"], + PresentationComm.Suspension, + PresentationComm["Plus d'autorisation"], + ]), + eb( + "PresCommDate", + ">=", + sql`DATE_ADD(NOW(),INTERVAL -730 DAY)`, + ), + ]), + ]), + ) + .where(({ eb }) => + eb.or([ + eb("StatId", "is", null), + eb("StatId", "!=", PresentationStat.Abrogation), + eb( + "PresStatDate", + ">=", + sql`DATE_ADD(NOW(),INTERVAL -730 DAY)`, + ), + ]), + ) + .leftJoin( + "CNAM_InfoTarif", + "Presentation.codeCIP13", + "CNAM_InfoTarif.Cip13", + ) + .selectAll() + .execute() + ).sort((a, b) => + a.Prix && b.Prix + ? parseFloat(a.Prix.replace(",", ".")) - + parseFloat(b.Prix.replace(",", ".")) + : a.Prix + ? -1 + : b.Prix + ? 1 + : 0, + ); + + const delivrance: SpecDelivrance[] = await pdbmMySQL + .selectFrom("Spec_Delivrance") + .where("SpecId", "=", CIS) + .innerJoin( + "DicoDelivrance", + "Spec_Delivrance.DelivId", + "DicoDelivrance.DelivId", + ) + .selectAll() + .execute(); + + return { + specialite, + composants, + presentations, + delivrance, + }; +});