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

Commit

Permalink
Merge branch 'main' into lancement-annuaire
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
  • Loading branch information
carolineBda committed Apr 4, 2022
2 parents 19bf1e1 + ea4e0dc commit f9bb14f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [1.25.1](https://github.com/SocialGouv/mon-psy-sante/compare/v1.25.0...v1.25.1) (2022-03-31)


### Bug Fixes

* **deps:** update dependency got to v12.0.3 ([#120](https://github.com/SocialGouv/mon-psy-sante/issues/120)) ([17eebfc](https://github.com/SocialGouv/mon-psy-sante/commit/17eebfc2353809d3d00c77e8ed8badcd55b95e90))

# [1.25.0](https://github.com/SocialGouv/mon-psy-sante/compare/v1.24.0...v1.25.0) (2022-03-31)


Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monpsy",
"version": "1.25.0",
"version": "1.25.1",
"commitlint": {
"extends": [
"@commitlint/config-conventional"
Expand All @@ -22,7 +22,7 @@
"bcryptjs": "^2.4.3",
"cron": "^1.8.2",
"dotenv": "^16.0.0",
"got": "12.0.2",
"got": "12.0.3",
"graphql": "^16.3.0",
"graphql-request": "^4.0.0",
"helmet": "^5.0.2",
Expand Down Expand Up @@ -85,7 +85,7 @@
"cy": "cypress open",
"cy:run": "cypress run",
"cron:launch": "ts-node --transpile-only src/cron/launch.ts",
"script:extract-departments": "ts-node --transpile-only scripts/stats-departements.js",
"script:extract-departments": "ts-node --transpile-only scripts/stats-departements.ts",
"script:extract-emails-acceptes": "ts-node --transpile-only scripts/extract-emails-acceptes.ts"
}
}
42 changes: 28 additions & 14 deletions scripts/stats-departements.js → scripts/stats-departements.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const fs = require("fs");
const _ = require("lodash");
const { getAll, countAll } = require("../src/services/psychologists");
import fs from "fs";
import _ from "lodash";

const file = fs.createWriteStream("departments-stats.csv");
import { requestPsychologistsState } from "../src/services/demarchesSimplifiees/buildRequest";
import { getAllPsychologistList } from "../src/services/demarchesSimplifiees/import";
import { DEPARTMENTS } from "../src/types/enums/department";

const file = fs.createWriteStream("departments-stats.csv");

const extractDepNumber = (dep) => dep.split(" - ")[0];

const deps = DEPARTMENTS.map((dep) => {
return { count: 0, name: dep, department: dep.split(" - ")[0] };
return { count: 0, name: dep, department: extractDepNumber(dep) };
});

function log(text, count) {
Expand All @@ -22,14 +26,22 @@ function displayList(list) {

log("Dep", "total");
(async () => {
const count = await countAll();
console.log(`Fetching ${count} psy`);
const psychologists = await getAll({ pagesize: count, pageindex: 0 }).catch(
(e) => {
console.log("Error while fetching psy: ", e);
process.exit(-1);
}
const requestPsychologistsStateWithDep = _.bind(
requestPsychologistsState,
null,
_,
`groupeInstructeur {
label
}`
);
const result = await getAllPsychologistList(
requestPsychologistsStateWithDep
).catch((e) => {
console.log(e);
process.exit(-1);
});

const psychologists = result.psychologists;
console.log(psychologists[0]);
const sansSuite = _.filter(psychologists, { state: "sans_suite" });
const refuse = _.filter(psychologists, { state: "refuse" });
Expand All @@ -42,8 +54,10 @@ log("Dep", "total");
);

valids.forEach((psy) => {
const dep = _.find(deps, { department: psy.department });
if (!dep) console.log(">>>>>>>>>", psy.department);
const dep = _.find(deps, {
department: extractDepNumber(psy.groupeInstructeur.label),
});
if (!dep) console.log(">>>>>>>>>", psy.groupeInstructeur.label);
dep.count++;
});

Expand Down
18 changes: 15 additions & 3 deletions src/cron/demarchesSimplifiees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,43 @@ import {
getPsychologistState,
} from "../services/demarchesSimplifiees/import";
import {
countAll,
filterIdsNotInDb,
saveMany,
updateState,
} from "../services/psychologists";

async function logPsyNumber(): Promise<void> {
const count = await countAll();
console.log("Psy in DB: ", count);
}

export const importData = async (): Promise<void> => {
try {
console.log("Starting importData...");
await logPsyNumber();
const latestCursor = await models.DSCursor.findOne({
raw: true,
where: { id: 1 },
});

//@ts-ignore
const dsAPIData = await getPsychologistList(latestCursor.cursor);

console.log(dsAPIData);
if (dsAPIData.psychologists.length > 0) {
await saveMany(dsAPIData.psychologists);
console.log(`${dsAPIData.psychologists.length} saved`);
await models.DSCursor.update(
{
cursor: dsAPIData.lastCursor,
},
{ where: { id: 1 } }
);
console.log(`New cursor ${dsAPIData.lastCursor} saved`);
}

console.log(`Import done, ${dsAPIData.psychologists.length} saved`);
console.log(`Import done`);
await logPsyNumber();
} catch (err) {
Sentry.captureException(err);
console.error("ERROR: Could not import DS API data to PG", err);
Expand All @@ -42,7 +52,8 @@ export const importData = async (): Promise<void> => {

export const importState = async (): Promise<void> => {
try {
console.log("Starting importState...");
console.log("Starting importState.");
await logPsyNumber();

const dsAPIData = await getPsychologistState();

Expand All @@ -56,6 +67,7 @@ export const importState = async (): Promise<void> => {
console.log(`Added ${missingPsy.length} missing psys`);
}
console.log(`importState done.`);
await logPsyNumber();
} catch (err) {
Sentry.captureException(err);
console.error("ERROR importState: ", err);
Expand Down
4 changes: 3 additions & 1 deletion src/services/demarchesSimplifiees/buildRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const getWhereConditionAfterCursor = (cursor: string): string => {
};

export const requestPsychologistsState = async (
afterCursor: string | undefined
afterCursor: string | undefined,
extraInfos?: string | undefined
): Promise<DSResponse> => {
const paginationCondition = getWhereConditionAfterCursor(afterCursor);
const query = gql`
Expand All @@ -36,6 +37,7 @@ export const requestPsychologistsState = async (
archived
state
number
${extraInfos ?? ""}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/demarchesSimplifiees/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import parsePsychologists from "./parse-psychologists";

const limit = pLimit(5);
const getAllPsychologistList = async (
export const getAllPsychologistList = async (
graphqlFunction: (string) => Promise<DSResponse>,
cursor: string | undefined = undefined,
accumulator: DSPsychologist[] = []
Expand Down Expand Up @@ -43,7 +43,7 @@ export const getPsychologistList = async (
psychologists: Psychologist[];
lastCursor: string;
}> => {
const time = `Fetching all psychologists from DS (query id #${Math.random().toString()} with cursor ${cursor})`;
const time = `Fetch all psychologists from DS (query id #${Math.random().toString()} with cursor ${cursor})`;

console.time(time);
const list = await getAllPsychologistList(
Expand Down Expand Up @@ -79,7 +79,7 @@ export const getPsychologistFromListIds = async (
export const getPsychologistState = async (): Promise<
Partial<Psychologist>[]
> => {
const time = `Fetching all psychologists state from DS (query id #${Math.random().toString()})`;
const time = `Fetch all psychologists state from DS (query id #${Math.random().toString()})`;

console.time(time);
const list = await getAllPsychologistList(requestPsychologistsState);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4091,10 +4091,10 @@ globby@^11.0.4:
merge2 "^1.4.1"
slash "^3.0.0"

got@12.0.2:
version "12.0.2"
resolved "https://registry.yarnpkg.com/got/-/got-12.0.2.tgz#8ce4c3baa50bb18a0858d2539caa0fac19e109bf"
integrity sha512-Zi4yHiqCgaorUbknr/RHFBsC3XqjSodaw0F3qxlqAqyj+OGYZl37/uy01R0qz++KANKQYdY5FHJ0okXZpEzwWQ==
got@12.0.3:
version "12.0.3"
resolved "https://registry.yarnpkg.com/got/-/got-12.0.3.tgz#c7314daab26d42039e624adbf98f6d442e5de749"
integrity sha512-hmdcXi/S0gcAtDg4P8j/rM7+j3o1Aq6bXhjxkDhRY2ipe7PHpvx/14DgTY2czHOLaGeU8VRvRecidwfu9qdFug==
dependencies:
"@sindresorhus/is" "^4.6.0"
"@szmarczak/http-timer" "^5.0.1"
Expand Down

0 comments on commit f9bb14f

Please sign in to comment.