Skip to content

Commit

Permalink
fix: send null values instead of empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Falinor committed Jan 24, 2025
1 parent d0ce12a commit 192fc55
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 50 deletions.
29 changes: 15 additions & 14 deletions frontend/src/components/HousingEdition/HousingEditionSideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function HousingEditionSideMenu(props: HousingEditionSideMenuProps) {
occupancy: props.housing?.occupancy ?? Occupancy.UNKNOWN,
occupancyIntended: props.housing?.occupancyIntended ?? Occupancy.UNKNOWN,
status: props.housing?.status ?? HousingStatus.NEVER_CONTACTED,
subStatus: props.housing?.subStatus ?? '',
subStatus: props.housing?.subStatus ?? null,
note: ''
},
mode: 'onSubmit',
Expand Down Expand Up @@ -114,6 +114,18 @@ function HousingEditionSideMenu(props: HousingEditionSideMenuProps) {
}
});

const { data } = useFindPrecisionsQuery();
const precisionOptions = data ?? [];
const precisions =
housing?.precisions?.length && precisionOptions.length
? housing.precisions
.concat(housing?.vacancyReasons ?? [])
// Only keep the well formed precisions and vacancy reasons
// like `Dispositifs > Dispositifs incitatifs > Réserve personnelle ou pour une autre personne`
.filter((precision) => precision.split(' > ').length === 3)
.map((precision) => toNewPrecision(precisionOptions, precision))
: [];

function submit() {
if (housing) {
const { note, ...payload } = form.getValues();
Expand All @@ -129,7 +141,8 @@ function HousingEditionSideMenu(props: HousingEditionSideMenuProps) {
occupancy: payload.occupancy as Occupancy,
occupancyIntended: payload.occupancyIntended as Occupancy | null,
status: payload.status as HousingStatus,
subStatus: payload.subStatus
subStatus: payload.subStatus,
precisions: precisions.map((p) => p.id)
});
}

Expand Down Expand Up @@ -169,18 +182,6 @@ function HousingEditionSideMenu(props: HousingEditionSideMenuProps) {
}

function MobilisationTab(): ElementOf<TabsProps.Uncontrolled['tabs']> {
const { data } = useFindPrecisionsQuery();

const precisionOptions = data ?? [];
const precisions =
housing?.precisions?.length && precisionOptions.length
? housing.precisions
.concat(housing?.vacancyReasons ?? [])
// Only keep the well formed precisions and vacancy reasons
// like `Dispositifs > Dispositifs incitatifs > Réserve personnelle ou pour une autre personne`
.filter((precision) => precision.split(' > ').length === 3)
.map((precision) => toNewPrecision(precisionOptions, precision))
: [];
const mechanisms = precisions.filter((precision) =>
isPrecisionMechanismCategory(precision.category)
);
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/components/HousingList/HousingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const HousingList = ({

const [pagination, setPagination] = useState<Pagination>(DefaultPagination);
const [sort, setSort] = useState<HousingSort>();
const [updatingHousing, setUpdatingHousing] = useState<Housing | null>(null);
const [updatingHousing, setUpdatingHousing] = useState<string | null>(null);

const { housingList } = useHousingList({
filters,
Expand Down Expand Up @@ -243,7 +243,7 @@ const HousingList = ({
title="Mettre à jour"
size="small"
priority="secondary"
onClick={() => setUpdatingHousing(housing)}
onClick={() => setUpdatingHousing(housing.id)}
>
Mettre à jour
</Button>
Expand Down Expand Up @@ -337,7 +337,9 @@ const HousingList = ({
</>
)}
<HousingEditionSideMenu
housing={updatingHousing}
housing={
housingList.find((housing) => housing.id === updatingHousing) ?? null
}
expand={!!updatingHousing}
onClose={() => {
setUpdatingHousing(null);
Expand Down
18 changes: 7 additions & 11 deletions frontend/src/services/housing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { parseOwner } from './owner.service';
import { HousingCount } from '../models/HousingCount';
import { zlvApi } from './api.service';
import fp from 'lodash/fp';

export interface FindOptions
extends PaginationOptions,
Expand Down Expand Up @@ -140,16 +139,13 @@ export const housingApi = zlvApi.injectEndpoints({
query: ({ id, ...payload }) => ({
url: `housing/${id}`,
method: 'PUT',
body: fp.pick(
[
'occupancy',
'occupancyIntended',
'status',
'subStatus',
'precisions'
],
payload
)
body: {
occupancy: payload.occupancy,
occupancyIntended: payload.occupancyIntended ?? null,
status: payload.status,
subStatus: payload.subStatus?.length ? payload.subStatus : null,
precisions: payload.precisions?.length ? payload.precisions : null
} satisfies HousingUpdatePayloadDTO
}),
invalidatesTags: (result, error, payload) => [
{ type: 'Housing', id: payload.id },
Expand Down
1 change: 0 additions & 1 deletion packages/models/src/HousingDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export type HousingUpdatePayloadDTO =
// Optional, nullable keys
subStatus?: string | null;
precisions?: string[] | null;
vacancyReasons?: string[] | null;
occupancyIntended?: Occupancy | null;
};

Expand Down
8 changes: 0 additions & 8 deletions packages/schemas/src/housing-update-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export const housingUpdatePayload: ObjectSchema<HousingUpdatePayloadDTO> =
.transform((value) =>
Array.isArray(value) && value.length === 0 ? null : value
),
vacancyReasons: array()
.of(string().trim().required())
.nullable()
.optional()
.default(null)
.transform((value) =>
Array.isArray(value) && value.length === 0 ? null : value
),
occupancyIntended: string()
.oneOf(OCCUPANCY_VALUES)
.nullable()
Expand Down
5 changes: 0 additions & 5 deletions packages/schemas/src/test/housing-update-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ describe('Housing update payload', () => {
fc.constant(null),
fc.constant(undefined)
),
vacancyReasons: fc.oneof(
fc.array(fc.stringMatching(/\S/), { minLength: 1 }),
fc.constant(null),
fc.constant(undefined)
),
occupancyIntended: fc.oneof(
fc.constantFrom(...OCCUPANCY_VALUES),
fc.constant(null),
Expand Down
6 changes: 1 addition & 5 deletions server/src/controllers/housingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ describe('Housing API', () => {
),
subStatus: null,
precisions: null,
vacancyReasons: null,
occupancy: faker.helpers.arrayElement(
OCCUPANCY_VALUES.filter(
(occupancy) => occupancy !== housing.occupancy
Expand Down Expand Up @@ -627,7 +626,6 @@ describe('Housing API', () => {
status: payload.status,
subStatus: null,
precisions: payload.precisions,
vacancyReasons: payload.vacancyReasons,
occupancy: payload.occupancy,
occupancyIntended: payload.occupancyIntended
});
Expand All @@ -647,7 +645,6 @@ describe('Housing API', () => {
status: payload.status as unknown as HousingStatusApi.Blocked,
sub_status: null,
precisions: payload.precisions,
vacancy_reasons: payload.vacancyReasons,
occupancy: payload.occupancy,
occupancy_intended: payload.occupancyIntended
});
Expand Down Expand Up @@ -762,8 +759,7 @@ describe('Housing API', () => {
subStatus: housing.subStatus,
occupancy: housing.occupancy,
occupancyIntended: housing.occupancyIntended,
precisions: housing.precisions,
vacancyReasons: housing.vacancyReasons
precisions: housing.precisions
};

const { status } = await request(app)
Expand Down
4 changes: 1 addition & 3 deletions server/src/controllers/housingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ async function updateNext(
HousingUpdatePayloadDTO
>;

const precisionIds = (body.precisions ?? []).concat(
body.vacancyReasons ?? []
);
const precisionIds = body.precisions ?? [];
const [housing, precisions] = await Promise.all([
housingRepository.findOne({
id: params.id,
Expand Down

0 comments on commit 192fc55

Please sign in to comment.