From d99790613b0e3209facd2ae818d0ee7a08d23854 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Mon, 10 Oct 2022 16:20:48 +0800 Subject: [PATCH] [offers][fix] fix bugs create profile bugs --- .../server/router/offers-profile-router.ts | 252 +++++++++++------- 1 file changed, 163 insertions(+), 89 deletions(-) diff --git a/apps/portal/src/server/router/offers-profile-router.ts b/apps/portal/src/server/router/offers-profile-router.ts index 0d9eb845..afc245ee 100644 --- a/apps/portal/src/server/router/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers-profile-router.ts @@ -1,5 +1,6 @@ import crypto from "crypto"; import { z } from "zod"; +import { Prisma } from "@prisma/client"; import { createProtectedRouter } from "./context"; @@ -31,22 +32,22 @@ const offer = z.object({ }) const experience = z.object({ - companyId: z.string(), - durationInMonths: z.number(), - jobType: z.string(), + companyId: z.string().optional(), + durationInMonths: z.number().optional(), + jobType: z.string().optional(), level: z.string().optional(), monthlySalary: valuation.optional(), - specialization: z.string(), - title: z.string(), + specialization: z.string().optional(), + title: z.string().optional(), totalCompensation: valuation.optional(), }) const education = z.object({ - endDate: z.date(), - field: z.string(), - school: z.string(), - startDate: z.date(), - type: z.string(), + endDate: z.date().optional(), + field: z.string().optional(), + school: z.string().optional(), + startDate: z.date().optional(), + type: z.string().optional(), }) export const offersProfileRouter = createProtectedRouter().mutation( @@ -72,12 +73,12 @@ export const offersProfileRouter = createProtectedRouter().mutation( .update(Date.now().toString()) .digest("hex"); - return await ctx.prisma.offersProfile.create({ + const profile = await ctx.prisma.offersProfile.create({ data: { background: { create: { educations: { - create: [ + create: input.background.educations.map((x) => ({ endDate: x.endDate, field: x.field, @@ -85,12 +86,11 @@ export const offersProfileRouter = createProtectedRouter().mutation( startDate: x.startDate, type: x.type })) - ] }, experiences: { - create: [ + create: input.background.experiences.map((x) => { - if (x.jobType === "Intern") { + if (x.jobType === "INTERN" && x.totalCompensation?.currency !== undefined && x.totalCompensation.value !== undefined) { return { companyId: { connect: { @@ -110,28 +110,29 @@ export const offersProfileRouter = createProtectedRouter().mutation( }, } } - - return { - companyId: { - connect: { - id: x.companyId - } - }, - durationInMonths: x.durationInMonths, - jobType: x.jobType, - monthlySalary: { - create: { - currency: x.monthlySalary?.currency, - value: x.totalCompensation?.value - } - }, - specialization: x.specialization, - title: x.title, + if (x.jobType === "FULLTIME" && x.monthlySalary?.currency !== undefined && x.monthlySalary.value !== undefined) { + return { + companyId: { + connect: { + id: x.companyId + } + }, + durationInMonths: x.durationInMonths, + jobType: x.jobType, + monthlySalary: { + create: { + currency: x.monthlySalary?.currency, + value: x.monthlySalary?.value + } + }, + specialization: x.specialization, + title: x.title, + } } + throw Prisma.PrismaClientKnownRequestError }) - ] }, specificYoes: { create: @@ -147,24 +148,24 @@ export const offersProfileRouter = createProtectedRouter().mutation( offers: { create: input.offers.map((x) => { - if (x.jobType === "Intern") { + if (x.jobType === "INTERN" || x.jobType === "FULLTIME") { return { - OffersIntern: { - create: { - internshipCycle: x.job.internshipCycle, - monthlySalary: { - create: { - currency: x.job.monthlySalary?.currency, - value: x.job.monthlySalary?.value - } - }, - specialization: x.job.specialization, - startYear: x.job.startYear, - title: x.job.title, - } - }, + // OffersIntern: { + // create: { + // internshipCycle: x.job.internshipCycle, + // monthlySalary: { + // create: { + // currency: x.job.monthlySalary?.currency, + // value: x.job.monthlySalary?.value + // } + // }, + // specialization: x.job.specialization, + // startYear: x.job.startYear, + // title: x.job.title, + // } + // }, comments: x.comments, - companyId: { + company: { connect: { id: x.companyId } @@ -175,53 +176,126 @@ export const offersProfileRouter = createProtectedRouter().mutation( negotiationStrategy: x.negotiationStrategy } } - return { - OffersFullTime: { - create: { - baseSalaryId: { - create: { - currency: x.job.base?.currency, - value: x.job.base?.value - } - }, - bonusId: { - create: { - currency: x.job.bonus?.currency, - value: x.job.bonus?.value - } - }, - level: x.job.level, - specialization: x.job.specialization, - startYear: x.job.startYear, - stocks: { - create: { - currency: x.job.stocks?.currency, - value: x.job.stocks?.value, - } - }, - title: x.job.title, - } - }, - comments: x.comments, - companyId: { - connect: { - id: x.companyId - } - }, - jobType: x.jobType, - location: x.location, - monthYearReceived: x.monthYearReceived, - negotiationStrategy: x.negotiationStrategy - } + // If (x.jobType === "FULLTIME") { + // return { + // // OffersFullTime: { + // // create: { + // // baseSalaryId: { + // // create: { + // // currency: x.job.base?.currency, + // // value: x.job.base?.value + // // } + // // }, + // // bonusId: { + // // create: { + // // currency: x.job.bonus?.currency, + // // value: x.job.bonus?.value + // // } + // // }, + // // level: x.job.level, + // // specialization: x.job.specialization, + // // startYear: x.job.startYear, + // // stocks: { + // // create: { + // // currency: x.job.stocks?.currency, + // // value: x.job.stocks?.value, + // // } + // // }, + // // title: x.job.title, + // // } + // // }, + // comments: x.comments, + // company: { + // connect: { + // id: x.companyId + // } + // }, + // jobType: x.jobType, + // location: x.location, + // monthYearReceived: x.monthYearReceived, + // negotiationStrategy: x.negotiationStrategy + // } + // } + + // Throw error + throw Prisma.PrismaClientKnownRequestError }) }, profileName: "anonymous account", - } + }, + // Include: { + // select: { + // id: true + // } + // } }); + // Create specific jobs + input.offers.map((x) => { + if (x.jobType === "INTERN" && x.job.internshipCycle !== undefined && x.job.monthlySalary?.currency !== undefined && x.job.monthlySalary.value !== undefined && x.job.startYear !== undefined) { + ctx.prisma.offersIntern.create({ + data: { + internshipCycle: x.job.internshipCycle, + monthlySalary: { + create: { + currency: x.job.monthlySalary?.currency, + value: x.job.monthlySalary?.value + } + }, + offer: { + connect: { + id: profile.id + } + }, + specialization: x.job.specialization, + startYear: x.job.startYear, + title: x.job.title, + } + }) + } else if (x.jobType === "FULLTIME" && x.job.startYear !== undefined && x.job.base?.currency !== undefined && x.job.base?.value !== undefined && x.job.bonus?.currency !== undefined && x.job.bonus?.value !== undefined && x.job.stocks?.currency !== undefined && x.job.stocks?.value !== undefined && x.job.totalCompensation?.currency !== undefined && x.job.totalCompensation?.value !== undefined && x.job.level !== undefined) { + ctx.prisma.offersFullTime.create({ + data: { + baseSalary: { + create: { + currency: x.job.base?.currency, + value: x.job.base?.value + } + }, + bonus: { + create: { + currency: x.job.bonus?.currency, + value: x.job.bonus?.value + } + }, + level: x.job.level, + offer: { + connect: { + id: profile.id + } + }, + specialization: x.job.specialization, + stocks: { + create: { + currency: x.job.stocks?.currency, + value: x.job.stocks?.value, + } + }, + title: x.job.title, + totalCompensation: { + create: { + currency: x.job.totalCompensation?.currency, + value: x.job.totalCompensation?.value, + } + }, + } + }) + } + + throw Prisma.PrismaClientKnownRequestError + }) // TODO: add analysis to profile object then return - // return profile + return profile } }, );