Skip to content

Commit

Permalink
fix: improve seed data and validity (#4099)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Jun 5, 2024
1 parent c19f18c commit adab010
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 109 deletions.
2 changes: 1 addition & 1 deletion api/prisma/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const LISTINGS_TO_SEED = 10;
export const APPLICATIONS_PER_LISTINGS = 5;
export const APPLICATIONS_PER_LISTINGS = 10;
22 changes: 11 additions & 11 deletions api/prisma/seed-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ export const devSeeding = async (

await reservedCommunityTypeFactoryAll(jurisdiction.id, prismaClient);

const householdSize = randomInt(0, 6);
for (let index = 0; index < LISTINGS_TO_SEED; index++) {
const householdMembers = await householdMemberFactoryMany(householdSize);

const applications = [];

for (let j = 0; j <= APPLICATIONS_PER_LISTINGS; j++) {
applications.push(
await applicationFactory({
householdSize,
unitTypeId: unitTypes[randomInt(0, 5)].id,
householdMember: householdMembers,
multiselectQuestions,
}),
for (let j = 0; j < APPLICATIONS_PER_LISTINGS; j++) {
const householdSize = randomInt(1, 6);
const householdMembers = await householdMemberFactoryMany(
householdSize - 1,
);
const app = await applicationFactory({
householdSize,
unitTypeId: unitTypes[randomInt(0, 5)].id,
householdMember: householdMembers,
multiselectQuestions,
});
applications.push(app);
}

const listing = await listingFactory(jurisdiction.id, prismaClient, {
Expand Down
43 changes: 43 additions & 0 deletions api/prisma/seed-helpers/alternate-contact-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Prisma } from '@prisma/client';
import { randomInt } from 'crypto';
import { randomAdjective, randomNoun } from './word-generator';
import { AlternateContactRelationship } from '../../src/enums/applications/alternate-contact-relationship-enum';
import { addressFactory } from './address-factory';

export const alternateContactFactory =
(): Prisma.AlternateContactCreateWithoutApplicationsInput => {
const relationshipKeys = Object.values(AlternateContactRelationship);
const firstName = randomNoun();
const lastName = randomNoun();
const relationshipType =
relationshipKeys[randomInt(relationshipKeys.length)];

if (relationshipType === AlternateContactRelationship.noContact) {
return {
firstName: undefined,
lastName: undefined,
type: relationshipType,
otherType: undefined,
phoneNumber: undefined,
emailAddress: undefined,
agency: undefined,
address: { create: addressFactory() },
};
}
return {
firstName: firstName,
lastName: lastName,
type: relationshipType,
otherType:
relationshipType === AlternateContactRelationship.other
? randomAdjective()
: undefined,
phoneNumber: '(123) 123-1231',
emailAddress: `${firstName}.${lastName}@example.com`,
address: { create: addressFactory() },
agency:
relationshipType === AlternateContactRelationship.caseManager
? randomAdjective()
: undefined,
};
};
13 changes: 12 additions & 1 deletion api/prisma/seed-helpers/application-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from './number-generator';
import { preferenceFactory } from './application-preference-factory';
import { demographicsFactory } from './demographic-factory';
import { alternateContactFactory } from './alternate-contact-factory';
import { randomBoolean } from './boolean-generator';

export const applicationFactory = async (optionalParams?: {
householdSize?: number;
Expand All @@ -41,6 +43,7 @@ export const applicationFactory = async (optionalParams?: {
};
}
const demographics = await demographicsFactory();
const additionalPhone = randomBoolean();
return {
confirmationCode: generateConfirmationCode(),
applicant: { create: applicantFactory(optionalParams?.applicant) },
Expand All @@ -52,7 +55,9 @@ export const applicationFactory = async (optionalParams?: {
submissionDate: new Date(),
householdSize: optionalParams?.householdSize ?? 1,
income: '40000',
incomePeriod: IncomePeriodEnum.perYear,
incomePeriod: randomBoolean()
? IncomePeriodEnum.perYear
: IncomePeriodEnum.perMonth,
preferences: preferenceFactory(
optionalParams?.multiselectQuestions
? optionalParams.multiselectQuestions.filter(
Expand Down Expand Up @@ -92,13 +97,18 @@ export const applicationFactory = async (optionalParams?: {
demographics: {
create: demographics,
},
alternateContact: { create: alternateContactFactory() },
userAccounts: optionalParams?.userId
? {
connect: {
id: optionalParams.userId,
},
}
: undefined,
incomeVouchers: randomBoolean(),
additionalPhoneNumber: additionalPhone ? '(456) 456-4564' : undefined,
additionalPhone,
additionalPhoneNumberType: additionalPhone ? 'cell' : undefined,
};
};

Expand All @@ -109,6 +119,7 @@ export const applicantFactory = (
const lastName = randomNoun();
return {
firstName: firstName,
middleName: randomBoolean() ? randomNoun() : undefined,
lastName: lastName,
emailAddress: `${firstName}.${lastName}@example.com`,
noEmail: false,
Expand Down
57 changes: 30 additions & 27 deletions api/prisma/seed-helpers/household-member-factory.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
import { Prisma, YesNoEnum } from '@prisma/client';
import { randomInt } from 'crypto';
import { HouseholdMemberRelationship } from '../../src/enums/applications/household-member-relationship-enum';
import { addressFactory } from './address-factory';
import { randomAdjective, randomNoun } from './word-generator';
import { randomNoun } from './word-generator';
import {
randomBirthDay,
randomBirthMonth,
randomBirthYear,
} from './number-generator';
import { randomBoolean } from './boolean-generator';

export const householdMemberFactorySingle =
(): Prisma.HouseholdMemberCreateWithoutApplicationsInput => {
const firstName = randomNoun();
const lastName = randomNoun();
const randomYesNo = randomBoolean() === true ? YesNoEnum.yes : YesNoEnum.no;
return {
firstName: firstName,
middleName: randomNoun(),
lastName: lastName,
// Question: why are these strings?
birthMonth: randomBirthMonth(),
birthDay: randomBirthDay(),
birthYear: randomBirthYear(),
sameAddress: randomYesNo,
// Question: should this be an enum?
relationship: randomAdjective(),
workInRegion: randomYesNo,
householdMemberAddress: randomBoolean
? undefined
: { create: addressFactory() },
householdMemberWorkAddress: {
create: addressFactory(),
},
};
export const householdMemberFactorySingle = (
index: number,
): Prisma.HouseholdMemberCreateWithoutApplicationsInput => {
const firstName = randomNoun();
const lastName = randomNoun();

const relationshipKeys = Object.values(HouseholdMemberRelationship);

return {
firstName: firstName,
middleName: randomNoun(),
lastName: lastName,
// Question: why are these strings?
birthMonth: randomBirthMonth(),
birthDay: randomBirthDay(),
birthYear: randomBirthYear(),
sameAddress: YesNoEnum.yes,
relationship: relationshipKeys[randomInt(relationshipKeys.length)],
workInRegion: YesNoEnum.yes,
householdMemberAddress: { create: addressFactory() },
householdMemberWorkAddress: {
create: addressFactory(),
},
orderId: index,
};
};

export const householdMemberFactoryMany = async (
numberToMake: number,
): Promise<Prisma.HouseholdMemberCreateWithoutApplicationsInput[]> => {
const createArray: Promise<Prisma.HouseholdMemberCreateWithoutApplicationsInput>[] =
[...new Array(numberToMake)].map(async () =>
householdMemberFactorySingle(),
[...new Array(numberToMake)].map(async (index) =>
householdMemberFactorySingle(index),
);
return await Promise.all(createArray);
};
2 changes: 1 addition & 1 deletion api/prisma/seed-helpers/jurisdiction-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const jurisdictionFactory = (
listingApprovalPermissions?: UserRoleEnum[],
): Prisma.JurisdictionsCreateInput => ({
name: jurisdictionName,
notificationsSignUpUrl: null,
notificationsSignUpUrl: 'https://www.exygy.com',
languages: [LanguagesEnum.en],
partnerTerms: 'Example Terms',
publicUrl: 'http://localhost:3000',
Expand Down
14 changes: 10 additions & 4 deletions api/src/dtos/applications/alternate-contact.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Expose, Type } from 'class-transformer';
import {
IsDefined,
IsEmail,
IsEnum,
IsString,
MaxLength,
ValidateNested,
Expand All @@ -11,14 +12,19 @@ import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum
import { AbstractDTO } from '../shared/abstract.dto';
import { Address } from '../addresses/address.dto';
import { EnforceLowerCase } from '../../decorators/enforce-lower-case.decorator';
import { AlternateContactRelationship } from '../../enums/applications/alternate-contact-relationship-enum';

export class AlternateContact extends AbstractDTO {
@Expose()
@IsString({ groups: [ValidationsGroupsEnum.default] })
@MaxLength(16, { groups: [ValidationsGroupsEnum.default] })
@IsEnum(AlternateContactRelationship, {
groups: [ValidationsGroupsEnum.default],
})
@ApiPropertyOptional({
enum: AlternateContactRelationship,
enumName: 'AlternateContactRelationship',
})
@IsDefined({ groups: [ValidationsGroupsEnum.applicants] })
@ApiPropertyOptional()
type?: string;
type?: AlternateContactRelationship;

@Expose()
@IsString({ groups: [ValidationsGroupsEnum.default] })
Expand Down
13 changes: 9 additions & 4 deletions api/src/dtos/applications/household-member.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { YesNoEnum } from '@prisma/client';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum';
import { HouseholdMemberRelationship } from '../../enums/applications/household-member-relationship-enum';
import { AbstractDTO } from '../shared/abstract.dto';
import { Address } from '../addresses/address.dto';

Expand Down Expand Up @@ -67,10 +68,14 @@ export class HouseholdMember extends AbstractDTO {
sameAddress?: YesNoEnum;

@Expose()
@IsString({ groups: [ValidationsGroupsEnum.default] })
@MaxLength(64, { groups: [ValidationsGroupsEnum.default] })
@ApiPropertyOptional()
relationship?: string;
@IsEnum(HouseholdMemberRelationship, {
groups: [ValidationsGroupsEnum.default],
})
@ApiPropertyOptional({
enum: HouseholdMemberRelationship,
enumName: 'HouseholdMemberRelationship',
})
relationship?: HouseholdMemberRelationship;

@Expose()
@IsEnum(YesNoEnum, { groups: [ValidationsGroupsEnum.default] })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum AlternateContactRelationship {
familyMember = 'familyMember',
friend = 'friend',
caseManager = 'caseManager',
other = 'other',
noContact = 'noContact',
}
17 changes: 17 additions & 0 deletions api/src/enums/applications/household-member-relationship-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum HouseholdMemberRelationship {
spouse = 'spouse',
registeredDomesticPartner = 'registeredDomesticPartner',
parent = 'parent',
child = 'child',
sibling = 'sibling',
cousin = 'cousin',
aunt = 'aunt',
uncle = 'uncle',
nephew = 'nephew',
niece = 'niece',
grandparent = 'grandparent',
greatGrandparent = 'greatGrandparent',
inLaw = 'inLaw',
friend = 'friend',
other = 'other',
}
Loading

0 comments on commit adab010

Please sign in to comment.