Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
fix: fix adeli id errors for 9A 9B 9C 9D 9F 2A and 2B (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre authored Aug 11, 2022
1 parent 1d7ac67 commit 0da1689
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
28 changes: 23 additions & 5 deletions src/cron/demarchesSimplifiees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from "@sentry/nextjs";
import pLimit from "p-limit";

import { formatAdeliId } from "../services/adeli/formatAdeliId";
import { requestAdeli } from "../services/adeli/request";
import { addVerificationMessage } from "../services/demarchesSimplifiees/buildRequest";
import filterDossiersToVerif from "../services/demarchesSimplifiees/dossiers";
Expand All @@ -20,7 +21,6 @@ import {
} from "../services/psychologists";
import { AdeliData } from "../types/adeli";
import { Psychologist } from "../types/psychologist";
import { removeNonNumericCharacters } from "../utils/string";

const limit = pLimit(5);

Expand Down Expand Up @@ -74,6 +74,27 @@ export const importFromDS = async (): Promise<void> => {
await importArchived();
};

function isAdeliIdValidDepartment(
adeliId: string,
department: string
): boolean {
const departmentFromAdeli = formatAdeliId(adeliId).substring(0, 2);
switch (departmentFromAdeli) {
case "9A":
return department === "971";
case "9B":
return department === "972";
case "9C":
return department === "973";
case "9D":
return department === "974";
case "9F":
return department === "976";
default:
return departmentFromAdeli === department;
}
}

export const validateDossier = async (
dossier: Psychologist,
adeliData: AdeliData[]
Expand All @@ -84,10 +105,7 @@ export const validateDossier = async (
return [`Numéro ADELI invalide : ${dossier.adeliId}`];
}

const departmentFromAdeli = removeNonNumericCharacters(
dossier.adeliId || ""
).substring(0, 2);
if (dossier.department !== departmentFromAdeli) {
if (!isAdeliIdValidDepartment(dossier.adeliId || "", dossier.department)) {
errors.push(
`Le numéro ADELI ${dossier.adeliId} ne correspond pas au département ${dossier.department}`
);
Expand Down
32 changes: 19 additions & 13 deletions src/services/__tests__/adeli-resquest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ describe("requestAdeli", () => {
});

it.each`
input
${"123"}
${"1 2 3"}
${"1.2...3"}
${"00/00/01/23"}
`("should call api endpoint if adeli is $input", async ({ input }) => {
const result = await requestAdeli(input);
expect(axios.get).toHaveBeenCalledWith(
"https://datasette-ps-libre-acces.dev.fabrique.social.gouv.fr/PS_LibreAcces/PS_LibreAcces_Personne_activite.json",
{ params: { "Identification nationale PP__exact": "0000000123" } }
);
expect(result).toEqual([]);
});
input | expected
${"123"} | ${"0000000123"}
${"1 2 3"} | ${"0000000123"}
${"1.2...3"} | ${"0000000123"}
${"00/00/01/23"} | ${"0000000123"}
${"123zzz"} | ${"0000000123"}
${"9F12345678"} | ${"9F12345678"}
${"2A 1234 5678"} | ${"2A12345678"}
`(
"should call api endpoint if adeli is $input",
async ({ input, expected }) => {
const result = await requestAdeli(input);
expect(axios.get).toHaveBeenCalledWith(
"https://datasette-ps-libre-acces.dev.fabrique.social.gouv.fr/PS_LibreAcces/PS_LibreAcces_Personne_activite.json",
{ params: { "Identification nationale PP__exact": expected } }
);
expect(result).toEqual([]);
}
);
});
10 changes: 10 additions & 0 deletions src/services/adeli/formatAdeliId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const formatAdeliId = (adeliId: string): string => {
// Adeli ID is a 10-digit string, that can also contain letters for some departments.
// e.g. "9A1234678" (Guadeloupe) or "2A1234678" (Corse-du-Sud).
// In this particular function, we just want to remove non-alphanumeric (0 to 9 and A to F) characters
// since it is sent to adeli service for complete checking.
return (adeliId || "")
.replace(/[^0-9a-f]/gi, "")
.toUpperCase()
.padStart(10, "0");
};
7 changes: 2 additions & 5 deletions src/services/adeli/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import axios, { AxiosResponse } from "axios";

import { AdeliData, AdeliRawResponse } from "../../types/adeli";
import { zip } from "../../utils/array";
import { removeNonNumericCharacters } from "../../utils/string";
import config from "../config";

const formatRequestedAdeli = (adeliId: string): string =>
removeNonNumericCharacters(adeliId || "").padStart(10, "0");
import { formatAdeliId } from "./formatAdeliId";

const rowColumnsToObject = <Column>(row: string[], columns: Column[]) =>
Object.fromEntries(zip(columns, row));
Expand All @@ -24,7 +21,7 @@ export const requestAdeli = async (
config.adeli.apiUrl,
{
params: {
"Identification nationale PP__exact": formatRequestedAdeli(numeroAdeli),
"Identification nationale PP__exact": formatAdeliId(numeroAdeli),
},
}
);
Expand Down
4 changes: 0 additions & 4 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ export const firstWordAreSimilar = (

return areSimilar(firstWord1, firstWord2);
};

export function removeNonNumericCharacters(value: string): string {
return value.replace(/[^0-9]/g, "");
}

0 comments on commit 0da1689

Please sign in to comment.