diff --git a/apps/portal/src/mappers/offers-mappers.ts b/apps/portal/src/mappers/offers-mappers.ts index d012078f..6e72ddc5 100644 --- a/apps/portal/src/mappers/offers-mappers.ts +++ b/apps/portal/src/mappers/offers-mappers.ts @@ -33,8 +33,9 @@ import type { ProfileAnalysis, ProfileOffer, SpecificYoe, - Valuation, -} from '~/types/offers'; + UserProfile, + UserProfileOffer, + Valuation} from '~/types/offers'; const analysisOfferDtoMapper = ( offer: OffersOffer & { @@ -626,3 +627,84 @@ export const getOffersResponseMapper = ( }; return getOffersResponse; }; + +export const getUserProfileResponeMapper = (res: User & { + OffersProfile: Array; + }>; +} | null): Array => { + if (res) { + return res.OffersProfile.map((profile) => { + return { + createdAt: profile.createdAt, + id: profile.id, + offers: profile.offers.map((offer) => { + return userProfileOfferDtoMapper(offer) + }), + profileName: profile.profileName, + token: profile.editToken + } + }) + } + + return [] +} + +const userProfileOfferDtoMapper = ( + offer: OffersOffer & { + company: Company; + offersFullTime: (OffersFullTime & { totalCompensation: OffersCurrency }) | null; + offersIntern: (OffersIntern & { monthlySalary: OffersCurrency }) | null; + }): UserProfileOffer => { + const mappedOffer: UserProfileOffer = { + company: offersCompanyDtoMapper(offer.company), + id: offer.id, + income: { + baseCurrency: '', + baseValue: -1, + currency: '', + id: '', + value: -1, + }, + jobType: offer.jobType, + level: offer.offersFullTime?.level ?? '', + location: offer.location, + monthYearReceived: offer.monthYearReceived, + title: + offer.jobType === JobType.FULLTIME + ? offer.offersFullTime?.title ?? '' + : offer.offersIntern?.title ?? '', + } + + if (offer.offersFullTime?.totalCompensation) { + mappedOffer.income.value = + offer.offersFullTime.totalCompensation.value; + mappedOffer.income.currency = + offer.offersFullTime.totalCompensation.currency; + mappedOffer.income.id = offer.offersFullTime.totalCompensation.id; + mappedOffer.income.baseValue = + offer.offersFullTime.totalCompensation.baseValue; + mappedOffer.income.baseCurrency = + offer.offersFullTime.totalCompensation.baseCurrency; + } else if (offer.offersIntern?.monthlySalary) { + mappedOffer.income.value = offer.offersIntern.monthlySalary.value; + mappedOffer.income.currency = + offer.offersIntern.monthlySalary.currency; + mappedOffer.income.id = offer.offersIntern.monthlySalary.id; + mappedOffer.income.baseValue = + offer.offersIntern.monthlySalary.baseValue; + mappedOffer.income.baseCurrency = + offer.offersIntern.monthlySalary.baseCurrency; + } else { + throw new TRPCError({ + code: 'NOT_FOUND', + message: 'Total Compensation or Salary not found', + }); + } + + return mappedOffer +} \ No newline at end of file diff --git a/apps/portal/src/server/router/offers/offers-user-profile-router.ts b/apps/portal/src/server/router/offers/offers-user-profile-router.ts index 9d25257a..e95ed5a3 100644 --- a/apps/portal/src/server/router/offers/offers-user-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-user-profile-router.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; import * as trpc from '@trpc/server'; import { - addToProfileResponseMapper, + addToProfileResponseMapper, getUserProfileResponeMapper, } from '~/mappers/offers-mappers'; import { createProtectedRouter } from '../context'; @@ -13,35 +13,68 @@ export const offersUserProfileRouter = createProtectedRouter() token: z.string(), }), async resolve({ ctx, input }) { - const profile = await ctx.prisma.offersProfile.findFirst({ - where: { - id: input.profileId, - }, - }); - - const profileEditToken = profile?.editToken; - if (profileEditToken === input.token) { - - const userId = ctx.session.user.id - const updated = await ctx.prisma.offersProfile.update({ - data: { - user: { - connect: { - id: userId, - }, - }, - }, - where: { + const profile = await ctx.prisma.offersProfile.findFirst({ + where: { id: input.profileId, - }, + }, }); - return addToProfileResponseMapper(updated); - } + const profileEditToken = profile?.editToken; + if (profileEditToken === input.token) { - throw new trpc.TRPCError({ - code: 'UNAUTHORIZED', - message: 'Invalid token.', - }); + const userId = ctx.session.user.id + const updated = await ctx.prisma.offersProfile.update({ + data: { + user: { + connect: { + id: userId, + }, + }, + }, + where: { + id: input.profileId, + }, + }); + + return addToProfileResponseMapper(updated); + } + + throw new trpc.TRPCError({ + code: 'UNAUTHORIZED', + message: 'Invalid token.', + }); }, + }) + .mutation('getUserProfiles', { + async resolve({ ctx }) { + const userId = ctx.session.user.id + const result = await ctx.prisma.user.findFirst({ + include: { + OffersProfile: { + include: { + offers: { + include: { + company: true, + offersFullTime: { + include: { + totalCompensation: true + } + }, + offersIntern: { + include: { + monthlySalary: true + } + } + } + } + } + } + }, + where: { + id: userId + } + }) + + return getUserProfileResponeMapper(result) + } }); \ No newline at end of file diff --git a/apps/portal/src/types/offers.d.ts b/apps/portal/src/types/offers.d.ts index f2b26332..543d7aa7 100644 --- a/apps/portal/src/types/offers.d.ts +++ b/apps/portal/src/types/offers.d.ts @@ -183,3 +183,22 @@ export type AddToProfileResponse = { profileName: string; userId: string; }; + +export type UserProfile = { + createdAt: Date; + id: string; + offers: Array; + profileName: string; + token: string; +} + +export type UserProfileOffer = { + company: OffersCompany; + id: string; + income: Valuation; + jobType: JobType; + level: string; + location: string; + monthYearReceived: Date; + title: string; +} \ No newline at end of file