Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix owner kind values and labels #1082

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions frontend/src/models/HousingFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
LocalityKind,
Occupancy,
OCCUPANCY_VALUES,
OWNER_KIND_LABELS,
OWNER_KIND_VALUES,
OwnerAge,
OwnerKind,
Expand Down Expand Up @@ -66,9 +67,9 @@ export const noCampaignOption: SelectOption = {
export type NoCampaign = typeof noCampaignOption.value;

export const ownerKindOptions: SelectOption<OwnerKind>[] =
OWNER_KIND_VALUES.map((kind) => ({
value: kind,
label: kind
OWNER_KIND_VALUES.map((value) => ({
value: value,
label: OWNER_KIND_LABELS[value]
}));

export const campaignsCountOptions: SelectOption<CampaignCount>[] = [
Expand Down
24 changes: 17 additions & 7 deletions packages/models/src/OwnerKind.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
export const OWNER_KIND_VALUES = [
'Particulier',
'SCI, Copropriété, Autres personnes morales',
'Promoteur, Investisseur privé',
'Etat et collectivité territoriale',
'Bailleur social, Aménageur, Investisseur public',
'Autres',
'Absence de propriétaire'
'particulier',
'sci-copro',
'promoteur',
'etat-collectivite',
'bailleur-social',
'autres',
'absent'
] as const;

export type OwnerKind = (typeof OWNER_KIND_VALUES)[number];

export const OWNER_KIND_LABELS: Record<OwnerKind, string> = {
particulier: 'Particulier',
'sci-copro': 'SCI, Copropriété, Autres personnes morales',
promoteur: 'Promoteur, Investisseur privé',
'etat-collectivite': 'Etat et collectivité territoriale',
'bailleur-social': 'Bailleur social, Aménageur, Investisseur public',
autres: 'Autres',
absent: 'Absence de propriétaire'
};
3 changes: 2 additions & 1 deletion packages/models/src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ProspectDTO } from '../ProspectDTO';
import { EstablishmentDTO } from '../EstablishmentDTO';
import { ESTABLISHMENT_KIND_VALUES } from '../EstablishmentKind';
import { ESTABLISHMENT_SOURCE_VALUES } from '../EstablishmentSource';
import { OWNER_KIND_LABELS } from '../OwnerKind';

export function genGeoCode(): string {
const geoCode = faker.helpers.arrayElement([
Expand Down Expand Up @@ -342,7 +343,7 @@ export function genOwnerDTO(): OwnerDTO {
lastName
}),
phone: faker.phone.number(),
kind: 'PERSONNE PHYSIQUE'
kind: faker.helpers.arrayElement(Object.values(OWNER_KIND_LABELS))
});
}

Expand Down
3 changes: 2 additions & 1 deletion server/src/models/HousingFiltersApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
HousingFiltersDTO,
Occupancy,
OwnerKind,
OwnershipKind
} from '@zerologementvacant/models';
import { EnergyConsumptionGradesApi } from './HousingApi';
Expand All @@ -12,7 +13,7 @@ export interface HousingFiltersApi
housingIds?: string[];
establishmentIds?: string[];
groupIds?: string[];
ownerKinds?: string[];
ownerKinds?: OwnerKind[];
ownerAges?: string[];
multiOwners?: string[];
/**
Expand Down
12 changes: 10 additions & 2 deletions server/src/repositories/housingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
INTERNAL_CO_CONDOMINIUM_VALUES,
INTERNAL_MONO_CONDOMINIUM_VALUES,
Occupancy,
OWNER_KIND_LABELS,
PaginationOptions
} from '@zerologementvacant/models';
import db, { toRawArray, where } from '~/infra/database';
Expand Down Expand Up @@ -136,7 +137,11 @@ async function count(filters: HousingFiltersApi): Promise<HousingCountApi> {

const [allowedGeoCodes, intercommunalities] = await Promise.all([
fetchGeoCodes(filters.establishmentIds ?? []),
fetchGeoCodes(Array.isArray(filters.intercommunalities) ? filters.intercommunalities : [])
fetchGeoCodes(
Array.isArray(filters.intercommunalities)
? filters.intercommunalities
: []
)
]);
const localities = filters.localities ?? [];
const geoCodes = Set(allowedGeoCodes)
Expand Down Expand Up @@ -480,7 +485,10 @@ function filteredQuery(opts: FilteredQueryOptions) {
queryBuilder.whereIn(`${ownerTable}.id`, filters.ownerIds);
}
if (filters.ownerKinds?.length) {
queryBuilder.whereIn(`${ownerTable}.kind_class`, filters.ownerKinds);
const ownerKinds = filters.ownerKinds.map(
(kind) => OWNER_KIND_LABELS[kind]
);
queryBuilder.whereIn(`${ownerTable}.kind_class`, ownerKinds);
}
if (filters.ownerAges?.length) {
queryBuilder.where((whereBuilder) => {
Expand Down
15 changes: 10 additions & 5 deletions server/src/repositories/test/housingRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import {
isSecondaryOwner,
Occupancy,
OCCUPANCY_VALUES,
OWNER_KIND_LABELS,
OWNER_KIND_VALUES,
OwnershipKind,
ROOM_COUNT_VALUES
} from '@zerologementvacant/models';
Expand Down Expand Up @@ -528,16 +530,18 @@ describe('Housing repository', () => {
});

describe('by owner kind', () => {
const kinds = ['Particulier', 'Investisseur', 'SCI'];
const kinds = OWNER_KIND_VALUES;

beforeEach(async () => {
const housings = Array.from({ length: kinds.length }, () =>
genHousingApi()
);
await Housing().insert(housings.map(formatHousingRecordApi));
const owners: OwnerApi[] = kinds.map((kind, i) => {
return { ...housings[i].owner, kind };
});
const owners: OwnerApi[] = Object.values(OWNER_KIND_LABELS).map(
(kind, i) => {
return { ...housings[i].owner, kind };
}
);
await Owners().insert(owners.map(formatOwnerApi));
await HousingOwners().insert(
housings.flatMap((housing) =>
Expand All @@ -553,8 +557,9 @@ describe('Housing repository', () => {
}
});

expect(actual.length).toBeGreaterThan(0);
expect(actual).toSatisfyAll<HousingApi>((housing) => {
return housing.owner?.kind === kind;
return housing.owner?.kind === OWNER_KIND_LABELS[kind];
});
});
});
Expand Down
12 changes: 2 additions & 10 deletions server/src/test/testFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
INTERNAL_MONO_CONDOMINIUM_VALUES,
Occupancy,
OCCUPANCY_VALUES,
OWNER_KIND_LABELS,
UserAccountDTO
} from '@zerologementvacant/models';

Expand Down Expand Up @@ -206,16 +207,7 @@ export const genOwnerApi = (): OwnerApi => {
email: genEmail(),
phone: faker.phone.number(),
kind: faker.helpers.maybe(
() =>
faker.helpers.arrayElement([
'Particulier',
'Investisseur',
'Promoteur, Investisseur privé',
'SCI',
'SCI, Copropriété, Autres personnes morales',
'Autres',
'Etat et collectivité territoriale'
]),
() => faker.helpers.arrayElement(Object.values(OWNER_KIND_LABELS)),
{ probability: 0.8 }
),
kindDetail: randomstring.generate(),
Expand Down
Loading