Skip to content

Commit

Permalink
Merge pull request #4 from betagouv/presentation_comm_statut
Browse files Browse the repository at this point in the history
Display active or recent presentations
  • Loading branch information
jillro authored Sep 19, 2024
2 parents 13dd1f0 + 6662b9f commit 17fb6dc
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 3 deletions.
45 changes: 44 additions & 1 deletion src/app/medicament/[CIS]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import JSZIP from "jszip";
// @ts-ignore
import * as windows1252 from "windows-1252";
import HTMLParser, { HTMLElement } from "node-html-parser";
import { Nullable } from "kysely";
import { Nullable, sql } from "kysely";
import { parse as csvParse } from "csv-parse/sync";

import {
pdbmMySQL,
Presentation,
PresentationComm,
PresentationStat,
PresInfoTarif,
SpecComposant,
SpecDelivrance,
Expand All @@ -28,6 +30,7 @@ import DsfrLeafletSection from "@/app/medicament/[CIS]/DsfrLeafletSection";
import { isHtmlElement } from "@/app/medicament/[CIS]/leafletUtils";
import {
atcToBreadcrumbs,
dateShortFormat,
displayComposants,
formatSpecName,
getSpecialiteGroupName,
Expand Down Expand Up @@ -85,6 +88,34 @@ const getSpecialite = cache(async (CIS: string) => {
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",
Expand Down Expand Up @@ -349,6 +380,18 @@ export default async function Page({
) : (
<>Prix libre - non remboursable</>
)}
{p.CommId !== PresentationComm.Commercialisation && (
<Badge severity="warning" className={fr.cx("fr-ml-1v")}>
{PresentationComm[p.CommId]}
{p.PresCommDate && ` (${dateShortFormat(p.PresCommDate)})`}
</Badge>
)}
{p.StatId && p.StatId === PresentationStat.Abrogation && (
<Badge severity="error" className={fr.cx("fr-ml-1v")}>
{PresentationStat[p.StatId]}
{p.PresStatDate && ` (${dateShortFormat(p.PresStatDate)})`}
</Badge>
)}
</li>
))}
</ul>
Expand Down
91 changes: 89 additions & 2 deletions src/db/pdbmMySQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ interface PdbmMySQL {
CNAM_InfoTarif: InfoTarifTable;
Spec_Delivrance: SpecDelivranceTable;
DicoDelivrance: DicoDelivranceTable;
StatutComm: StatutCommTable;
StatutAdm: StatutAdmTable;
}

interface SpecialiteTable {
SpecId: string;
StatId: string;
CommId: string;
StatId: SpecialiteStat | null;
CommId: SpecialiteComm | null;
ProcId: string;
SpecGeneId: string;
SpecDenom01: string;
Expand Down Expand Up @@ -70,6 +72,10 @@ interface PresentationTable {
SpecId: string;
PresNum: string;
PresNom01: string;
CommId: PresentationComm;
StatId: PresentationStat | null;
PresCommDate: Date | null;
PresStatDate: Date | null;
codeCIP13: string;
}

Expand All @@ -92,6 +98,20 @@ interface DicoDelivranceTable {
DelivLong: string;
}

interface StatutAdmTable {
StatId: number;
StatLibCourt: string;
StatLibLong: string;
StatDomaine: "P" | "S";
}

interface StatutCommTable {
CommId: number;
CommLibCourt: string;
CommLibLong: string;
CommDomaine: "P" | "S";
}

export type Specialite = Selectable<SpecialiteTable>;
export type SpecElement = Selectable<SpecElementTable>;
export type SpecComposant = Selectable<SpecComposantTable>;
Expand All @@ -101,6 +121,39 @@ export type PresInfoTarif = Selectable<InfoTarifTable>;
export type SpecDelivrance = Selectable<SpecDelivranceTable> &
Selectable<DicoDelivranceTable>;

// Those enums are store as small dictionary tables in the database
// but to benefit from TypeScript type checking
// we define them here as well and check that they match the database
// at runtime

// Should match StatutAdm table

export enum SpecialiteStat {
"Valide" = 10,
"Abrogée" = 20,
"Suspendue" = 30,
"Retirée" = 40,
"Archivée" = 60,
}

export enum PresentationStat {
Abrogation = 50,
}

// Should match StatutComm table

export enum SpecialiteComm {
"Commercialisée" = 50,
}

export enum PresentationComm {
"Commercialisation" = 10,
"Arrêt" = 20,
"Suspension" = 30,
"Non communiquée" = 40,
"Plus d'autorisation" = 45,
}

export const pdbmMySQL = new Kysely<PdbmMySQL>({
dialect: new MysqlDialect({
pool: process.env.PDBM_URL
Expand All @@ -116,3 +169,37 @@ export const pdbmMySQL = new Kysely<PdbmMySQL>({
}),
}),
});

(async () => {
const StatutComm = await pdbmMySQL
.selectFrom("StatutComm")
.selectAll()
.execute();

const StatutAdm = await pdbmMySQL
.selectFrom("StatutAdm")
.selectAll()
.execute();

// Check that enums matches the database
Object.values(StatutComm).forEach(({ CommId, CommLibCourt, CommDomaine }) => {
if (
(CommDomaine === "S" ? SpecialiteComm : PresentationComm)[CommId] !==
CommLibCourt
) {
throw new Error(
`Enum does not match database: ${CommId} ${CommLibCourt} ${CommDomaine}`,
);
}
});
Object.values(StatutAdm).forEach(({ StatId, StatLibCourt, StatDomaine }) => {
if (
(StatDomaine === "S" ? SpecialiteStat : PresentationStat)[StatId] !==
StatLibCourt
) {
throw new Error(
`Enum does not match database: ${StatId} ${StatLibCourt} ${StatDomaine}`,
);
}
});
})();
8 changes: 8 additions & 0 deletions src/displayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ export function atcToBreadcrumbs(atc: string): string[] {
return row[4];
});
}

export function dateShortFormat(date: Date): string {
return date.toLocaleDateString("fr-FR", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
}

0 comments on commit 17fb6dc

Please sign in to comment.