From 2be72c6ef41566a6c50419e756035aac0bdecdb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guillois?= Date: Thu, 12 Dec 2024 15:22:25 +0100 Subject: [PATCH 1/4] fix: boolean filters (isTaxed and multiOwners --- frontend/src/services/housing.service.ts | 6 ++++-- server/src/controllers/housingController.ts | 22 ++++++++++++++++++--- server/src/routers/protected.ts | 10 +++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) 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..6424d8ffd 100644 --- a/server/src/controllers/housingController.ts +++ b/server/src/controllers/housingController.ts @@ -28,7 +28,7 @@ import HousingMissingError from '~/errors/housingMissingError'; import noteRepository from '~/repositories/noteRepository'; import { NoteApi } from '~/models/NoteApi'; import { logger } from '~/infra/logger'; -import { HousingFiltersDTO, Pagination } from '@zerologementvacant/models'; +import { HousingFiltersDTO, OCCUPANCY_VALUES, Pagination } from '@zerologementvacant/models'; import { toHousingRecordApi, toOwnerApi } from '~/scripts/shared'; import HousingExistsError from '~/errors/housingExistsError'; import ownerRepository from '~/repositories/ownerRepository'; @@ -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, From ed2e87dc4d93bc3cab544b73be3747d8bdb2c308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guillois?= Date: Thu, 12 Dec 2024 15:35:11 +0100 Subject: [PATCH 2/4] fix: lint --- server/src/controllers/housingController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/controllers/housingController.ts b/server/src/controllers/housingController.ts index 6424d8ffd..ef145a997 100644 --- a/server/src/controllers/housingController.ts +++ b/server/src/controllers/housingController.ts @@ -28,7 +28,7 @@ import HousingMissingError from '~/errors/housingMissingError'; import noteRepository from '~/repositories/noteRepository'; import { NoteApi } from '~/models/NoteApi'; import { logger } from '~/infra/logger'; -import { HousingFiltersDTO, OCCUPANCY_VALUES, Pagination } from '@zerologementvacant/models'; +import { HousingFiltersDTO, Pagination } from '@zerologementvacant/models'; import { toHousingRecordApi, toOwnerApi } from '~/scripts/shared'; import HousingExistsError from '~/errors/housingExistsError'; import ownerRepository from '~/repositories/ownerRepository'; From 6da9b49a8df083b0d796b3956900aab8bf09cc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guillois?= Date: Thu, 12 Dec 2024 16:54:22 +0100 Subject: [PATCH 3/4] fix: properly mock the API endpoint for housing count retrieval --- frontend/src/mocks/handlers/housing-handlers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/mocks/handlers/housing-handlers.ts b/frontend/src/mocks/handlers/housing-handlers.ts index 7fa4be7aa..44665aa99 100644 --- a/frontend/src/mocks/handlers/housing-handlers.ts +++ b/frontend/src/mocks/handlers/housing-handlers.ts @@ -57,13 +57,13 @@ 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 subset: HousingDTO[] = fp.pipe( - filterByCampaign(payload.filters?.campaignIds), + filterByCampaign(payload.filters?.campaignIds?.filter((id): id is string => id !== null)), filterByHousingKind(payload.filters?.housingKinds), filterByStatus( payload.filters?.status From d1e4c13d208c991bffd2915ed0eb6b8dfddbf552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guillois?= Date: Tue, 17 Dec 2024 16:34:55 +0100 Subject: [PATCH 4/4] fix: update mock housing handler (POST to GET) --- .../src/mocks/handlers/housing-handlers.ts | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/frontend/src/mocks/handlers/housing-handlers.ts b/frontend/src/mocks/handlers/housing-handlers.ts index 44665aa99..b8a0c80f4 100644 --- a/frontend/src/mocks/handlers/housing-handlers.ts +++ b/frontend/src/mocks/handlers/housing-handlers.ts @@ -60,16 +60,24 @@ export const housingHandlers: RequestHandler[] = [ 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?.filter((id): id is string => id !== null)), - 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(