Skip to content

Commit

Permalink
Merge pull request #340 from mission-apprentissage/ui-server/add-opco…
Browse files Browse the repository at this point in the history
…-scope

[UI - Serveur] Ajout du scope OBSERVER OPCO
  • Loading branch information
yohanngab authored Oct 29, 2024
2 parents e1aa54b + 5708c93 commit cae2c0b
Show file tree
Hide file tree
Showing 8 changed files with 4,967 additions and 6 deletions.
1 change: 1 addition & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@ export const OBSERVER_SCOPES = {
REGION: "region",
SIRETS: "sirets",
NATIONAL: "national",
OPCO: "opco",
};
9 changes: 7 additions & 2 deletions server/src/dao/campagnes.dao.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sql } from "kysely";

import { OBSERVER_SCOPES } from "../constants";
import { getKbdClient } from "../db/db";
import type { ObserverScope } from "../types";

Expand Down Expand Up @@ -81,11 +82,15 @@ export const getAllWithTemoignageCountAndTemplateName = async ({
.groupBy(["campagnes.id", "questionnaires.id", "formations.id", "etablissements.id"])
.orderBy("campagnes.created_at", "desc");

if (scope && scope.field && scope.field !== "sirets" && scope.value) {
if (scope && scope.field && scope.field !== "sirets" && scope.field !== OBSERVER_SCOPES.OPCO && scope.value) {
baseQuery = baseQuery.where(`formations.${scope.field}`, "=", scope.value);
}

if (scope && scope.field && scope.field === "sirets" && scope.value.length) {
if (scope && scope.field && scope.field === OBSERVER_SCOPES.OPCO && scope.value.length) {
baseQuery = baseQuery.where(sql`formations.catalogue_data ->> 'rncp_code'`, "in", scope.value);
}

if (scope && scope.field && scope.field === OBSERVER_SCOPES.SIRETS && scope.value.length) {
baseQuery = baseQuery.where(`formations.etablissement_gestionnaire_siret`, "in", scope.value);
}

Expand Down
9 changes: 7 additions & 2 deletions server/src/dao/formations.dao.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DeleteResult } from "kysely";
import { sql } from "kysely";

import { OBSERVER_SCOPES } from "../constants";
import { getKbdClient } from "../db/db";
import type { Formation, ObserverScope } from "../types";

Expand Down Expand Up @@ -337,13 +338,17 @@ export const findAllWithCampagnesCount = async (
);
}

if (scope && scope.field && scope.field !== "sirets" && scope.value) {
if (scope && scope.field && scope.field !== "sirets" && scope.field !== OBSERVER_SCOPES.OPCO && scope.value) {
baseQuery = baseQuery.where(`formations.${scope.field}`, "=", scope.value);
}

if (scope && scope.field && scope.field === "sirets" && scope.value.length) {
if (scope && scope.field && scope.field === OBSERVER_SCOPES.SIRETS && scope.value.length) {
baseQuery = baseQuery.where(`formations.etablissement_gestionnaire_siret`, "in", scope.value);
}

if (scope && scope.field && scope.field === OBSERVER_SCOPES.OPCO && scope.value.length) {
baseQuery = baseQuery.where(sql`formations.catalogue_data ->> 'rncp_code'`, "in", scope.value);
}

return baseQuery.execute();
};
13 changes: 12 additions & 1 deletion server/src/services/campagnes.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-nocheck -- TODO

import { DIPLOME_TYPE_MATCHER } from "../constants";
import fs from "fs";

import { DIPLOME_TYPE_MATCHER, OBSERVER_SCOPES } from "../constants";
import * as campagnesDao from "../dao/campagnes.dao";
import * as etablissementsDao from "../dao/etablissements.dao";
import * as formationsDao from "../dao/formations.dao";
Expand All @@ -11,13 +13,22 @@ import * as catalogue from "../modules/catalogue";
import * as pdfExport from "../modules/pdfExport";
import * as xlsxExport from "../modules/xlsxExport";
import { appendDataWhenEmpty, getMedianDuration, getStatistics } from "../utils/campagnes.utils";
import { getStaticFilePath } from "../utils/getStaticFilePath";
import { getChampsLibreField } from "../utils/verbatims.utils";

export const getCampagnes = async ({ isObserver, scope, page = 1, pageSize = 10, query, search }) => {
try {
let campagnes = [];

if (isObserver) {
// Nécessaire pour ne pas stocker la liste de code RNCP dans le scope d'un user et réconcilier les labels/valeurs
if (scope.field === OBSERVER_SCOPES.OPCO) {
const SCOPE_LIST = getStaticFilePath("./opco.json");
const opcos = JSON.parse(fs.readFileSync(SCOPE_LIST, "utf8"));
const rncpCodes = opcos.find((opco) => opco.label === scope.value).value;

scope.value = rncpCodes;
}
campagnes = await campagnesDao.getAllWithTemoignageCountAndTemplateName({ scope, query });
} else {
campagnes = await campagnesDao.getAllWithTemoignageCountAndTemplateName({ query });
Expand Down
13 changes: 13 additions & 0 deletions server/src/services/formations.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @ts-nocheck -- TODO

import fs from "fs";

import { OBSERVER_SCOPES } from "../constants";
import * as formationsDao from "../dao/formations.dao";
import { getStaticFilePath } from "../utils/getStaticFilePath";

export const createFormation = async (formation) => {
try {
Expand Down Expand Up @@ -84,6 +88,15 @@ export const updateFormation = async (id, updatedFormation) => {

export const getFormationsEtablissementsDiplomesWithCampagnesCount = async ({ userSiret, scope }) => {
try {
// Nécessaire pour ne pas stocker la liste de code RNCP dans le scope d'un user et réconcilier les labels/valeurs
if (scope.field === OBSERVER_SCOPES.OPCO) {
const SCOPE_LIST = getStaticFilePath("./opco.json");
const opcos = JSON.parse(fs.readFileSync(SCOPE_LIST, "utf8"));
const rncpCodes = opcos.find((opco) => opco.label === scope.value).value;

scope.value = rncpCodes;
}

const formations = await formationsDao.findAllWithCampagnesCount(userSiret, scope);

const formattedByDiplome = formations.reduce((acc, formation) => {
Expand Down
Loading

0 comments on commit cae2c0b

Please sign in to comment.