[offers][fix] fix change experience type edit bug

pull/503/head
Stuart Long Chay Boon 2 years ago
parent 5a4e4e8977
commit acc9ab00e1

@ -972,7 +972,19 @@ export const offersProfileRouter = createRouter()
for (const exp of input.background.experiences) { for (const exp of input.background.experiences) {
if (exp.id) { 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({ await ctx.prisma.offersExperience.update({
data: { data: {
companyId: exp.companyId, // TODO: check if can change with connect or whether there is a difference 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, 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) { if (exp.totalCompensation) {
await ctx.prisma.offersExperience.update({ await ctx.prisma.offersExperience.update({
data: { data: {
monthlySalary: { totalCompensation: {
upsert: { upsert: {
create: { create: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
exp.monthlySalary.value, exp.totalCompensation.value,
exp.monthlySalary.currency, exp.totalCompensation.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: exp.monthlySalary.currency, currency: exp.totalCompensation.currency,
value: exp.monthlySalary.value, 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( where: {
exp.monthlySalary.value, id: exp.id,
exp.monthlySalary.currency, },
baseCurrencyString, });
), }
currency: exp.monthlySalary.currency, } else if (exp.jobType === JobType.INTERN) {
value: exp.monthlySalary.value, // 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: {
where: { id: exp.id,
id: exp.id, },
}, });
}); }
}
if (exp.totalCompensation) {
await ctx.prisma.offersExperience.update({ await ctx.prisma.offersExperience.update({
data: { data: {
totalCompensation: { totalCompensation: undefined,
upsert: { totalCompensationId: null,
create: { },
baseCurrency: baseCurrencyString, where: {
baseValue: await convert( id: exp.id
exp.totalCompensation.value, }
exp.totalCompensation.currency, })
baseCurrencyString, } else if (exp.jobType === JobType.FULLTIME) {
), if (exp.totalCompensation) {
currency: exp.totalCompensation.currency, await ctx.prisma.offersExperience.update({
value: exp.totalCompensation.value, data: {
}, totalCompensation: {
update: { upsert: {
baseCurrency: baseCurrencyString, create: {
baseValue: await convert( baseCurrency: baseCurrencyString,
exp.totalCompensation.value, baseValue: await convert(
exp.totalCompensation.currency, exp.totalCompensation.value,
baseCurrencyString, exp.totalCompensation.currency,
), baseCurrencyString,
currency: exp.totalCompensation.currency, ),
value: exp.totalCompensation.value, 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: {
where: { id: exp.id
id: exp.id, }
}, })
});
} }
} else if (!exp.id) { } else if (!exp.id) {
// Create new experience // Create new experience
@ -1581,6 +1684,18 @@ export const offersProfileRouter = createRouter()
for (const offerToUpdate of input.offers) { for (const offerToUpdate of input.offers) {
if (offerToUpdate.id) { if (offerToUpdate.id) {
// Update existing offer // 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({ await ctx.prisma.offersOffer.update({
data: { data: {
comments: offerToUpdate.comments, comments: offerToUpdate.comments,
@ -1606,82 +1721,191 @@ export const offersProfileRouter = createRouter()
}, },
}); });
if (offerToUpdate.offersIntern?.monthlySalary != null) { if (currentOffer.jobType === offerToUpdate.jobType) {
await ctx.prisma.offersIntern.update({ if (offerToUpdate.offersIntern?.monthlySalary != null) {
data: { await ctx.prisma.offersIntern.update({
internshipCycle: data: {
offerToUpdate.offersIntern.internshipCycle ?? undefined, internshipCycle:
monthlySalary: { offerToUpdate.offersIntern.internshipCycle ?? undefined,
upsert: { monthlySalary: {
create: { upsert: {
baseCurrency: baseCurrencyString, create: {
baseValue: await convert( baseCurrency: baseCurrencyString,
offerToUpdate.offersIntern.monthlySalary.value, baseValue: await convert(
offerToUpdate.offersIntern.monthlySalary.currency, offerToUpdate.offersIntern.monthlySalary.value,
baseCurrencyString, offerToUpdate.offersIntern.monthlySalary.currency,
), baseCurrencyString,
currency: ),
offerToUpdate.offersIntern.monthlySalary.currency, currency:
value: offerToUpdate.offersIntern.monthlySalary.value, offerToUpdate.offersIntern.monthlySalary.currency,
}, value: offerToUpdate.offersIntern.monthlySalary.value,
update: { },
baseCurrency: baseCurrencyString, update: {
baseValue: await convert( baseCurrency: baseCurrencyString,
offerToUpdate.offersIntern.monthlySalary.value, baseValue: await convert(
offerToUpdate.offersIntern.monthlySalary.currency, offerToUpdate.offersIntern.monthlySalary.value,
baseCurrencyString, offerToUpdate.offersIntern.monthlySalary.currency,
), baseCurrencyString,
currency: ),
offerToUpdate.offersIntern.monthlySalary.currency, currency:
value: offerToUpdate.offersIntern.monthlySalary.value, offerToUpdate.offersIntern.monthlySalary.currency,
value: offerToUpdate.offersIntern.monthlySalary.value,
},
}, },
}, },
startYear: offerToUpdate.offersIntern.startYear ?? undefined,
title: offerToUpdate.offersIntern.title,
}, },
startYear: offerToUpdate.offersIntern.startYear ?? undefined, where: {
title: offerToUpdate.offersIntern.title, id: offerToUpdate.offersIntern.id,
}, },
where: { });
id: offerToUpdate.offersIntern.id, }
},
});
}
if (offerToUpdate.offersFullTime?.totalCompensation != null) { if (offerToUpdate.offersFullTime?.totalCompensation != null) {
await ctx.prisma.offersFullTime.update({ await ctx.prisma.offersFullTime.update({
data: { data: {
level: offerToUpdate.offersFullTime.level ?? undefined, level: offerToUpdate.offersFullTime.level ?? undefined,
title: offerToUpdate.offersFullTime.title, title: offerToUpdate.offersFullTime.title,
}, },
where: { where: {
id: offerToUpdate.offersFullTime.id, id: offerToUpdate.offersFullTime.id,
}, },
}); });
if (offerToUpdate.offersFullTime.baseSalary != null) { 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({ await ctx.prisma.offersFullTime.update({
data: { data: {
baseSalary: { totalCompensation: {
upsert: { upsert: {
create: { create: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.baseSalary.value, offerToUpdate.offersFullTime.totalCompensation.value,
offerToUpdate.offersFullTime.baseSalary.currency, offerToUpdate.offersFullTime.totalCompensation
.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: currency:
offerToUpdate.offersFullTime.baseSalary.currency, offerToUpdate.offersFullTime.totalCompensation
value: offerToUpdate.offersFullTime.baseSalary.value, .currency,
value:
offerToUpdate.offersFullTime.totalCompensation.value,
}, },
update: { update: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.baseSalary.value, offerToUpdate.offersFullTime.totalCompensation.value,
offerToUpdate.offersFullTime.baseSalary.currency, offerToUpdate.offersFullTime.totalCompensation
.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: currency:
offerToUpdate.offersFullTime.baseSalary.currency, offerToUpdate.offersFullTime.totalCompensation
value: offerToUpdate.offersFullTime.baseSalary.value, .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({ await ctx.prisma.offersFullTime.update({
data: { data: {
bonus: { totalCompensation: {
upsert: { upsert: {
create: { create: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.bonus.value, offerToUpdate.offersFullTime.totalCompensation.value,
offerToUpdate.offersFullTime.bonus.currency, offerToUpdate.offersFullTime.totalCompensation
.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: offerToUpdate.offersFullTime.bonus.currency, currency:
value: offerToUpdate.offersFullTime.bonus.value, offerToUpdate.offersFullTime.totalCompensation
.currency,
value:
offerToUpdate.offersFullTime.totalCompensation.value,
}, },
update: { update: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.bonus.value, offerToUpdate.offersFullTime.totalCompensation.value,
offerToUpdate.offersFullTime.bonus.currency, offerToUpdate.offersFullTime.totalCompensation
.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: offerToUpdate.offersFullTime.bonus.currency, currency:
value: offerToUpdate.offersFullTime.bonus.value, 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: { data: {
stocks: { internshipCycle:
offerToUpdate.offersIntern.internshipCycle ?? undefined,
monthlySalary: {
upsert: { upsert: {
create: { create: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.stocks.value, offerToUpdate.offersIntern.monthlySalary.value,
offerToUpdate.offersFullTime.stocks.currency, offerToUpdate.offersIntern.monthlySalary.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: currency:
offerToUpdate.offersFullTime.stocks.currency, offerToUpdate.offersIntern.monthlySalary.currency,
value: offerToUpdate.offersFullTime.stocks.value, value: offerToUpdate.offersIntern.monthlySalary.value,
}, },
update: { update: {
baseCurrency: baseCurrencyString, baseCurrency: baseCurrencyString,
baseValue: await convert( baseValue: await convert(
offerToUpdate.offersFullTime.stocks.value, offerToUpdate.offersIntern.monthlySalary.value,
offerToUpdate.offersFullTime.stocks.currency, offerToUpdate.offersIntern.monthlySalary.currency,
baseCurrencyString, baseCurrencyString,
), ),
currency: currency:
offerToUpdate.offersFullTime.stocks.currency, offerToUpdate.offersIntern.monthlySalary.currency,
value: offerToUpdate.offersFullTime.stocks.value, value: offerToUpdate.offersIntern.monthlySalary.value,
}, },
}, },
}, },
startYear: offerToUpdate.offersIntern.startYear ?? undefined,
title: offerToUpdate.offersIntern.title,
}, },
where: { where: {
id: offerToUpdate.offersFullTime.id, id: offerToUpdate.offersIntern.id,
}, },
}); });
} }
await ctx.prisma.offersFullTime.update({
await ctx.prisma.offersOffer.update({
data: { data: {
totalCompensation: { offersFullTime: undefined,
upsert: { offersFullTimeId: null
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,
},
},
},
}, },
where: { where: {
id: offerToUpdate.offersFullTime.id, id: offerToUpdate.id
}, }
}); })
} }
} else { } else {
// Create new offer // Create new offer

Loading…
Cancel
Save