Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use grist for atc data #13

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/medicament/[CIS]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import liste_CIS_MVP from "@/liste_CIS_MVP.json";
import DsfrLeafletSection from "@/app/medicament/[CIS]/DsfrLeafletSection";
import { isHtmlElement } from "@/app/medicament/[CIS]/leafletUtils";
import {
atcToBreadcrumbs,
dateShortFormat,
displayComposants,
formatSpecName,
Expand All @@ -37,6 +36,7 @@ import {
Specialite,
SubstanceNom,
} from "@/db/pdbmMySQL/types";
import { getAtcLabels } from "@/data/atc";

export async function generateMetadata(
{ params: { CIS } }: { params: { CIS: string } },
Expand Down Expand Up @@ -311,7 +311,7 @@ export default async function Page({
await getSpecialite(CIS);
const leaflet = await getLeaflet(CIS);
const atc = getAtc(CIS);
const atcBreadcrumbs = atc ? atcToBreadcrumbs(atc) : null;
const atcBreadcrumbs = atc ? await getAtcLabels(atc) : null;

return (
<>
Expand Down
16 changes: 0 additions & 16 deletions src/data/ATC-labels-1.json

This file was deleted.

96 changes: 0 additions & 96 deletions src/data/ATC-labels-2.json

This file was deleted.

62 changes: 62 additions & 0 deletions src/data/atc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import "server-only";
import atcOfficialLabels from "@/data/ATC 2024 02 15.json";

async function getGristTableData(tableId: string) {
const response = await fetch(
`https://grist.numerique.gouv.fr/api/docs/${process.env.GRIST_DOC_ID}/tables/${tableId}/records`,
{
headers: {
Authorization: `Bearer ${process.env.GRIST_API_KEY}`,
},
},
);
return await response.json();
}

async function getAtcLabel1(code: string): Promise<string> {
const data = await getGristTableData("Table_Niveau_1");
const record = data.records.find(
(record: any) => record.fields.Lettre_1_ATC_1 === code.slice(0, 1),
);
if (!record) {
throw new Error(`ATC code not found: ${code.slice(0, 1)}`);
}

return record.fields.Libelles_niveau_1;
}

async function getAtcLabel2(code: string): Promise<string> {
const atcData = await getGristTableData("Table_Niveau_2");
const record = atcData.records.find(
(record: any) => record.fields.Lettre_2_ATC2 === code.slice(0, 3),
);
if (!record) {
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
}

const libeleId = record.fields.Libelles_niveau_2;
const libeleData = await getGristTableData("Intitules_possibles");
const libeleRecord = libeleData.records.find(
(record: any) => record.id === libeleId,
);

if (!libeleRecord) {
throw new Error(`ATC code not found: ${code.slice(0, 3)}`);
}

return libeleRecord.fields.Libelles_niveau_2;
}

async function getAtcOfficialLabel(code: string): Promise<string> {
if (!(code.slice(0, 7) in atcOfficialLabels)) {
throw new Error(`ATC code not found: ${code.slice(0, 7)}`);
}

return (atcOfficialLabels as Record<string, string>)[code.slice(0, 7)];
}

export async function getAtcLabels(atc: string): Promise<string[]> {
return Promise.all(
[getAtcLabel1, getAtcLabel2, getAtcOfficialLabel].map((f) => f(atc)),
);
}
16 changes: 0 additions & 16 deletions src/displayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {
Specialite,
SubstanceNom,
} from "@/db/pdbmMySQL/types";
import atcLabels1 from "@/data/ATC-labels-1.json";
import atcLabels2 from "@/data/ATC-labels-2.json";
import atcOfficialLabels from "@/data/ATC 2024 02 15.json";

export const formatSpecName = (name: string): string =>
name
Expand Down Expand Up @@ -116,19 +113,6 @@ export function displayComposants(
.join("; ");
}

export function atcToBreadcrumbs(atc: string): string[] {
return [
[1, atcLabels1] as [number, Record<string, string>],
[3, atcLabels2] as [number, Record<string, string>],
[7, atcOfficialLabels] as [number, Record<string, string>],
].map(([i, labels]) => {
if (!(atc.slice(0, i) in labels)) {
throw new Error(`ATC code not found: ${atc.slice(0, i)}`);
}
return labels[atc.slice(0, i)];
});
}

export function dateShortFormat(date: Date): string {
return date.toLocaleDateString("fr-FR", {
year: "numeric",
Expand Down