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 39cd0d4f..cdf0c8b8 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -972,7 +972,19 @@ export const offersProfileRouter = createRouter() for (const exp of input.background.experiences) { if (exp.id) { - // Update existing experience + const currentExp = await ctx.prisma.offersExperience.findFirst({ + where: { + id: exp.id + } + }) + + if (!currentExp) { + throw new trpc.TRPCError({ + code: 'NOT_FOUND', + message: 'Experience does not exist', + }); + } + await ctx.prisma.offersExperience.update({ data: { companyId: exp.companyId, // TODO: check if can change with connect or whether there is a difference @@ -984,73 +996,164 @@ export const offersProfileRouter = createRouter() id: exp.id, }, }); + if (currentExp.jobType === exp.jobType) { + // Update existing experience + if (exp.monthlySalary) { + await ctx.prisma.offersExperience.update({ + data: { + monthlySalary: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.monthlySalary.value, + exp.monthlySalary.currency, + baseCurrencyString, + ), + currency: exp.monthlySalary.currency, + value: exp.monthlySalary.value, + }, + 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.monthlySalary) { - await ctx.prisma.offersExperience.update({ - data: { - monthlySalary: { - upsert: { - create: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - exp.monthlySalary.value, - exp.monthlySalary.currency, - baseCurrencyString, - ), - currency: exp.monthlySalary.currency, - value: exp.monthlySalary.value, + if (exp.totalCompensation) { + await ctx.prisma.offersExperience.update({ + data: { + totalCompensation: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.totalCompensation.value, + exp.totalCompensation.currency, + baseCurrencyString, + ), + currency: exp.totalCompensation.currency, + value: exp.totalCompensation.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.totalCompensation.value, + exp.totalCompensation.currency, + baseCurrencyString, + ), + currency: exp.totalCompensation.currency, + value: exp.totalCompensation.value, + }, }, - 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, + }, + }); + } + } else if (exp.jobType === JobType.INTERN) { + // Add 1 remove the other + if (exp.monthlySalary) { + await ctx.prisma.offersExperience.update({ + data: { + monthlySalary: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.monthlySalary.value, + exp.monthlySalary.currency, + baseCurrencyString, + ), + currency: exp.monthlySalary.currency, + value: exp.monthlySalary.value, + }, + 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, - }, - }); - } + where: { + id: exp.id, + }, + }); + } - if (exp.totalCompensation) { await ctx.prisma.offersExperience.update({ data: { - totalCompensation: { - upsert: { - create: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - exp.totalCompensation.value, - exp.totalCompensation.currency, - baseCurrencyString, - ), - currency: exp.totalCompensation.currency, - value: exp.totalCompensation.value, - }, - update: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - exp.totalCompensation.value, - exp.totalCompensation.currency, - baseCurrencyString, - ), - currency: exp.totalCompensation.currency, - value: exp.totalCompensation.value, + totalCompensation: undefined, + totalCompensationId: null, + }, + where: { + id: exp.id + } + }) + } else if (exp.jobType === JobType.FULLTIME) { + if (exp.totalCompensation) { + await ctx.prisma.offersExperience.update({ + data: { + totalCompensation: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + exp.totalCompensation.value, + exp.totalCompensation.currency, + baseCurrencyString, + ), + currency: exp.totalCompensation.currency, + value: exp.totalCompensation.value, + }, + 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, + }, + }); + } + + await ctx.prisma.offersExperience.update({ + data: { + monthlySalary: undefined, + monthlySalaryId: null }, - }, - where: { - id: exp.id, - }, - }); + where: { + id: exp.id + } + }) } } else if (!exp.id) { // Create new experience @@ -1581,6 +1684,18 @@ export const offersProfileRouter = createRouter() for (const offerToUpdate of input.offers) { if (offerToUpdate.id) { // Update existing offer + const currentOffer = await ctx.prisma.offersOffer.findFirst({ + where: { + id: offerToUpdate.id + } + }) + + if (!currentOffer) { + throw new trpc.TRPCError({ + code: 'NOT_FOUND', + message: 'Offer to update does not exist', + }); + } await ctx.prisma.offersOffer.update({ data: { comments: offerToUpdate.comments, @@ -1606,82 +1721,191 @@ export const offersProfileRouter = createRouter() }, }); - if (offerToUpdate.offersIntern?.monthlySalary != null) { - await ctx.prisma.offersIntern.update({ - data: { - internshipCycle: - offerToUpdate.offersIntern.internshipCycle ?? undefined, - monthlySalary: { - upsert: { - create: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - offerToUpdate.offersIntern.monthlySalary.value, - offerToUpdate.offersIntern.monthlySalary.currency, - baseCurrencyString, - ), - currency: - offerToUpdate.offersIntern.monthlySalary.currency, - value: offerToUpdate.offersIntern.monthlySalary.value, - }, - update: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - offerToUpdate.offersIntern.monthlySalary.value, - offerToUpdate.offersIntern.monthlySalary.currency, - baseCurrencyString, - ), - currency: - offerToUpdate.offersIntern.monthlySalary.currency, - value: offerToUpdate.offersIntern.monthlySalary.value, + if (currentOffer.jobType === offerToUpdate.jobType) { + if (offerToUpdate.offersIntern?.monthlySalary != null) { + await ctx.prisma.offersIntern.update({ + data: { + internshipCycle: + offerToUpdate.offersIntern.internshipCycle ?? undefined, + monthlySalary: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersIntern.monthlySalary.value, + offerToUpdate.offersIntern.monthlySalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersIntern.monthlySalary.currency, + value: offerToUpdate.offersIntern.monthlySalary.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersIntern.monthlySalary.value, + offerToUpdate.offersIntern.monthlySalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersIntern.monthlySalary.currency, + value: offerToUpdate.offersIntern.monthlySalary.value, + }, }, }, + startYear: offerToUpdate.offersIntern.startYear ?? undefined, + title: offerToUpdate.offersIntern.title, }, - startYear: offerToUpdate.offersIntern.startYear ?? undefined, - title: offerToUpdate.offersIntern.title, - }, - where: { - id: offerToUpdate.offersIntern.id, - }, - }); - } + where: { + id: offerToUpdate.offersIntern.id, + }, + }); + } - if (offerToUpdate.offersFullTime?.totalCompensation != null) { - await ctx.prisma.offersFullTime.update({ - data: { - level: offerToUpdate.offersFullTime.level ?? undefined, - title: offerToUpdate.offersFullTime.title, - }, - where: { - id: offerToUpdate.offersFullTime.id, - }, - }); - if (offerToUpdate.offersFullTime.baseSalary != null) { + if (offerToUpdate.offersFullTime?.totalCompensation != null) { + await ctx.prisma.offersFullTime.update({ + data: { + level: offerToUpdate.offersFullTime.level ?? undefined, + title: offerToUpdate.offersFullTime.title, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + if (offerToUpdate.offersFullTime.baseSalary != null) { + await ctx.prisma.offersFullTime.update({ + data: { + baseSalary: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.baseSalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.baseSalary.currency, + value: offerToUpdate.offersFullTime.baseSalary.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.baseSalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.baseSalary.currency, + value: offerToUpdate.offersFullTime.baseSalary.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } + if (offerToUpdate.offersFullTime.bonus != null) { + await ctx.prisma.offersFullTime.update({ + data: { + bonus: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.bonus.value, + offerToUpdate.offersFullTime.bonus.currency, + baseCurrencyString, + ), + currency: offerToUpdate.offersFullTime.bonus.currency, + value: offerToUpdate.offersFullTime.bonus.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.bonus.value, + offerToUpdate.offersFullTime.bonus.currency, + baseCurrencyString, + ), + currency: offerToUpdate.offersFullTime.bonus.currency, + value: offerToUpdate.offersFullTime.bonus.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } + if (offerToUpdate.offersFullTime.stocks != null) { + await ctx.prisma.offersFullTime.update({ + data: { + stocks: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersFullTime.stocks.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.stocks.currency, + value: offerToUpdate.offersFullTime.stocks.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersFullTime.stocks.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.stocks.currency, + value: offerToUpdate.offersFullTime.stocks.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } await ctx.prisma.offersFullTime.update({ data: { - baseSalary: { + totalCompensation: { upsert: { create: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.baseSalary.value, - offerToUpdate.offersFullTime.baseSalary.currency, + offerToUpdate.offersFullTime.totalCompensation.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, baseCurrencyString, ), currency: - offerToUpdate.offersFullTime.baseSalary.currency, - value: offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, + value: + offerToUpdate.offersFullTime.totalCompensation.value, }, update: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.baseSalary.value, - offerToUpdate.offersFullTime.baseSalary.currency, + offerToUpdate.offersFullTime.totalCompensation.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, baseCurrencyString, ), currency: - offerToUpdate.offersFullTime.baseSalary.currency, - value: offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, + value: + offerToUpdate.offersFullTime.totalCompensation.value, }, }, }, @@ -1691,30 +1915,151 @@ export const offersProfileRouter = createRouter() }, }); } - if (offerToUpdate.offersFullTime.bonus != null) { + } else if (currentOffer.jobType === JobType.FULLTIME) { + if (offerToUpdate.offersFullTime?.totalCompensation != null) { + await ctx.prisma.offersFullTime.update({ + data: { + level: offerToUpdate.offersFullTime.level ?? undefined, + title: offerToUpdate.offersFullTime.title, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + if (offerToUpdate.offersFullTime.baseSalary != null) { + await ctx.prisma.offersFullTime.update({ + data: { + baseSalary: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.baseSalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.baseSalary.currency, + value: offerToUpdate.offersFullTime.baseSalary.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.baseSalary.value, + offerToUpdate.offersFullTime.baseSalary.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.baseSalary.currency, + value: offerToUpdate.offersFullTime.baseSalary.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } + if (offerToUpdate.offersFullTime.bonus != null) { + await ctx.prisma.offersFullTime.update({ + data: { + bonus: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.bonus.value, + offerToUpdate.offersFullTime.bonus.currency, + baseCurrencyString, + ), + currency: offerToUpdate.offersFullTime.bonus.currency, + value: offerToUpdate.offersFullTime.bonus.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.bonus.value, + offerToUpdate.offersFullTime.bonus.currency, + baseCurrencyString, + ), + currency: offerToUpdate.offersFullTime.bonus.currency, + value: offerToUpdate.offersFullTime.bonus.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } + if (offerToUpdate.offersFullTime.stocks != null) { + await ctx.prisma.offersFullTime.update({ + data: { + stocks: { + upsert: { + create: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersFullTime.stocks.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.stocks.currency, + value: offerToUpdate.offersFullTime.stocks.value, + }, + update: { + baseCurrency: baseCurrencyString, + baseValue: await convert( + offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersFullTime.stocks.currency, + baseCurrencyString, + ), + currency: + offerToUpdate.offersFullTime.stocks.currency, + value: offerToUpdate.offersFullTime.stocks.value, + }, + }, + }, + }, + where: { + id: offerToUpdate.offersFullTime.id, + }, + }); + } await ctx.prisma.offersFullTime.update({ data: { - bonus: { + totalCompensation: { upsert: { create: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.bonus.value, - offerToUpdate.offersFullTime.bonus.currency, + offerToUpdate.offersFullTime.totalCompensation.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, baseCurrencyString, ), - currency: offerToUpdate.offersFullTime.bonus.currency, - value: offerToUpdate.offersFullTime.bonus.value, + currency: + offerToUpdate.offersFullTime.totalCompensation + .currency, + value: + offerToUpdate.offersFullTime.totalCompensation.value, }, update: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.bonus.value, - offerToUpdate.offersFullTime.bonus.currency, + offerToUpdate.offersFullTime.totalCompensation.value, + offerToUpdate.offersFullTime.totalCompensation + .currency, baseCurrencyString, ), - currency: offerToUpdate.offersFullTime.bonus.currency, - value: offerToUpdate.offersFullTime.bonus.value, + currency: + offerToUpdate.offersFullTime.totalCompensation + .currency, + value: + offerToUpdate.offersFullTime.totalCompensation.value, }, }, }, @@ -1724,80 +2069,66 @@ export const offersProfileRouter = createRouter() }, }); } - if (offerToUpdate.offersFullTime.stocks != null) { - await ctx.prisma.offersFullTime.update({ + + await ctx.prisma.offersOffer.update({ + data: { + offersIntern: undefined, + offersInternId: null + }, + where: { + id: offerToUpdate.id + } + }) + } else if (currentOffer.jobType === JobType.INTERN) { + if (offerToUpdate.offersIntern?.monthlySalary != null) { + await ctx.prisma.offersIntern.update({ data: { - stocks: { + internshipCycle: + offerToUpdate.offersIntern.internshipCycle ?? undefined, + monthlySalary: { upsert: { create: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.stocks.value, - offerToUpdate.offersFullTime.stocks.currency, + offerToUpdate.offersIntern.monthlySalary.value, + offerToUpdate.offersIntern.monthlySalary.currency, baseCurrencyString, ), currency: - offerToUpdate.offersFullTime.stocks.currency, - value: offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersIntern.monthlySalary.currency, + value: offerToUpdate.offersIntern.monthlySalary.value, }, update: { baseCurrency: baseCurrencyString, baseValue: await convert( - offerToUpdate.offersFullTime.stocks.value, - offerToUpdate.offersFullTime.stocks.currency, + offerToUpdate.offersIntern.monthlySalary.value, + offerToUpdate.offersIntern.monthlySalary.currency, baseCurrencyString, ), currency: - offerToUpdate.offersFullTime.stocks.currency, - value: offerToUpdate.offersFullTime.stocks.value, + offerToUpdate.offersIntern.monthlySalary.currency, + value: offerToUpdate.offersIntern.monthlySalary.value, }, }, }, + startYear: offerToUpdate.offersIntern.startYear ?? undefined, + title: offerToUpdate.offersIntern.title, }, where: { - id: offerToUpdate.offersFullTime.id, + id: offerToUpdate.offersIntern.id, }, }); } - await ctx.prisma.offersFullTime.update({ + + await ctx.prisma.offersOffer.update({ data: { - totalCompensation: { - upsert: { - create: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - offerToUpdate.offersFullTime.totalCompensation.value, - offerToUpdate.offersFullTime.totalCompensation - .currency, - baseCurrencyString, - ), - currency: - offerToUpdate.offersFullTime.totalCompensation - .currency, - value: - offerToUpdate.offersFullTime.totalCompensation.value, - }, - update: { - baseCurrency: baseCurrencyString, - baseValue: await convert( - offerToUpdate.offersFullTime.totalCompensation.value, - offerToUpdate.offersFullTime.totalCompensation - .currency, - baseCurrencyString, - ), - currency: - offerToUpdate.offersFullTime.totalCompensation - .currency, - value: - offerToUpdate.offersFullTime.totalCompensation.value, - }, - }, - }, + offersFullTime: undefined, + offersFullTimeId: null }, where: { - id: offerToUpdate.offersFullTime.id, - }, - }); + id: offerToUpdate.id + } + }) } } else { // Create new offer