Skip to content

Commit

Permalink
Merge pull request #1043 from MTES-MCT/fix-boolean-filters
Browse files Browse the repository at this point in the history
fix: boolean filters isTaxed and multiOwners
  • Loading branch information
loicguillois authored Dec 18, 2024
2 parents 15b3be4 + d1e4c13 commit a2ec783
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
26 changes: 17 additions & 9 deletions frontend/src/mocks/handlers/housing-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,27 @@ export const housingHandlers: RequestHandler[] = [
});
}
),
http.post<Record<string, never>, HousingPayload, HousingCountDTO>(
http.get<Record<string, never>, 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(
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/services/housing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ export const housingApi = zlvApi.injectEndpoints({
countHousing: builder.query<HousingCount, HousingFilters>({
query: (filters) => ({
url: 'housing/count',
method: 'POST',
body: { filters }
method: 'GET',
params: {
...filters
}
}),
providesTags: (result, errors, args) => [
{
Expand Down
20 changes: 18 additions & 2 deletions server/src/controllers/housingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,24 @@ async function list(
async function count(request: Request, response: Response): Promise<void> {
logger.trace('Count housing');

const { establishmentId, role } = (request as AuthenticatedRequest).auth;
const filters = <HousingFiltersApi>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,
Expand Down
10 changes: 7 additions & 3 deletions server/src/routers/protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a2ec783

Please sign in to comment.