diff --git a/frontend/src/mocks/handlers/housing-handlers.ts b/frontend/src/mocks/handlers/housing-handlers.ts index 7fa4be7aa..b8a0c80f4 100644 --- a/frontend/src/mocks/handlers/housing-handlers.ts +++ b/frontend/src/mocks/handlers/housing-handlers.ts @@ -57,19 +57,27 @@ export const housingHandlers: RequestHandler[] = [ }); } ), - http.post, HousingPayload, HousingCountDTO>( + http.get, HousingPayload, HousingCountDTO>( `${config.apiEndpoint}/api/housing/count`, async ({ request }) => { - const payload = await request.json(); + const url = new URL(request.url); + const queryParams = url.searchParams; + const campaignIds = + queryParams.get('campaignIds')?.split(',') ?? undefined; + const housingKinds = + queryParams.get('housingKinds')?.split(',') ?? undefined; + const status = queryParams.get('status') + ? [Number(queryParams.get('status'))] + : undefined; + const statuses = + status ?? + queryParams.get('statusList')?.split(',').map(Number) ?? + undefined; const subset: HousingDTO[] = fp.pipe( - filterByCampaign(payload.filters?.campaignIds), - filterByHousingKind(payload.filters?.housingKinds), - filterByStatus( - payload.filters?.status - ? [payload.filters.status] - : payload.filters?.statusList - ) + filterByCampaign(campaignIds), + filterByHousingKind(housingKinds), + filterByStatus(statuses) )(data.housings); const owners: number = fp.uniqBy( diff --git a/frontend/src/services/housing.service.ts b/frontend/src/services/housing.service.ts index 84de44c29..b24533e1e 100644 --- a/frontend/src/services/housing.service.ts +++ b/frontend/src/services/housing.service.ts @@ -80,8 +80,10 @@ export const housingApi = zlvApi.injectEndpoints({ countHousing: builder.query({ query: (filters) => ({ url: 'housing/count', - method: 'POST', - body: { filters } + method: 'GET', + params: { + ...filters + } }), providesTags: (result, errors, args) => [ { diff --git a/server/src/controllers/housingController.ts b/server/src/controllers/housingController.ts index b866eadb1..ef145a997 100644 --- a/server/src/controllers/housingController.ts +++ b/server/src/controllers/housingController.ts @@ -156,8 +156,24 @@ async function list( async function count(request: Request, response: Response): Promise { logger.trace('Count housing'); - const { establishmentId, role } = (request as AuthenticatedRequest).auth; - const filters = request.body.filters ?? {}; + const { auth, query } = request as AuthenticatedRequest; + + const { establishmentId, role } = auth; + + const filters: HousingFiltersApi = { + ...query, + multiOwners: query?.multiOwners?.map((value: boolean) => + value ? 'true' : 'false' + ), + roomsCounts: query?.roomsCounts?.map((value: string) => + value.toString() + ), + isTaxedValues: query?.isTaxedValues?.map((value: boolean) => + value ? 'true' : 'false' + ), + energyConsumption: + query?.energyConsumption as unknown as EnergyConsumptionGradesApi[], + }; const count = await housingRepository.count({ ...filters, diff --git a/server/src/routers/protected.ts b/server/src/routers/protected.ts index 0a416fb83..883bb9d1c 100644 --- a/server/src/routers/protected.ts +++ b/server/src/routers/protected.ts @@ -45,15 +45,19 @@ router.get( }), housingController.list ); -// TODO: replace by POST /housing router.post( '/housing', housingController.createValidators, validator.validate, housingController.create ); -// TODO: replace by GET /housing/count -router.post('/housing/count', housingController.count); +router.get('/housing/count', + validatorNext.validate({ + query: schemas.housingFilters + .concat(sortApi.sortSchema) + .concat(paginationSchema) + }), +housingController.count); router.get( '/housing/:id', housingController.getValidators,