[offers][fix] Fix generate analysis API by deleting analysis if exists before creating

This commit is contained in:
BryannYeap
2022-10-15 05:18:52 +08:00
parent c12c318a0b
commit 0edcfb7af3
3 changed files with 19 additions and 2 deletions

View File

@ -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;

View File

@ -358,10 +358,10 @@ model OffersFullTime {
model OffersAnalysis { model OffersAnalysis {
id String @id @default(cuid()) id String @id @default(cuid())
profile OffersProfile @relation(fields: [profileId], references: [id]) profile OffersProfile @relation(fields: [profileId], references: [id], onDelete: Cascade)
profileId String @unique profileId String @unique
overallHighestOffer OffersOffer @relation("HighestOverallOffer", fields: [offerId], references: [id]) overallHighestOffer OffersOffer @relation("HighestOverallOffer", fields: [offerId], references: [id], onDelete: Cascade)
offerId String @unique offerId String @unique
// OVERALL // OVERALL

View File

@ -71,6 +71,12 @@ export const offersAnalysisRouter = createRouter().query('generate', {
profileId: z.string(), profileId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
await ctx.prisma.offersAnalysis.deleteMany({
where: {
profileId: input.profileId,
},
});
const offers = await ctx.prisma.offersOffer.findMany({ const offers = await ctx.prisma.offersOffer.findMany({
include: { include: {
OffersFullTime: { OffersFullTime: {