Skip to content

Commit c8d7321

Browse files
authored
Merge pull request #13 from betagouv/maud/grist_atc
Use grist for atc data
2 parents 97cd924 + e896489 commit c8d7321

File tree

5 files changed

+64
-130
lines changed

5 files changed

+64
-130
lines changed

src/app/medicament/[CIS]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import liste_CIS_MVP from "@/liste_CIS_MVP.json";
1818
import DsfrLeafletSection from "@/app/medicament/[CIS]/DsfrLeafletSection";
1919
import { isHtmlElement } from "@/app/medicament/[CIS]/leafletUtils";
2020
import {
21-
atcToBreadcrumbs,
2221
dateShortFormat,
2322
displayComposants,
2423
formatSpecName,
@@ -37,6 +36,7 @@ import {
3736
Specialite,
3837
SubstanceNom,
3938
} from "@/db/pdbmMySQL/types";
39+
import { getAtcLabels } from "@/data/atc";
4040

4141
export async function generateMetadata(
4242
{ params: { CIS } }: { params: { CIS: string } },
@@ -311,7 +311,7 @@ export default async function Page({
311311
await getSpecialite(CIS);
312312
const leaflet = await getLeaflet(CIS);
313313
const atc = getAtc(CIS);
314-
const atcBreadcrumbs = atc ? atcToBreadcrumbs(atc) : null;
314+
const atcBreadcrumbs = atc ? await getAtcLabels(atc) : null;
315315

316316
return (
317317
<>

src/data/ATC-labels-1.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/data/ATC-labels-2.json

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/data/atc.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import "server-only";
2+
import atcOfficialLabels from "@/data/ATC 2024 02 15.json";
3+
4+
async function getGristTableData(tableId: string) {
5+
const response = await fetch(
6+
`https://grist.numerique.gouv.fr/api/docs/${process.env.GRIST_DOC_ID}/tables/${tableId}/records`,
7+
{
8+
headers: {
9+
Authorization: `Bearer ${process.env.GRIST_API_KEY}`,
10+
},
11+
},
12+
);
13+
return await response.json();
14+
}
15+
16+
async function getAtcLabel1(code: string): Promise<string> {
17+
const data = await getGristTableData("Table_Niveau_1");
18+
const record = data.records.find(
19+
(record: any) => record.fields.Lettre_1_ATC_1 === code.slice(0, 1),
20+
);
21+
if (!record) {
22+
throw new Error(`ATC code not found: ${code.slice(0, 1)}`);
23+
}
24+
25+
return record.fields.Libelles_niveau_1;
26+
}
27+
28+
async function getAtcLabel2(code: string): Promise<string> {
29+
const atcData = await getGristTableData("Table_Niveau_2");
30+
const record = atcData.records.find(
31+
(record: any) => record.fields.Lettre_2_ATC2 === code.slice(0, 3),
32+
);
33+
if (!record) {
34+
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
35+
}
36+
37+
const libeleId = record.fields.Libelles_niveau_2;
38+
const libeleData = await getGristTableData("Intitules_possibles");
39+
const libeleRecord = libeleData.records.find(
40+
(record: any) => record.id === libeleId,
41+
);
42+
43+
if (!libeleRecord) {
44+
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
45+
}
46+
47+
return libeleRecord.fields.Libelles_niveau_2;
48+
}
49+
50+
async function getAtcOfficialLabel(code: string): Promise<string> {
51+
if (!(code.slice(0, 7) in atcOfficialLabels)) {
52+
throw new Error(`ATC code not found: ${code.slice(0, 7)}`);
53+
}
54+
55+
return (atcOfficialLabels as Record<string, string>)[code.slice(0, 7)];
56+
}
57+
58+
export async function getAtcLabels(atc: string): Promise<string[]> {
59+
return Promise.all(
60+
[getAtcLabel1, getAtcLabel2, getAtcOfficialLabel].map((f) => f(atc)),
61+
);
62+
}

src/displayUtils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import {
44
Specialite,
55
SubstanceNom,
66
} from "@/db/pdbmMySQL/types";
7-
import atcLabels1 from "@/data/ATC-labels-1.json";
8-
import atcLabels2 from "@/data/ATC-labels-2.json";
9-
import atcOfficialLabels from "@/data/ATC 2024 02 15.json";
107

118
export const formatSpecName = (name: string): string =>
129
name
@@ -116,19 +113,6 @@ export function displayComposants(
116113
.join("; ");
117114
}
118115

119-
export function atcToBreadcrumbs(atc: string): string[] {
120-
return [
121-
[1, atcLabels1] as [number, Record<string, string>],
122-
[3, atcLabels2] as [number, Record<string, string>],
123-
[7, atcOfficialLabels] as [number, Record<string, string>],
124-
].map(([i, labels]) => {
125-
if (!(atc.slice(0, i) in labels)) {
126-
throw new Error(`ATC code not found: ${atc.slice(0, i)}`);
127-
}
128-
return labels[atc.slice(0, i)];
129-
});
130-
}
131-
132116
export function dateShortFormat(date: Date): string {
133117
return date.toLocaleDateString("fr-FR", {
134118
year: "numeric",

0 commit comments

Comments
 (0)