Skip to content

Commit

Permalink
feat(bo): list territoires filter and dsfrV2 760
Browse files Browse the repository at this point in the history
  • Loading branch information
l-scherer committed Dec 12, 2024
1 parent e410f6e commit 48656c5
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 143 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/demandeSejour/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = async function get(req, res, next) {
total: demandes.total,
});
} catch (error) {
console.log(error);
log.w(error);
log.w("DONE with error");
return next(error);
}
Expand Down
18 changes: 13 additions & 5 deletions packages/backend/src/controllers/territoire/list.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
const Territoire = require("../../services/Territoire");

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

const log = logger(module.filename);

module.exports = async function list(_req, res) {
module.exports = async function list(req, res, next) {
log.i("IN");
const territoires = await Territoire.fetch();
log.i("DONE");
return res.json({ territoires });
try {
const territoires = await Territoire.fetch(req.query);
log.d(territoires);
return res.status(200).json({
territoires: territoires.rows,
total: territoires.total,
});
} catch (error) {
log.w(error);
log.w("DONE with error");
return next(error);
}
};
10 changes: 6 additions & 4 deletions packages/backend/src/helpers/__tests__/queryParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ describe("queryParams", () => {
it("should return the original query and params if no filters are provided", () => {
const query = "SELECT * FROM users";
const result = applyFilters(query, [], {});
expect(result.query).toBe(query);
expect(result.query.trim()).toBe(query);
expect(result.params).toEqual([]);
});

it("should apply filters to the query", () => {
const query = "SELECT * FROM users";
const filterParams = { age: "30", name: "John" };
const result = applyFilters(query, [], filterParams);
const groupBy = "";
const result = applyFilters(query, [], filterParams, groupBy);
// remove return at line to fix space pb
expect(result.query.replace(/\s+/g, " ").trim()).toContain(
"unaccent(age::text) ILIKE '%' || unaccent($1) || '%'",
Expand All @@ -31,14 +32,15 @@ describe("queryParams", () => {
it("should handle array filters with ANY", () => {
const query = "SELECT * FROM users";
const filterParams = { roles: ["admin", "user"] };
const result = applyFilters(query, [], filterParams);
const groupBy = "";
const result = applyFilters(query, [], filterParams, groupBy);
expect(result.query).toContain("roles = ANY($1)");
expect(result.params).toEqual([["admin", "user"]]);
});

it("should return the original query if no parameters are provided", () => {
const result = applyFilters();
expect(result.query).toBe("");
expect(result.query.trim()).toBe("");
expect(result.params).toEqual([]);
});

Expand Down
18 changes: 15 additions & 3 deletions packages/backend/src/helpers/queryParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
if (Object.keys(filterParams).length === 0) {
return {
params: initialParams,
query,
query: `${query}`,
};
}
const filters = Object.entries(filterParams)
Expand All @@ -21,6 +21,19 @@ module.exports = {
query: `${query} AND ${filters}`,
};
},
applyGroupBy: (queryInitial, groupByParams = []) => {
if (Object.keys(groupByParams).length === 0) {
return `${queryInitial}`;
}
console.log("groupByParams", groupByParams);
const group = Object.entries(groupByParams)
.map(([key, value]) => {

Check failure on line 30 in packages/backend/src/helpers/queryParams.js

View workflow job for this annotation

GitHub Actions / lint-format-test

'key' is defined but never used
return value;
})
.join(", ");
return `${queryInitial} GROUP BY ${group}`;
},

applyPagination: (
query,
params,
Expand All @@ -39,15 +52,14 @@ module.exports = {
const countQuery = `
SELECT COUNT(*) AS total FROM (${query}) AS subquery;
`;

return {
countQuery,
countQueryParams: params,
params: [...params, limit, offset],
query: paginatedQuery,
};
},
sanityzeFiltersParams: (queryParams, availableParams) =>
sanityzeFiltersParams: (queryParams, availableParams) =>
Object.entries(availableParams).reduce((acc, [key, value]) => {
if (queryParams[key]) {
acc[value] = queryParams[key];
Expand Down
92 changes: 66 additions & 26 deletions packages/backend/src/services/Territoire.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ const logger = require("../utils/logger");
const pool = require("../utils/pgpool").getPool();

const log = logger(module.filename);
const {
sanityzePaginationParams,
sanityzeFiltersParams,
applyFilters,
applyPagination,
applyGroupBy,
} = require("../helpers/queryParams");

const query = {
getFicheIdByTerCode: `
select
fte.id AS id
FROM back.fiche_territoire fte
WHERE ter_code = $1`,
getOne: `
select
fte.id AS territoire_id,
Expand All @@ -23,31 +35,25 @@ const query = {
FROM back.fiche_territoire fte
INNER JOIN geo.territoires ter ON fte.ter_code = ter.code
WHERE fte.id = $1`,
getFicheIdByTerCode: `
select
fte.id AS id
FROM back.fiche_territoire fte
WHERE ter_code = $1`,
select: `
select: () =>
`
select
fte.id AS territoire_id,
fte.id AS "territoireId",
CASE (ter.code ~ '[0-9]')
WHEN true THEN 'DEP'
ELSE 'REG'
END AS type,
ter.code AS value,
ter.label AS text,
fte.service_telephone AS service_telephone,
fte.corresp_vao_nom AS corresp_vao_nom,
fte.corresp_vao_prenom AS corresp_vao_prenom,
fte.service_mail as service_mail,
COUNT(distinct(usr.id)) as nbusersbo
ter.code AS code,
ter.label AS label,
fte.corresp_vao_prenom AS "correspVaoPrenom",
fte.corresp_vao_nom AS "correspVaoNom",
fte.service_mail AS "serviceMail",
fte.service_telephone AS "serviceTelephone",
COUNT(distinct(usr.id)) as "nbUsersBo"
FROM geo.territoires ter
INNER JOIN back.fiche_territoire fte ON fte.ter_code = ter.code
LEFT JOIN back.users usr ON usr.ter_code = ter.code
WHERE code <> 'FRA'
GROUP BY territoire_id,type,value,text,service_telephone,corresp_vao_nom,corresp_vao_prenom,service_mail
ORDER BY type, text ASC`,
WHERE ter.code <> 'FRA'`,
update: `
UPDATE back.fiche_territoire
SET
Expand All @@ -62,17 +68,51 @@ const query = {
`,
};

module.exports.fetch = async (criterias = {}) => {
log.i("fetch - IN");
const { rows } = await pool.query(query.select);
module.exports.fetch = async (queryParams) => {
const titles = {
label: "ter.label",
code: "ter.code",
};
const groupBy = [
"fte.id",
"type",
"code",
"label",
"fte.service_telephone",
"fte.corresp_vao_nom",
"fte.corresp_vao_prenom",
"fte.service_mail",
];

return rows.filter((territoire) => {
return Object.entries(criterias).every(
([key, value]) => territoire[key] == value,
);
});
};
const { limit, offset, sortBy, sortDirection } = sanityzePaginationParams(
queryParams,
{
sortBy: titles,
},
);
const filterParams = sanityzeFiltersParams(queryParams, titles);

const queryGet = query.select();
const filterQuery = applyFilters(queryGet, [], filterParams, groupBy);
filterQuery.query = applyGroupBy(filterQuery.query, groupBy);
const paginatedQuery = applyPagination(
filterQuery.query,
filterQuery.params,
limit,
offset,
sortBy,
sortDirection,
);
console.log("paginatedQuery", paginatedQuery.query);
const result = await Promise.all([
pool.query(paginatedQuery.query, paginatedQuery.params),
pool.query(paginatedQuery.countQuery, paginatedQuery.countQueryParams),
]);
return {
rows: result[0].rows,
total: parseInt(result[1].rows[0].total, 10),
};
};

module.exports.readFicheIdByTerCode = async (territoireCode) => {
log.i("readFicheIdByTerCode - IN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const updateData = () => {
...(isValidParams(sortDirection.value)
? { sortDirection: sortDirection.value.toUpperCase() }
: {}),
search: getSearchParams(),
...getSearchParams(),
};
demandeSejourStore.fetchDemandes(query);
Expand Down
Loading

0 comments on commit 48656c5

Please sign in to comment.