diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 0f8ff33b..e0dd9065 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -196,8 +196,7 @@ model OffersProfile { offers OffersOffer[] - user User? @relation(fields: [userId], references: [id]) - userId String? + users User[] analysis OffersAnalysis? } diff --git a/apps/portal/src/components/offers/profile/ProfileDetails.tsx b/apps/portal/src/components/offers/profile/ProfileDetails.tsx index a83c8781..3f61060b 100644 --- a/apps/portal/src/components/offers/profile/ProfileDetails.tsx +++ b/apps/portal/src/components/offers/profile/ProfileDetails.tsx @@ -115,8 +115,9 @@ function ProfileAnalysis({ return (
- {!analysis &&

No analysis available.

} - {analysis && ( + {!analysis ? ( +

No analysis available.

+ ) : ( ; - user: User | null; + users: Array; }, inputToken: string | undefined, inputUserId: string | null | undefined, @@ -556,18 +556,12 @@ export const profileDtoMapper = ( profileDto.editToken = profile.editToken ?? null; profileDto.isEditable = true; - const users = profile.user; + const { users } = profile; - // TODO: BRYANN UNCOMMENT THIS ONCE U CHANGE THE SCHEMA - // for (let i = 0; i < users.length; i++) { - // if (users[i].id === inputUserId) { - // profileDto.isSaved = true - // } - // } - - // TODO: REMOVE THIS ONCE U CHANGE THE SCHEMA - if (users?.id === inputUserId) { - profileDto.isSaved = true; + for (let i = 0; i < users.length; i++) { + if (users[i].id === inputUserId) { + profileDto.isSaved = true; + } } } 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 f6567560..4c6c742d 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -237,7 +237,7 @@ export const offersProfileRouter = createRouter() }, }, }, - user: true, + users: true, }, where: { id: input.profileId, 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 d0468abb..d6837ddc 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 @@ -3,129 +3,130 @@ import * as trpc from '@trpc/server'; import { TRPCError } from '@trpc/server'; import { - addToProfileResponseMapper, getUserProfileResponseMapper, + addToProfileResponseMapper, + getUserProfileResponseMapper, } from '~/mappers/offers-mappers'; import { createProtectedRouter } from '../context'; export const offersUserProfileRouter = createProtectedRouter() - .mutation('addToUserProfile', { - input: z.object({ - profileId: z.string(), - token: z.string(), - }), - async resolve({ ctx, input }) { - const profile = await ctx.prisma.offersProfile.findFirst({ - where: { - id: input.profileId, - }, - }); + .mutation('addToUserProfile', { + input: z.object({ + profileId: z.string(), + 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 profileEditToken = profile?.editToken; + if (profileEditToken === input.token) { + const userId = ctx.session.user.id; + const updated = await ctx.prisma.offersProfile.update({ + data: { + users: { + connect: { + id: userId, + }, + }, + }, + where: { + id: input.profileId, + }, + }); - const userId = ctx.session.user.id - const updated = await ctx.prisma.offersProfile.update({ - data: { - user: { - connect: { - id: userId, + 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: 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 getUserProfileResponseMapper(result) - } - }) - .mutation('removeFromUserProfile', { - input: z.object({ - profileId: z.string(), - }), - async resolve({ ctx, input }) { - const userId = ctx.session.user.id + where: { + id: userId, + }, + }); - const profiles = await ctx.prisma.user.findFirst({ - include: { - OffersProfile: true - }, - where: { - id: userId - } - }) + return getUserProfileResponseMapper(result); + }, + }) + .mutation('removeFromUserProfile', { + input: z.object({ + profileId: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session.user.id; - // Validation - let doesProfileExist = false; + const profiles = await ctx.prisma.user.findFirst({ + include: { + OffersProfile: true, + }, + where: { + id: userId, + }, + }); - if (profiles?.OffersProfile) { - for (let i = 0; i < profiles.OffersProfile.length; i++) { - if (profiles.OffersProfile[i].id === input.profileId) { - doesProfileExist = true - } - } - } + // Validation + let doesProfileExist = false; - if (!doesProfileExist) { - throw new TRPCError({ - code: 'NOT_FOUND', - message: 'No such profile id saved.' - }) - } + if (profiles?.OffersProfile) { + for (let i = 0; i < profiles.OffersProfile.length; i++) { + if (profiles.OffersProfile[i].id === input.profileId) { + doesProfileExist = true; + } + } + } - await ctx.prisma.user.update({ - data: { - OffersProfile: { - disconnect: [{ - id: input.profileId - }] - } - }, - where: { - id: userId - } - }) + if (!doesProfileExist) { + throw new TRPCError({ + code: 'NOT_FOUND', + message: 'No such profile id saved.', + }); + } - } - }) \ No newline at end of file + await ctx.prisma.user.update({ + data: { + OffersProfile: { + disconnect: [ + { + id: input.profileId, + }, + ], + }, + }, + where: { + id: userId, + }, + }); + }, + });