From 0edcfb7af3aed1efe41f167f051b4bc0c7cddcb6 Mon Sep 17 00:00:00 2001 From: BryannYeap Date: Sat, 15 Oct 2022 05:18:52 +0800 Subject: [PATCH] [offers][fix] Fix generate analysis API by deleting analysis if exists before creating --- .../prisma/migrations/20221014211740_/migration.sql | 11 +++++++++++ apps/portal/prisma/schema.prisma | 4 ++-- .../server/router/offers/offers-analysis-router.ts | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 apps/portal/prisma/migrations/20221014211740_/migration.sql diff --git a/apps/portal/prisma/migrations/20221014211740_/migration.sql b/apps/portal/prisma/migrations/20221014211740_/migration.sql new file mode 100644 index 00000000..1c960010 --- /dev/null +++ b/apps/portal/prisma/migrations/20221014211740_/migration.sql @@ -0,0 +1,11 @@ +-- DropForeignKey +ALTER TABLE "OffersAnalysis" DROP CONSTRAINT "OffersAnalysis_offerId_fkey"; + +-- DropForeignKey +ALTER TABLE "OffersAnalysis" DROP CONSTRAINT "OffersAnalysis_profileId_fkey"; + +-- AddForeignKey +ALTER TABLE "OffersAnalysis" ADD CONSTRAINT "OffersAnalysis_profileId_fkey" FOREIGN KEY ("profileId") REFERENCES "OffersProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "OffersAnalysis" ADD CONSTRAINT "OffersAnalysis_offerId_fkey" FOREIGN KEY ("offerId") REFERENCES "OffersOffer"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 268d8d27..aa29f612 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -358,10 +358,10 @@ model OffersFullTime { model OffersAnalysis { id String @id @default(cuid()) - profile OffersProfile @relation(fields: [profileId], references: [id]) + profile OffersProfile @relation(fields: [profileId], references: [id], onDelete: Cascade) profileId String @unique - overallHighestOffer OffersOffer @relation("HighestOverallOffer", fields: [offerId], references: [id]) + overallHighestOffer OffersOffer @relation("HighestOverallOffer", fields: [offerId], references: [id], onDelete: Cascade) offerId String @unique // OVERALL diff --git a/apps/portal/src/server/router/offers/offers-analysis-router.ts b/apps/portal/src/server/router/offers/offers-analysis-router.ts index 882e84c3..dcdc64b6 100644 --- a/apps/portal/src/server/router/offers/offers-analysis-router.ts +++ b/apps/portal/src/server/router/offers/offers-analysis-router.ts @@ -71,6 +71,12 @@ export const offersAnalysisRouter = createRouter().query('generate', { profileId: z.string(), }), async resolve({ ctx, input }) { + await ctx.prisma.offersAnalysis.deleteMany({ + where: { + profileId: input.profileId, + }, + }); + const offers = await ctx.prisma.offersOffer.findMany({ include: { OffersFullTime: {