Skip to content

Commit

Permalink
fix(backend): fix export request
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Jan 16, 2025
1 parent 8729042 commit a3642c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ export const ExportStructureUsagersControllerSecurityTests: AppTestHttpClientSec
{
label: `${CONTROLLER}.export`,
query: async (context: AppTestContext) => ({
response: await AppTestHttpClient.get("/export", {
response: await AppTestHttpClient.get("/export/TOUS", {
context,
}),
expectedStatus: expectedResponseStatusBuilder.allowStructureOnly(
context.user,
{
roles: ["responsable", "admin"],
}
),
}),
},
{
label: `${CONTROLLER}.export`,
query: async (context: AppTestContext) => ({
response: await AppTestHttpClient.get("/export/VALIDE", {
context,
}),
expectedStatus: expectedResponseStatusBuilder.allowStructureOnly(
Expand Down
24 changes: 15 additions & 9 deletions packages/backend/src/usagers/services/usagers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,23 @@ export class UsagersService {
let skip = 0;
let total = 0;

const params: any[] = [structureId];
let whereClause = 'u."structureId" = $1';
let whereClause = 'WHERE u."structureId" = $1';
let countWhereClause = `${whereClause}`;
const countParams: any[] = [structureId];

if (statut !== UsagersFilterCriteriaStatut.TOUS) {
params.push(statut);
whereClause += ` AND u.statut = $${params.length}`;
countWhereClause += ` AND u.statut = $2`;
whereClause += ` AND u.statut = $4`;
countParams.push(statut);
}

const countQuery = `
SELECT COUNT(*) as count
FROM usager u
${whereClause}
${countWhereClause}
`;

const [{ count }] = await usagerRepository.query(countQuery, params);
const [{ count }] = await usagerRepository.query(countQuery, countParams);

const query = `
SELECT
Expand Down Expand Up @@ -323,12 +325,16 @@ export class UsagersService {
FROM usager u
LEFT JOIN usager_entretien e ON e."usagerUUID" = u.uuid
${whereClause}
ORDER BY u.uuid ASC
LIMIT $${params.length + 1} OFFSET $${params.length + 2}
ORDER BY u.nom ASC
LIMIT $2 OFFSET $3
`;

while (true) {
const queryParams = [...params, chunkSize, skip];
const queryParams: any[] = [structureId, chunkSize, skip];
if (statut !== UsagersFilterCriteriaStatut.TOUS) {
queryParams.push(statut);
}

const chunk = await usagerRepository.query(query, queryParams);

if (chunk.length === 0) {
Expand Down

0 comments on commit a3642c2

Please sign in to comment.