Skip to content

Commit

Permalink
fix: display substances in meds group lists
Browse files Browse the repository at this point in the history
Signed-off-by: Maud Royer <[email protected]>
  • Loading branch information
jillro committed Nov 13, 2024
1 parent ec7ba94 commit cb84b46
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 123 deletions.
113 changes: 1 addition & 112 deletions src/app/(container)/medicaments/[CIS]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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<SpecComposant & SubstanceNom> = (
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<PresInfoTarif>)[] = (
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>`DATE_ADD(NOW(),INTERVAL -730 DAY)`,
),
]),
]),
)
.where(({ eb }) =>
eb.or([
eb("StatId", "is", null),
eb("StatId", "!=", PresentationStat.Abrogation),
eb(
"PresStatDate",
">=",
sql<Date>`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);

Expand Down
34 changes: 23 additions & 11 deletions src/components/MedGroupSpecList.tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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 (
<li className={className}>
<div>
Expand All @@ -52,15 +60,19 @@ export default async function MedGroupSpecList({
{subClass}
</Tag>
)}
{substance && (
<Tag
small
nativeButtonProps={{
className: cx("fr-tag--custom-alt-substance"),
}}
>
{substance}
</Tag>
{displaySimpleComposants(composants).map(
(substance: SubstanceNom) => (
<Tag
key={substance.NomId}
small
linkProps={{
href: `/substances/${substance.NomId}`,
className: cx("fr-tag--custom-alt-substance"),
}}
>
{substance.NomLib}
</Tag>
),
)}
</ul>
</div>
Expand Down
117 changes: 117 additions & 0 deletions src/db/pdbmMySQL/utils.ts
Original file line number Diff line number Diff line change
@@ -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<SpecComposant & SubstanceNom> = (
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<PresInfoTarif>)[] = (
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>`DATE_ADD(NOW(),INTERVAL -730 DAY)`,
),
]),
]),
)
.where(({ eb }) =>
eb.or([
eb("StatId", "is", null),
eb("StatId", "!=", PresentationStat.Abrogation),
eb(
"PresStatDate",
">=",
sql<Date>`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,
};
});

0 comments on commit cb84b46

Please sign in to comment.