From 34538919b1babe04be5058ffadcefa4d7c6f5a98 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Mon, 24 Oct 2022 01:30:30 +0800 Subject: [PATCH] [offers][fix] fix edit profile endpoint --- .../router/offers/offers-profile-router.ts | 106 +++++++++++++----- 1 file changed, 75 insertions(+), 31 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 41d7bdce..bc6f0d43 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -707,7 +707,7 @@ export const offersProfileRouter = createRouter() // Update existing experience await ctx.prisma.offersExperience.update({ data: { - companyId: exp.companyId, + companyId: exp.companyId, // TODO: check if can change with connect or whether there is a difference durationInMonths: exp.durationInMonths, level: exp.level, specialization: exp.specialization, @@ -718,39 +718,83 @@ export const offersProfileRouter = createRouter() }); if (exp.monthlySalary) { - 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, - }, - }); + 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: { + create: { + 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) { - 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, - }, - }); + 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: { + create: { + 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