-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: display substances in meds group lists
Signed-off-by: Maud Royer <[email protected]>
- Loading branch information
Showing
3 changed files
with
141 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}); |