Skip to content

Commit

Permalink
fix: update seeds + fix listing delete (#3940)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Mar 14, 2024
1 parent 567381f commit 18df523
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
-- DropForeignKey

ALTER TABLE "application_methods"
DROP CONSTRAINT "application_methods_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "application_methods" ADD CONSTRAINT "application_methods_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "applications"
DROP CONSTRAINT "applications_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "applications" ADD CONSTRAINT "applications_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE
SET NULL ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "listing_images"
DROP CONSTRAINT "listing_images_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "listing_images" ADD CONSTRAINT "listing_images_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "listing_events"
DROP CONSTRAINT "listing_events_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "listing_events" ADD CONSTRAINT "listing_events_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "listing_multiselect_questions"
DROP CONSTRAINT "listing_multiselect_questions_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "listing_multiselect_questions" ADD CONSTRAINT "listing_multiselect_questions_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "units_summary"
DROP CONSTRAINT "units_summary_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "units_summary" ADD CONSTRAINT "units_summary_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "application_flagged_set"
DROP CONSTRAINT "application_flagged_set_listing_id_fkey";

-- AddForeignKey

ALTER TABLE "application_flagged_set" ADD CONSTRAINT "application_flagged_set_listing_id_fkey"
FOREIGN KEY ("listing_id") REFERENCES "listings"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "paper_applications"
DROP CONSTRAINT "paper_applications_application_method_id_fkey";

-- AddForeignKey

ALTER TABLE "paper_applications" ADD CONSTRAINT "paper_applications_application_method_id_fkey"
FOREIGN KEY ("application_method_id") REFERENCES "application_methods"("id") ON
DELETE
SET NULL ON
UPDATE NO ACTION;

-- DropForeignKey

ALTER TABLE "household_member"
DROP CONSTRAINT "household_member_application_id_fkey";

-- AddForeignKey

ALTER TABLE "household_member" ADD CONSTRAINT "household_member_application_id_fkey"
FOREIGN KEY ("application_id") REFERENCES "applications"("id") ON
DELETE CASCADE ON
UPDATE NO ACTION;

20 changes: 10 additions & 10 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ model ActivityLog {
module String @db.VarChar
action String @db.VarChar
metadata Json?
recordId String? @map("record_id") @db.Uuid
recordId String? @map("record_id") @db.Uuid
userId String? @map("user_id") @db.Uuid
userAccounts UserAccounts? @relation(fields: [userId], references: [id], onUpdate: NoAction)
Expand Down Expand Up @@ -143,7 +143,7 @@ model ApplicationFlaggedSet {
status FlaggedSetStatusEnum @default(pending)
resolvingUserId String? @map("resolving_user_id") @db.Uuid
userAccounts UserAccounts? @relation(fields: [resolvingUserId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
applications Applications[]
@@index([listingId])
Expand All @@ -161,7 +161,7 @@ model ApplicationMethods {
acceptsPostmarkedApplications Boolean? @map("accepts_postmarked_applications")
phoneNumber String? @map("phone_number")
listingId String? @map("listing_id") @db.Uuid
listings Listings? @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
paperApplications PaperApplications[]
@@map("application_methods")
Expand Down Expand Up @@ -218,7 +218,7 @@ model Applications {
applicationsAlternateAddress Address? @relation("applications_alternate_address", fields: [alternateAddressId], references: [id], onDelete: NoAction, onUpdate: NoAction)
userAccounts UserAccounts? @relation(fields: [userId], references: [id], onUpdate: NoAction)
applicationsMailingAddress Address? @relation("applications_mailing_address", fields: [mailingAddressId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: SetNull, onUpdate: NoAction)
demographics Demographics? @relation(fields: [demographicsId], references: [id], onDelete: NoAction, onUpdate: NoAction)
preferredUnitTypes UnitTypes[]
householdMember HouseholdMember[]
Expand Down Expand Up @@ -305,7 +305,7 @@ model HouseholdMember {
addressId String? @unique() @map("address_id") @db.Uuid
workAddressId String? @unique() @map("work_address_id") @db.Uuid
applicationId String? @map("application_id") @db.Uuid
applications Applications? @relation(fields: [applicationId], references: [id], onDelete: NoAction, onUpdate: NoAction)
applications Applications? @relation(fields: [applicationId], references: [id], onDelete: Cascade, onUpdate: NoAction)
householdMemberAddress Address? @relation("household_member_address", fields: [addressId], references: [id], onDelete: NoAction, onUpdate: NoAction)
householdMemberWorkAddress Address? @relation("household_member_work_address", fields: [workAddressId], references: [id], onDelete: NoAction, onUpdate: NoAction)
Expand Down Expand Up @@ -365,7 +365,7 @@ model ListingEvents {
listingId String? @map("listing_id") @db.Uuid
fileId String? @map("file_id") @db.Uuid
assets Assets? @relation(fields: [fileId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@map("listing_events")
}
Expand Down Expand Up @@ -404,7 +404,7 @@ model ListingImages {
listingId String @map("listing_id") @db.Uuid
imageId String @map("image_id") @db.Uuid
assets Assets @relation(fields: [imageId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@id([listingId, imageId])
@@index([listingId])
Expand All @@ -416,7 +416,7 @@ model ListingMultiselectQuestions {
listingId String @map("listing_id") @db.Uuid
multiselectQuestionId String @map("multiselect_question_id") @db.Uuid
multiselectQuestions MultiselectQuestions @relation(fields: [multiselectQuestionId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@id([listingId, multiselectQuestionId])
@@map("listing_multiselect_questions")
Expand Down Expand Up @@ -619,7 +619,7 @@ model PaperApplications {
fileId String? @map("file_id") @db.Uuid
applicationMethodId String? @map("application_method_id") @db.Uuid
assets Assets? @relation(fields: [fileId], references: [id], onDelete: Cascade, onUpdate: NoAction)
applicationMethods ApplicationMethods? @relation(fields: [applicationMethodId], references: [id], onDelete: NoAction, onUpdate: NoAction)
applicationMethods ApplicationMethods? @relation(fields: [applicationMethodId], references: [id], onDelete: SetNull, onUpdate: NoAction)
@@map("paper_applications")
}
Expand Down Expand Up @@ -756,7 +756,7 @@ model UnitsSummary {
priorityTypeId String? @map("priority_type_id") @db.Uuid
unitTypes UnitTypes? @relation(fields: [unitTypeId], references: [id], onDelete: NoAction, onUpdate: NoAction)
unitAccessibilityPriorityTypes UnitAccessibilityPriorityTypes? @relation(fields: [priorityTypeId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: NoAction, onUpdate: NoAction)
listings Listings? @relation(fields: [listingId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@map("units_summary")
}
Expand Down
12 changes: 10 additions & 2 deletions api/prisma/seed-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@ export const devSeeding = async (

const listing = await listingFactory(jurisdiction.id, prismaClient, {
amiChart: amiChart,
numberOfUnits: index,
numberOfUnits: index + 1,
includeBuildingFeatures: index > 1,
includeEligibilityRules: index > 2,
status: listingStatusEnumArray[randomInt(listingStatusEnumArray.length)],
status:
index < 4
? ListingsStatusEnum.active
: listingStatusEnumArray[
index - 3 < listingStatusEnumArray.length
? index - 3
: randomInt(listingStatusEnumArray.length - 1)
],
multiselectQuestions:
index > 0 ? multiselectQuestions.slice(0, index - 1) : [],
applications,
digitalApp: !!(index % 2),
});
await prismaClient.listings.create({
data: listing,
Expand Down
63 changes: 57 additions & 6 deletions api/prisma/seed-helpers/listing-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import {
MultiselectQuestions,
PrismaClient,
ListingsStatusEnum,
ApplicationMethodsTypeEnum,
} from '@prisma/client';
import { randomInt } from 'crypto';
import { randomName } from './word-generator';
import { addressFactory } from './address-factory';
import { unitFactoryMany } from './unit-factory';
import { reservedCommunityTypeFactoryGet } from './reserved-community-type-factory';

const cloudinaryIds = [
'dev/blake-wheeler-zBHU08hdzhY-unsplash_swqash',
'dev/krzysztof-hepner-V7Q0Oh3Az-c-unsplash_xoj7sr',
'dev/dillon-kydd-2keCPb73aQY-unsplash_lm7krp',
'dev/inside_qo9wre',
'dev/interior_mc9erd',
'dev/apartment_ez3yyz',
'dev/trayan-xIOYJSVEZ8c-unsplash_f1axsg',
'dev/apartment_building_2_b7ujdd',
];

export const listingFactory = async (
jurisdictionId: string,
prismaClient: PrismaClient,
Expand All @@ -25,6 +38,8 @@ export const listingFactory = async (
applications?: Prisma.ApplicationsCreateInput[];
applicationDueDate?: Date;
afsLastRunSetInPast?: boolean;
digitalApp?: boolean;
noImage?: boolean;
},
): Promise<Prisma.ListingsCreateInput> => {
const previousListing = optionalParams?.listing || {};
Expand All @@ -39,6 +54,11 @@ export const listingFactory = async (
prismaClient,
jurisdictionId,
);

const digitalApp = !!optionalParams?.digitalApp
? optionalParams.digitalApp
: Math.random() < 0.5;

return {
createdAt: new Date(),
assets: [],
Expand All @@ -60,11 +80,14 @@ export const listingFactory = async (
listingsApplicationDropOffAddress: {
create: addressFactory(),
},
reservedCommunityTypes: {
connect: {
id: reservedCommunityType.id,
},
},
reservedCommunityTypes:
Math.random() < 0.5
? {
connect: {
id: reservedCommunityType.id,
},
}
: {},
// For application flagged set tests the date needs to be before the updated timestamp
// All others should be a newer timestamp so that they are not picked up by AFS tests
afsLastRunAt: optionalParams?.afsLastRunSetInPast
Expand Down Expand Up @@ -93,7 +116,6 @@ export const listingFactory = async (
...featuresAndUtilites(),
...buildingFeatures(optionalParams?.includeBuildingFeatures),
...additionalEligibilityRules(optionalParams?.includeEligibilityRules),
...previousListing,
jurisdictions: {
connect: {
id: jurisdictionId,
Expand All @@ -105,6 +127,35 @@ export const listingFactory = async (
}
: undefined,
applicationDueDate: optionalParams?.applicationDueDate ?? undefined,
developer: randomName(),
leasingAgentName: randomName(),
leasingAgentEmail: '[email protected]',
leasingAgentPhone: '515-604-0183',
digitalApplication: digitalApp,
commonDigitalApplication: digitalApp,
paperApplication: Math.random() < 0.5,
referralOpportunity: Math.random() < 0.5,
applicationMethods: digitalApp
? {
create: {
type: ApplicationMethodsTypeEnum.Internal,
},
}
: {},
listingImages: !optionalParams?.noImage
? {
create: {
ordinal: 0,
assets: {
create: {
label: 'cloudinaryBuilding',
fileId: cloudinaryIds[randomInt(cloudinaryIds.length)],
},
},
},
}
: {},
...previousListing,
};
};

Expand Down
2 changes: 1 addition & 1 deletion api/prisma/seed-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ export const stagingSeed = async (
assets: {
create: {
label: 'cloudinaryBuilding',
fileId: 'dev/blake-wheeler-zBHU08hdzhY-unsplash_swqash ',
fileId: 'dev/blake-wheeler-zBHU08hdzhY-unsplash_swqash',
},
},
},
Expand Down
4 changes: 3 additions & 1 deletion api/test/integration/listing.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ describe('Listing Controller Tests', () => {
data: jurisdictionFactory(),
});
await reservedCommunityTypeFactoryAll(jurisdictionA.id, prisma);
const listingData = await listingFactory(jurisdictionA.id, prisma);
const listingData = await listingFactory(jurisdictionA.id, prisma, {
noImage: true,
});
const listing = await prisma.listings.create({
data: listingData,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,9 @@ describe('Testing Permissioning of endpoints as Admin User', () => {
'permission juris 17',
);
await reservedCommunityTypeFactoryAll(jurisdictionA, prisma);
const listingData = await listingFactory(jurisdictionA, prisma);
const listingData = await listingFactory(jurisdictionA, prisma, {
noImage: true,
});
const listing = await prisma.listings.create({
data: listingData,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,9 @@ describe('Testing Permissioning of endpoints as Jurisdictional Admin in the corr
});

it('should succeed for delete endpoint & create an activity log entry', async () => {
const listingData = await listingFactory(jurisId, prisma);
const listingData = await listingFactory(jurisId, prisma, {
noImage: true,
});
const listing = await prisma.listings.create({
data: listingData,
});
Expand Down

0 comments on commit 18df523

Please sign in to comment.