Skip to content

Commit

Permalink
fix: various bugs (#715)
Browse files Browse the repository at this point in the history
## Tickets liés
Close #830 #833
tickets liés : 

## Description

fix de deux bug (cf jira). J'ai ajouté un fix pour l tri sur le nom des
hebergement qui ne marchait pas.

De plus, l'utilisation de `sort by LOWER(<var>)` ne fonctionnait pas si
`<var>`pas de type `varchar` => cast

### dont régressions potentielles à tester

## Screenshot / liens loom 

## Check-list

 - [x] ~~Ma branche est rebase sur main~~
- [x] Des tests ont été écrits pour tous les endpoints créés ou modifiés
 - [x] Refacto "à la volée" des parties sur lesquelles j'ai codée
 - [x] Plus de `console.log`
  • Loading branch information
benjaminDNUM authored Feb 6, 2025
2 parents 934ccc5 + 87b7782 commit 3aad16a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/helpers/__tests__/queryParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("queryParams", () => {
const query = "SELECT * FROM users";
const params = [];
const result = applyPagination(query, params, 10, 5, "name", "DESC");
expect(result.query).toContain("ORDER BY LOWER(name) DESC");
expect(result.query).toContain("ORDER BY LOWER(name::varchar) DESC");
expect(result.query).toContain("LIMIT $1");
expect(result.query).toContain("OFFSET $2");
expect(result.params).toEqual([10, 5]);
Expand Down
20 changes: 15 additions & 5 deletions packages/backend/src/helpers/queryParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = {
) => {
const paginatedQuery = `
${query}
${sortBy ? `ORDER BY LOWER(${sortBy}) ${sortDirection}` : ""}
${sortBy ? `ORDER BY LOWER(${sortBy}::varchar) ${sortDirection}` : ""}
LIMIT $${params.length + 1}
OFFSET $${params.length + 2};
`;
Expand Down Expand Up @@ -114,7 +114,6 @@ module.exports = {
titles,
defaultParams = {},
) => {
const defaultSortDirection = ["", "ASC", "DESC"];
const defaultLimit = 10;
const defaultOffset = 0;
return {
Expand All @@ -125,9 +124,10 @@ module.exports = {
? (defaultParams?.offset ?? defaultOffset)
: parseInt(offset, 10),
sortBy: getSort(sortBy, titles, defaultParams.sortBy),
sortDirection: defaultSortDirection.includes(sortDirection)
? sortDirection
: "",
sortDirection: getSortDirection(
sortDirection,
defaultParams.sortDirection,
),
};
},
};
Expand All @@ -141,3 +141,13 @@ const getSort = (sortBy, titles, defaultSort = "") => {
}
return defaultSort;
};

const getSortDirection = (sortDirection, defaultSortDirection) => {
const possibleSortDirections = ["", "ASC", "DESC"];
if (sortDirection) {
return possibleSortDirections.includes(sortDirection) ? sortDirection : "";
}
return possibleSortDirections.includes(defaultSortDirection)
? defaultSortDirection
: "";
};
4 changes: 3 additions & 1 deletion packages/backend/src/services/DemandeSejour.js
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,10 @@ module.exports.get = async (organismesId, queryParams) => {
];
const { limit, offset, sortBy, sortDirection } = sanitizePaginationParams(
queryParams,
titles,
{
sortBy: titles,
sortBy: "ds.edited_at",
sortDirection: "DESC",
},
);
const filterParams = sanitizeFiltersParams(queryParams, titles);
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/services/hebergement/Hebergement.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ module.exports.getByDepartementCodes = async (departementsCodes, params) => {
return rows[0];
};


module.exports.getByUserId = async (userId, queryParams) => {
log.i("getByUserId - IN", { userId });
const queryParamsWithUserId = { ...queryParams, userId };
Expand All @@ -567,6 +566,7 @@ module.exports.getByUserId = async (userId, queryParams) => {
filterEnabled: true,
key: "h.nom",
queryKey: "nom",
sortEnabled: true,
type: "default",
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import {
} from "@vao/shared";
import dayjs from "dayjs";
const toaster = useToaster();
const demandeSejourStore = useDemandeSejourStore();
const userStore = useUserStore();
const route = useRoute();
Expand Down

0 comments on commit 3aad16a

Please sign in to comment.