Skip to content

Commit

Permalink
test(server): seed owners independently from housings to enable multi…
Browse files Browse the repository at this point in the history
…-owners
  • Loading branch information
Falinor committed Feb 6, 2025
1 parent c4d4ac0 commit ee61f48
Showing 1 changed file with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function seed(knex: Knex): Promise<void> {
{
count: {
min: 100,
max: 10000
max: 5000
}
}
);
Expand Down Expand Up @@ -117,17 +117,48 @@ export async function seed(knex: Knex): Promise<void> {
housingAddresses.map(formatAddressApi)
);

// Insert owners
const owners: ReadonlyArray<OwnerApi> = faker.helpers.multiple(
() => genOwnerApi(),
{
count: {
min: 100,
max: 5000
}
}
);
console.log(`Inserting ${owners.length} owners...`, {
establishment: establishment.name
});
await knex.batchInsert(ownerTable, owners.map(formatOwnerApi));

// Link owners to housings
const housingOwners: ReadonlyArray<HousingOwnerApi> = housings.flatMap(
(housing) => {
const owners = faker.helpers.multiple(() => genOwnerApi(), {
count: {
min: 1,
max: 6
}
const activeOwners = faker.helpers.arrayElements(owners, {
min: 1,
max: 6
});
const archivedOwners: ReadonlyArray<HousingOwnerApi> = [];
const archivedOwners: ReadonlyArray<HousingOwnerApi> = faker.helpers
.arrayElements(
owners.filter(
(owner) =>
!activeOwners.some((activeOwner) => activeOwner.id === owner.id)
),
{
min: 1,
max: 2
}
)
.map((archivedOwner) => ({
...archivedOwner,
ownerId: archivedOwner.id,
housingGeoCode: housing.geoCode,
housingId: housing.id,
rank: -2
}));

return owners
return activeOwners
.map<HousingOwnerApi>((owner, index) => ({
...owner,
ownerId: owner.id,
Expand All @@ -138,12 +169,6 @@ export async function seed(knex: Knex): Promise<void> {
.concat(archivedOwners);
}
);
const owners: ReadonlyArray<OwnerApi> = housingOwners.flat();
console.log(`Inserting ${owners.length} owners...`, {
establishment: establishment.name
});
await knex.batchInsert(ownerTable, owners.map(formatOwnerApi));

console.log(`Inserting ${housingOwners.length} housing owners...`, {
establishment: establishment.name
});
Expand Down

0 comments on commit ee61f48

Please sign in to comment.