Skip to content

Commit

Permalink
Merge pull request #1025 from MTES-MCT/fix-add-housing
Browse files Browse the repository at this point in the history
Fix a bug where housings could not be added
  • Loading branch information
Falinor authored Dec 3, 2024
2 parents 1093489 + e1822d5 commit dfa5b32
Show file tree
Hide file tree
Showing 28 changed files with 542 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ function HousingDetailsCardOccupancy({ housing, lastOccupancyEvent }: Props) {
? housing.vacancyStartYear
: undefined;

function situationSince(
occupancy: string,
lastOccupancyChange: number | undefined
): string {
function situationSince(lastOccupancyChange: number | undefined): string {
if (lastOccupancyChange === undefined) {
return 'Inconnu';
}
Expand Down Expand Up @@ -77,9 +74,7 @@ function HousingDetailsCardOccupancy({ housing, lastOccupancyEvent }: Props) {
<Grid container rowSpacing={3} xs>
<Grid xs={4}>
<LabelNext component="h3">Dans cette situation depuis</LabelNext>
<Typography>
{situationSince(housing.occupancy, lastOccupancyChange)}
</Typography>
<Typography>{situationSince(lastOccupancyChange)}</Typography>
</Grid>
<Grid xs={4}>
<LabelNext component="h3">Source</LabelNext>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/mocks/handlers/housing-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const housingHandlers: RequestHandler[] = [

// Add a housing
http.post<never, HousingPayloadDTO, HousingDTO>(
`${config.apiEndpoint}/api/housing/creation`,
`${config.apiEndpoint}/api/housing`,
async ({ request }) => {
const payload = await request.json();
const datafoncierHousing = data.datafoncierHousings.find(
Expand Down
45 changes: 23 additions & 22 deletions frontend/src/models/Housing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { differenceInDays, format } from 'date-fns';
import { List } from 'immutable';
import { match, Pattern } from 'ts-pattern';

import {
Expand Down Expand Up @@ -236,37 +237,37 @@ export const getOccupancy = (
) => (occupancy && occupancy.length > 0 ? occupancy : OccupancyUnknown);

export function getSource(housing: Housing): string {

const labels: Record<string, string> = {
'lovac': 'LOVAC',
'ff': 'Fichiers fonciers',
lovac: 'LOVAC',
ff: 'Fichiers fonciers',
'datafoncier-import': 'Fichiers fonciers',
'datafoncier-manual': 'Fichiers fonciers'
};

const aggregatedData: Record<string, string[]> = {};
const years = List(housing.dataFileYears)
.groupBy((dataFileYear) => dataFileYear.split('-').at(0) as string)
.map((dataFileYears) =>
dataFileYears.map(
(dataFileYear) => dataFileYear.split('-').at(1) as string
)
)
.filter((years) => !years.isEmpty())
.reduce((acc, years, source) => {
const label = labels[source] ?? source;
return acc.concat(`${label} (${years.join(', ')})`);
}, List<string>());

const source = housing.source ? labels[housing.source] : null;

let result = null;
if(housing.dataFileYears.length > 0) {
housing.dataFileYears.forEach((item) => {
const [name, year] = item.split('-');
if (!aggregatedData[name]) {
aggregatedData[name] = [];
}
aggregatedData[name].push(year);
});
if (!years.isEmpty()) {
return years.join(', ');
}

result = Object.keys(aggregatedData)
.map((name) => {
const years = aggregatedData[name].join(', ');
return labels[name] + ' (' + years + ')';
})
.join(', ');
} else if (housing.source) {
result = labels[housing.source];
if (source) {
return source;
}

return result || 'Inconnue';
return 'Inconnue';
}

export function toHousingDTO(housing: Housing): HousingDTO {
Expand Down
16 changes: 9 additions & 7 deletions frontend/src/models/test/Housing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ describe('Housing', () => {

describe('getSource', () => {
it.each`
dataFileYears | expected
${['lovac-2019']} | ${'LOVAC (2019)'}
${['lovac-2020', 'lovac-2021']} | ${'LOVAC (2020, 2021)'}
${['ff-2020', 'ff-2021', 'lovac-2021']} | ${'Fichiers fonciers (2020, 2021), LOVAC (2021)'}
dataFileYears | source | expected
${['lovac-2019']} | ${'lovac'} | ${'LOVAC (2019)'}
${['lovac-2020', 'lovac-2021']} | ${'lovac'} | ${'LOVAC (2020, 2021)'}
${['ff-2020', 'ff-2021', 'lovac-2021']} | ${'lovac'} | ${'Fichiers fonciers (2020, 2021), LOVAC (2021)'}
${[]} | ${'datafoncier-manual'} | ${'Fichiers fonciers'}
${[]} | ${'datafoncier-import'} | ${'Fichiers fonciers'}
`(
'should format $dataFileYears to $expected',
({ dataFileYears, expected }) => {
const housing: Housing = { ...genHousing(), dataFileYears };
`should format $dataFileYears and $source to $expected`,
({ dataFileYears, source, expected }) => {
const housing: Housing = { ...genHousing(), dataFileYears, source };

const actual = getSource(housing);

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/services/housing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const housingApi = zlvApi.injectEndpoints({
// TODO: fix this any type
createHousing: builder.mutation<Housing, any>({
query: (payload) => ({
url: 'housing/creation',
url: 'housing',
method: 'POST',
body: payload
}),
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/views/Housing/test/HousingView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import {
HousingDTO,
HousingOwnerDTO,
Occupancy,
OwnerDTO
} from '@zerologementvacant/models';
import {
Expand Down Expand Up @@ -62,6 +63,50 @@ describe('Housing view', () => {
expect(name).toBeVisible();
});

describe('Show housing details', () => {
describe('Vacancy start year', () => {
it('should be unknown', async () => {
housing.occupancy = Occupancy.RENT;
housing.vacancyStartYear = undefined;

renderView(housing);

const vacancyStartYear = await screen
.findByText(/^Dans cette situation depuis/)
// eslint-disable-next-line testing-library/no-node-access
.then((label) => label.nextElementSibling);
expect(vacancyStartYear).toHaveTextContent('Inconnu');
});

it('should be defined', async () => {
housing.occupancy = Occupancy.VACANT;
housing.vacancyStartYear = faker.date.past().getFullYear();

renderView(housing);

const vacancyStartYear = await screen
.findByText(/^Dans cette situation depuis/)
// eslint-disable-next-line testing-library/no-node-access
.then((label) => label.nextElementSibling);
expect(vacancyStartYear).toHaveTextContent('Moins d’un an');
});
});

describe('Source', () => {
it('should be "Fichiers fonciers (2023)"', async () => {
housing.dataFileYears = ['ff-2023'];

renderView(housing);

const source = await screen
.findByText(/^Source/)
// eslint-disable-next-line testing-library/no-node-access
.then((label) => label.nextElementSibling);
expect(source).toHaveTextContent('Fichiers fonciers (2023)');
});
});
});

describe('Update owner details', () => {
it('should update their name', async () => {
renderView(housing);
Expand Down
9 changes: 8 additions & 1 deletion packages/models/src/DatafoncierHousing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fp from 'lodash/fp';

import { Occupancy, OCCUPANCY_VALUES } from './Occupancy';

/**
* @see http://doc-datafoncier.cerema.fr/ff/doc_fftp/table/pb0010_local/last/
*/
Expand Down Expand Up @@ -57,7 +59,7 @@ export interface DatafoncierHousing {
dloy48a: number | null;
top48a: string;
dnatlc: string;
ccthp: string;
ccthp: string | null;
proba_rprs: string;
typeact: string | null;
loghvac: string | null;
Expand Down Expand Up @@ -140,3 +142,8 @@ export function toAddress(housing: DatafoncierHousing): string {
const city = housing.idcomtxt;
return `${streetNumber}${repetition} ${street}, ${zipcode} ${city}`;
}

export function toOccupancy(ccthp: DatafoncierHousing['ccthp']): Occupancy {
const occupancy = OCCUPANCY_VALUES.find((occupancy) => occupancy === ccthp);
return occupancy ?? Occupancy.UNKNOWN;
}
24 changes: 24 additions & 0 deletions packages/models/src/test/DatafoncierHousing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Occupancy, OCCUPANCY_VALUES } from '../Occupancy';
import { toOccupancy } from '../DatafoncierHousing';

describe('DatafoncierHousing', () => {
describe('toOccupancy', () => {
it.each(OCCUPANCY_VALUES)('should return %s', (ccthp) => {
const actual = toOccupancy(ccthp);

expect(actual).toBe(ccthp);
});

it(`should return ${Occupancy.UNKNOWN} when ccthp is null`, () => {
const actual = toOccupancy(null);

expect(actual).toBe(Occupancy.UNKNOWN);
});

it(`should return ${Occupancy.UNKNOWN} otherwise`, () => {
const actual = toOccupancy('test');

expect(actual).toBe(Occupancy.UNKNOWN);
});
});
});
9 changes: 7 additions & 2 deletions packages/models/src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { OwnerDTO } from '../OwnerDTO';
import { RolesDTO } from '../RolesDTO';
import { SenderDTO, SignatoryDTO } from '../SenderDTO';
import { UserDTO } from '../UserDTO';
import { OCCUPANCY_VALUES } from '../Occupancy';
import { Occupancy, OCCUPANCY_VALUES } from '../Occupancy';
import { HOUSING_KIND_VALUES } from '../HousingKind';
import { DatafoncierHousing } from '../DatafoncierHousing';
import { HOUSING_STATUS_VALUES } from '../HousingStatus';
Expand Down Expand Up @@ -127,7 +127,12 @@ export function genDatafoncierHousingDTO(
dloy48a: faker.number.int(99),
top48a: faker.string.alphanumeric(1),
dnatlc: faker.string.alphanumeric(1),
ccthp: faker.helpers.arrayElement(['L', 'V']),
ccthp: faker.helpers.arrayElement([
...OCCUPANCY_VALUES.filter(
(occupancy) => occupancy !== Occupancy.UNKNOWN
),
null
]),
proba_rprs: faker.string.alphanumeric(7),
typeact: faker.string.alphanumeric(4),
loghvac: faker.string.alphanumeric(1),
Expand Down
Loading

0 comments on commit dfa5b32

Please sign in to comment.