Skip to content

Commit

Permalink
fix(hebergement): refacto getters
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Nov 21, 2024
1 parent 078537f commit e8c37f9
Show file tree
Hide file tree
Showing 25 changed files with 555 additions and 243 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/demandeSejour/depose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const dayjs = require("dayjs");
const yup = require("yup");

const DemandeSejour = require("../../services/DemandeSejour");
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const Send = require("../../services/mail").mailService.send;
const PdfDeclaration2Mois = require("../../services/pdf/declaration2mois/generate");
const PdfDeclaration8jours = require("../../services/pdf/declaration8jours/generate");
Expand Down
38 changes: 0 additions & 38 deletions packages/backend/src/controllers/demandeSejour/getHebergement.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = async function get(req, res) {
sort: titleSorted ?? "nom",
},
);

return res.status(200).json({
count: data.total_count,
hebergements: data.hebergements || [],
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/controllers/demandeSejour/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module.exports.get = require("./get");
module.exports.getAdminStats = require("./getAdminStats");
module.exports.getExtract = require("./getExtract");
module.exports.getExtractHebergement = require("./getExtractHebergement");
module.exports.getHebergement = require("./getHebergement");
module.exports.getHebergementsByDepartementCodes = require("./getHebergementsByDepartementCodes");
module.exports.getById = require("./getById");
module.exports.getByIdBo = require("./getByIdBo");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");

const logger = require("../../utils/logger");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");

const logger = require("../../utils/logger");
const Sentry = require("@sentry/node");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/getById.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const AppError = require("../../utils/error");

const logger = require("../../utils/logger");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/getExtract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const logger = require("../../utils/logger");
const dayjs = require("dayjs");

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const yup = require("yup");
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const HebergementSchema = require("../../schemas/hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const yup = require("yup");

const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const HebergementSchema = require("../../schemas/hebergement");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,33 @@ async function checkPermissionDeclarationSejour(req, res, next) {

// Requête SQL simplifiée, ne récupérant que les informations brutes nécessaires
const query = `
SELECT ds.id, ds.hebergement, o.personne_morale, agr.region_obtention
SELECT ds.id, o.personne_morale, agr.region_obtention
FROM front.demande_sejour ds
INNER JOIN front.organismes o ON o.id = ds.organisme_id
LEFT JOIN front.agrements agr ON agr.organisme_id = ds.organisme_id
WHERE ds.id = $1
`;

const queryHebegements = `
SELECT DISTINCT
A.DEPARTEMENT AS DEPARTEMENT
FROM
FRONT.DEMANDE_SEJOUR_TO_HEBERGEMENT DSTH
INNER JOIN FRONT.HEBERGEMENT H ON H.ID = DSTH.HEBERGEMENT_ID
INNER JOIN FRONT.ADRESSE A ON A.ID = H.ADRESSE_ID
WHERE
DEMANDE_SEJOUR_ID = $1
`;

let sejour;
let departementsHebergements;
try {
const { rows } = await pool.query(query, [declarationId]);
departementsHebergements =
(await pool.query(queryHebegements, [declarationId]))?.rows.map(
(r) => r.departement,
) ?? [];

if (!rows || rows.length === 0) {
return next(
new AppError(
Expand All @@ -54,14 +71,9 @@ async function checkPermissionDeclarationSejour(req, res, next) {
);
}

const { hebergement, personne_morale, region_obtention } = sejour;
const { personne_morale, region_obtention } = sejour;

// Traitement des données JSON pour vérifier les départements
const hebergements = hebergement?.hebergements || [];
const departementsHebergements = hebergements.map(
(h) => h.coordonnees?.adresse?.departement,
);

const hasValidDepartement = departements.some((dep) =>
departementsHebergements.includes(dep.value),
);
Expand Down
7 changes: 0 additions & 7 deletions packages/backend/src/routes/sejour.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ router.get(
getDepartements,
demandeSejourController.getHebergementsByDepartementCodes,
);
router.get(
"/admin/hebergement/:sejourId/:hebergementId",
boCheckJWT,
boCheckRoleDS,
getDepartements,
demandeSejourController.getHebergement,
);
router.get(
"/admin/historique/:declarationId",
boCheckJWT,
Expand Down
Loading

0 comments on commit e8c37f9

Please sign in to comment.