From ab7f064734868b7d8481e0677838fe46141d534a Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Wed, 2 Nov 2022 15:33:20 +0800 Subject: [PATCH] [offers][fix] use upsert to remove id in valuation --- .../router/offers/offers-profile-router.ts | 96 ++++++++----------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git a/apps/portal/src/server/router/offers/offers-profile-router.ts b/apps/portal/src/server/router/offers/offers-profile-router.ts index 5f367098..e25bb076 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -786,26 +786,10 @@ export const offersProfileRouter = createRouter() }); if (exp.monthlySalary) { - if (exp.monthlySalary.id) { - await ctx.prisma.offersCurrency.update({ - data: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - exp.monthlySalary.value, - exp.monthlySalary.currency, - baseCurrencyString, - ), - currency: exp.monthlySalary.currency, - value: exp.monthlySalary.value, - }, - where: { - id: exp.monthlySalary.id, - }, - }); - } else { - await ctx.prisma.offersExperience.update({ - data: { - monthlySalary: { + await ctx.prisma.offersExperience.update({ + data: { + monthlySalary: { + upsert: { create: { baseCurrency: baseCurrencyString, baseValue: await convert( @@ -816,36 +800,30 @@ export const offersProfileRouter = createRouter() currency: exp.monthlySalary.currency, value: exp.monthlySalary.value, }, - }, - }, - where: { - id: exp.id, - }, - }); - } + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.monthlySalary.value, + exp.monthlySalary.currency, + baseCurrencyString, + ), + currency: exp.monthlySalary.currency, + value: exp.monthlySalary.value, + } + } + } + }, + where: { + id: exp.id + } + }) } if (exp.totalCompensation) { - if (exp.totalCompensation.id) { - await ctx.prisma.offersCurrency.update({ - data: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - exp.totalCompensation.value, - exp.totalCompensation.currency, - baseCurrencyString, - ), - currency: exp.totalCompensation.currency, - value: exp.totalCompensation.value, - }, - where: { - id: exp.totalCompensation.id, - }, - }); - } else { - await ctx.prisma.offersExperience.update({ - data: { - totalCompensation: { + await ctx.prisma.offersExperience.update({ + data: { + totalCompensation: { + upsert: { create: { baseCurrency: baseCurrencyString, baseValue: await convert( @@ -856,13 +834,23 @@ export const offersProfileRouter = createRouter() currency: exp.totalCompensation.currency, value: exp.totalCompensation.value, }, - }, - }, - where: { - id: exp.id, - }, - }); - } + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.totalCompensation.value, + exp.totalCompensation.currency, + baseCurrencyString, + ), + currency: exp.totalCompensation.currency, + value: exp.totalCompensation.value, + } + } + } + }, + where: { + id: exp.id + } + }) } } else if (!exp.id) { // Create new experience