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

Commit

Permalink
feat(importState): save missing psy (as importData seems to miss some…
Browse files Browse the repository at this point in the history
… psy) (#123)
  • Loading branch information
carolineBda authored Mar 29, 2022
1 parent b760f6c commit 5bbcd35
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 12 deletions.
127 changes: 127 additions & 0 deletions src/__tests__/cron/importState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { importState } from "../../cron/demarchesSimplifiees";
import { models } from "../../db/models";
import { getOnePsychologist } from "../../db/seeds/psychologist";
import { request } from "../../services/demarchesSimplifiees/request";
import { Psychologist } from "../../types/psychologist";

jest.mock("../../services/demarchesSimplifiees/request");

function formatForDS(psy: Psychologist) {
return Object.assign(
{
number: psy.id,
groupeInstructeur: {
id: psy.instructorId,
label: psy.department + " - dep",
},
demandeur: { prenom: psy.firstName, nom: psy.lastName },
champs: [
{
id: "Q2hhbXAtMTYwMzgwNQ==",
stringValue: psy.phone,
},
{
id: "Q2hhbXAtMTYyNzkzOQ==",
stringValue: psy.address,
},
{
id: "Q2hhbXAtMjMyMzQyMg==",
label: "Adresse postale d'un autre lieu d'exercice ",
stringValue: "",
},
{
id: "Q2hhbXAtMjIyMjcwMg==",
label: "Public",
stringValue: "",
},
],
usager: {
email: psy.email,
},
},
psy
);
}

const states = ["accepte", "en_instruction", "refuse"];
const archiveds = [true, false];

const psychologistsInDB = archiveds.flatMap((archived) =>
states.flatMap((state) =>
getOnePsychologist({
archived,
state,
instructorId: "psyInDB",
})
)
);
const psyHasChanged = getOnePsychologist({
state: "accepte",
address: "psyHasChanged",
instructorId: "psyInDB",
});

psychologistsInDB.push(psyHasChanged);
const psychologistsInDS = psychologistsInDB.map((psy) => {
return formatForDS(psy);
});

const newPsy = formatForDS(
getOnePsychologist({
state: "accepte",
address: "newPsy",
instructorId: "psyInDB",
})
);
psychologistsInDS.push(newPsy);
const updatedPsy = formatForDS(psyHasChanged);
updatedPsy.state = "refuse";
psychologistsInDS.push(updatedPsy);

describe("Cron import State", () => {
beforeEach(async () => {
await models.Psychologist.destroy({ where: {} });

//@ts-ignore
await models.Psychologist.bulkCreate(psychologistsInDB);

// @ts-ignore
request.mockImplementationOnce(() =>
Promise.resolve({
demarche: { dossiers: { nodes: psychologistsInDS, pageInfo: {} } },
})
);

//@ts-ignore
request.mockImplementationOnce(() =>
Promise.resolve({ dossier: updatedPsy })
);
//@ts-ignore
request.mockImplementationOnce(() => Promise.resolve({ dossier: newPsy }));
});

it("Should add missing psy", async () => {
// @ts-ignore
const psyBefore: Psychologist = await models.Psychologist.findOne({
where: { id: psyHasChanged.id },
raw: true,
});
expect(psyBefore.state).toEqual("accepte");

await importState();

// @ts-ignore
const psychologists: Psychologist[] = await models.Psychologist.findAll({
where: { instructorId: "psyInDB" },
raw: true,
});

expect(psychologists.length).toEqual(8);
expect(psychologists.find((psy) => psy.address === "newPsy").state).toEqual(
"accepte"
);
expect(
psychologists.find((psy) => psy.address === "psyHasChanged").state
).toEqual("refuse");
});
});
2 changes: 1 addition & 1 deletion src/components/Directory/Psychologist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Psychologist = ({
<Icon
aria-hidden="true"
alt=""
className="fr-mr-1w"
className="fr-mr-1w fr-mb-1v"
height="18"
width="18"
src="/images/icones/map-pin-fill.svg"
Expand Down
19 changes: 16 additions & 3 deletions src/cron/demarchesSimplifiees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as Sentry from "@sentry/nextjs";

import { models } from "../db/models";
import {
getPsychologistFromListIds,
getPsychologistList,
getPsychologistState,
} from "../services/demarchesSimplifiees/import";
import { saveMany, updateState } from "../services/psychologists";
import {
filterIdsNotInDb,
saveMany,
updateState,
} from "../services/psychologists";

export const importData = async (): Promise<void> => {
try {
Expand Down Expand Up @@ -42,9 +47,17 @@ export const importState = async (): Promise<void> => {
const dsAPIData = await getPsychologistState();

await updateState(dsAPIData);
console.log("importState done");
const missingPsy: number[] = await filterIdsNotInDb(
dsAPIData.filter((psy) => psy.state === "accepte")
);
if (missingPsy.length) {
const missingPsyData = await getPsychologistFromListIds(missingPsy);
await saveMany(missingPsyData);
console.log(`Added ${missingPsy.length} missing psys`);
}
console.log(`importState done.`);
} catch (err) {
Sentry.captureException(err);
console.error("ERROR: Could not import DS API state to PG", err);
console.error("ERROR importState: ", err);
}
};
35 changes: 35 additions & 0 deletions src/services/demarchesSimplifiees/buildRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,38 @@ export const requestPsychologistsAcceptes = async (

return request(query);
};

export const requestPsychologistsById = async (
id: number
): Promise<DSResponse> => {
const query = gql`
{
dossier (number: ${id}) {
archived
number
groupeInstructeur {
id
label
}
state
champs {
id
label
stringValue
}
usager {
email
}
demandeur {
... on PersonnePhysique {
civilite
nom
prenom
}
}
}
}
`;

return request(query);
};
21 changes: 21 additions & 0 deletions src/services/demarchesSimplifiees/import.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import pLimit from "p-limit";

import { DSResponse } from "../../types/demarcheSimplifiee";
import { DSPsychologist, Psychologist } from "../../types/psychologist";
import {
requestPsychologistsAcceptes,
requestPsychologistsById,
requestPsychologistsState,
} from "./buildRequest";
import parsePsychologists from "./parse-psychologists";

const limit = pLimit(5);
const getAllPsychologistList = async (
graphqlFunction: (string) => Promise<DSResponse>,
cursor: string | undefined = undefined,
Expand Down Expand Up @@ -55,6 +59,23 @@ export const getPsychologistList = async (
return results;
};

function fetchPsyById(ids: number[]) {
return Promise.all(
ids.map(async (id) => limit(() => requestPsychologistsById(id)))
);
}

function mapPsyData(psyData: any[]) {
return psyData.map((psy) => psy.dossier).flat();
}

export const getPsychologistFromListIds = async (
ids: number[]
): Promise<Psychologist[]> => {
const psyData = await fetchPsyById(ids);
return await parsePsychologists(mapPsyData(psyData));
};

export const getPsychologistState = async (): Promise<
Partial<Psychologist>[]
> => {
Expand Down
18 changes: 16 additions & 2 deletions src/services/faq/faq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import difficulty from "./patient/difficulty";
import eligibilityPatient from "./patient/eligibility";
import end from "./patient/end";
import information from "./patient/information";
import reimbursment from "./patient/reimbursment";
import reimbursement from "./patient/reimbursment";
import agreement from "./psy/agreement";
import billing from "./psy/billing";
import psyDoctor from "./psy/doctor";
Expand All @@ -30,13 +30,19 @@ const items = [
{ faq: eligibilityPatient, title: "Éligibilité" },
{ faq: begin, title: "Début du parcours" },
{ faq: accompaniment, title: "Accompagnement par le psychologue" },
{ faq: reimbursment, title: "Remboursement" },
{ faq: reimbursement, title: "Remboursement" },
{ faq: end, title: "Fin du parcours" },
{ faq: children, title: "Patients de moins de 18 ans" },
{ faq: difficulty, title: "En cas de difficulté" },
{ faq: information, title: "Information sur la santé mentale" },
],
title: "Patient",
documents: [
{
url: "/documents/MonPsy_Flyer grand public.pdf",
title: " Flyer Grand Public",
},
],
},
{
documents: [
Expand Down Expand Up @@ -78,6 +84,14 @@ const items = [
{
key: "medecin",
documents: [
{
title: "Guide pour les médecins",
url: "/documents/MonPsy_Guide médecin_2022.pdf",
},
{
title: "Fiche mémo pour les médecins",
url: "/documents/MonPsy_Fiche-Mémo_médecin_2022.pdf",
},
{
title: "Echelles d’évaluation",
url: "/documents/MonPsy_Livret_échelles évaluations_2022.pdf",
Expand Down
30 changes: 24 additions & 6 deletions src/services/psychologists.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pLimit from "p-limit";
import Sequelize, { Op } from "sequelize";

import { models } from "../db/models";
Expand All @@ -7,6 +8,8 @@ import { PUBLIC } from "../types/enums/public";
import { Psychologist } from "../types/psychologist";
import getAddressCoordinates from "./getAddressCoordinates";

const limit = pLimit(5);

export const getOne = async (id: string): Promise<Psychologist> => {
// @ts-ignore
return models.Psychologist.findOne({
Expand Down Expand Up @@ -143,13 +146,28 @@ export const update = async (
export const updateState = async (newStates: Partial<Psychologist>[]) => {
return Promise.all(
newStates.map((newState) =>
models.Psychologist.update(
{
archived: newState.archived,
state: newState.state,
},
{ where: { id: newState.id } }
limit(() =>
models.Psychologist.update(
{
archived: newState.archived,
state: newState.state,
},
{ where: { id: newState.id } }
)
)
)
);
};

export const filterIdsNotInDb = async (psy): Promise<number[]> => {
// @ts-ignore
const psyInDb: { id: number }[] = await models.Psychologist.findAll({
attributes: ["id"],
raw: true,
where: { state: "accepte" },
});

return psy
.filter((psy) => !psyInDb.find((fromDb) => psy.id === fromDb.id))
.map((psy) => psy.id);
};

0 comments on commit 5bbcd35

Please sign in to comment.