Skip to content

Commit 188a183

Browse files
committed
wip
1 parent b7c3f27 commit 188a183

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Hebergement = require("../../services/hebergement/Hebergement");
2+
const logger = require("../../utils/logger");
3+
4+
const log = logger(module.filename);
5+
6+
module.exports = async function list(req, res, next) {
7+
log.i("IN");
8+
const departements = req.departements.map((d) => d.value);
9+
try {
10+
const result = await Hebergement.getByDepartementCodes2(
11+
req.query,
12+
departements,
13+
);
14+
log.d(result);
15+
return res.status(200).json(result);
16+
} catch (error) {
17+
log.w("DONE with error");
18+
return next(error);
19+
}
20+
};

packages/backend/src/controllers/hebergement/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports.get = require("./get");
22
module.exports.getByDepartements = require("./getByDepartements");
3+
module.exports.getByDepartements2 = require("./getByDepartements2");
34
module.exports.getById = require("./getById");
45
module.exports.getBySiren = require("./getBySiren");
56
module.exports.getExtract = require("./getExtract");

packages/backend/src/routes/hebergement.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ router.get(
1818
hebergementController.getByDepartements,
1919
);
2020

21+
router.get(
22+
"/admin2/",
23+
boCheckJWT,
24+
getDepartements,
25+
hebergementController.getByDepartements2,
26+
);
27+
2128
router.get(
2229
"/extract/",
2330
boCheckJWT,

packages/backend/src/services/hebergement/Hebergement.js

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ ${new Array(nbRows)
177177
`,
178178
[departementCodes, search, limit, offset],
179179
],
180+
getByDepartementCodes2: () => `
181+
SELECT
182+
H.ID AS "id",
183+
A.DEPARTEMENT AS "departement",
184+
H.EMAIL AS EMAIL,
185+
H.TELEPHONE_1 AS TELEPHONE,
186+
H.NOM_GESTIONNAIRE AS "nomGestionnaire",
187+
A.LABEL AS ADRESSE,
188+
H.NOM AS "nom",
189+
( SELECT VALUE FROM FRONT.HEBERGEMENT_TYPE WHERE ID = H.ID) AS "typeHebergement",
190+
H.VISITE_LOCAUX_AT AS "dateVisite",
191+
H.REGLEMENTATION_ERP AS "reglementationErp"
192+
FROM
193+
FRONT.HEBERGEMENT H
194+
LEFT JOIN FRONT.ADRESSE A ON A.ID = H.ADRESSE_ID
195+
WHERE
196+
A.DEPARTEMENT = ANY($1)
197+
AND (
198+
unaccent(h.nom) ILIKE '%' || unaccent($2) || '%'
199+
OR h.email ILIKE '%' || $2 || '%'
200+
OR unaccent(a.label) ILIKE '%' || unaccent($2) || '%'
201+
)
202+
AND CURRENT IS TRUE
203+
AND h.statut_id = (SELECT id FROM front.hebergement_statut WHERE value = 'actif');
204+
`,
180205
getById: `
181206
SELECT
182207
id,
@@ -311,12 +336,12 @@ ${new Array(nbRows)
311336
id = $1
312337
`,
313338
updateStatut: `
314-
UPDATE
315-
front.hebergement
339+
UPDATE
340+
front.hebergement
316341
SET statut_id = (SELECT id FROM front.hebergement_statut WHERE value = $3),
317342
edited_by = $2,
318343
edited_at = NOW()
319-
WHERE id = $1
344+
WHERE id = $1
320345
AND current = TRUE
321346
`,
322347
};
@@ -547,7 +572,6 @@ module.exports.updateStatut = async (userId, hebergementId, statut) => {
547572
return hebergementId;
548573
};
549574

550-
551575
module.exports.getByDepartementCodes = async (departementsCodes, params) => {
552576
log.i("getByDepartementCodes - IN");
553577
const { rows } = await pool.query(
@@ -556,6 +580,79 @@ module.exports.getByDepartementCodes = async (departementsCodes, params) => {
556580
log.d("getByDepartementCodes - DONE");
557581
return rows[0];
558582
};
583+
module.exports.getByDepartementCodes2 = async (
584+
departementsCodes,
585+
queryParams,
586+
) => {
587+
const titles = [
588+
{
589+
key: "h.nom",
590+
queryKey: "nom",
591+
sortEnabled: true,
592+
type: "default",
593+
},
594+
{
595+
key: "a.departement",
596+
queryKey: "departement",
597+
sortEnabled: true,
598+
type: "default",
599+
},
600+
{
601+
key: "a.label",
602+
queryKey: "adresse",
603+
sortEnabled: true,
604+
type: "default",
605+
},
606+
{
607+
key: " h.telephone_1",
608+
queryKey: "telephone",
609+
sortEnabled: true,
610+
type: "default",
611+
},
612+
{
613+
key: " h.email",
614+
queryKey: "email",
615+
sortEnabled: true,
616+
type: "default",
617+
},
618+
{
619+
key: "h.visite_locaux_at",
620+
queryKey: "dateVisite",
621+
sortEnabled: true,
622+
type: "date",
623+
},
624+
{
625+
key: "h.reglementation_erp",
626+
queryKey: "reglementationErp",
627+
sortEnabled: true,
628+
type: "default",
629+
},
630+
];
631+
const filterParams = sanitizeFiltersParams(queryParams, titles);
632+
const queryGet = query.getByDepartementCodes2();
633+
const params = [...departementsCodes];
634+
const filterQuery = applyFilters(queryGet, params, filterParams);
635+
const { limit, offset, sortBy, sortDirection } = sanitizePaginationParams(
636+
queryParams,
637+
titles,
638+
);
639+
const paginatedQuery = applyPagination(
640+
filterQuery.query,
641+
filterQuery.params,
642+
limit,
643+
offset,
644+
sortBy,
645+
sortDirection,
646+
);
647+
const result = await Promise.all([
648+
pool.query(paginatedQuery.query, paginatedQuery.params),
649+
pool.query(paginatedQuery.countQuery, paginatedQuery.countQueryParams),
650+
]);
651+
return {
652+
rows: result[0].rows,
653+
total: parseInt(result[1].rows[0].total, 10),
654+
};
655+
};
559656

560657
module.exports.getByUserId = async (userId, queryParams) => {
561658
log.i("getByUserId - IN", { userId });

0 commit comments

Comments
 (0)