[offers][chore] Change user relation in OffersProfile from one-to-many to many-to-many

pull/501/head^2
Bryann Yeap Kok Keong 3 years ago
parent cc52f362d4
commit d69e5c2c99

@ -196,8 +196,7 @@ model OffersProfile {
offers OffersOffer[] offers OffersOffer[]
user User? @relation(fields: [userId], references: [id]) users User[]
userId String?
analysis OffersAnalysis? analysis OffersAnalysis?
} }

@ -115,8 +115,9 @@ function ProfileAnalysis({
return ( return (
<div className="mx-8 my-4"> <div className="mx-8 my-4">
{!analysis && <p>No analysis available.</p>} {!analysis ? (
{analysis && ( <p>No analysis available.</p>
) : (
<OfferAnalysis <OfferAnalysis
allAnalysis={analysis} allAnalysis={analysis}
isError={false} isError={false}

@ -536,7 +536,7 @@ export const profileDtoMapper = (
offersIntern: (OffersIntern & { monthlySalary: OffersCurrency }) | null; offersIntern: (OffersIntern & { monthlySalary: OffersCurrency }) | null;
} }
>; >;
user: User | null; users: Array<User>;
}, },
inputToken: string | undefined, inputToken: string | undefined,
inputUserId: string | null | undefined, inputUserId: string | null | undefined,
@ -556,20 +556,14 @@ export const profileDtoMapper = (
profileDto.editToken = profile.editToken ?? null; profileDto.editToken = profile.editToken ?? null;
profileDto.isEditable = true; 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 for (let i = 0; i < users.length; i++) {
if (users?.id === inputUserId) { if (users[i].id === inputUserId) {
profileDto.isSaved = true; profileDto.isSaved = true;
} }
} }
}
return profileDto; return profileDto;
}; };

@ -237,7 +237,7 @@ export const offersProfileRouter = createRouter()
}, },
}, },
}, },
user: true, users: true,
}, },
where: { where: {
id: input.profileId, id: input.profileId,

@ -3,7 +3,8 @@ import * as trpc from '@trpc/server';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { import {
addToProfileResponseMapper, getUserProfileResponseMapper, addToProfileResponseMapper,
getUserProfileResponseMapper,
} from '~/mappers/offers-mappers'; } from '~/mappers/offers-mappers';
import { createProtectedRouter } from '../context'; import { createProtectedRouter } from '../context';
@ -23,11 +24,10 @@ export const offersUserProfileRouter = createProtectedRouter()
const profileEditToken = profile?.editToken; const profileEditToken = profile?.editToken;
if (profileEditToken === input.token) { if (profileEditToken === input.token) {
const userId = ctx.session.user.id;
const userId = ctx.session.user.id
const updated = await ctx.prisma.offersProfile.update({ const updated = await ctx.prisma.offersProfile.update({
data: { data: {
user: { users: {
connect: { connect: {
id: userId, id: userId,
}, },
@ -49,7 +49,7 @@ export const offersUserProfileRouter = createProtectedRouter()
}) })
.mutation('getUserProfiles', { .mutation('getUserProfiles', {
async resolve({ ctx }) { async resolve({ ctx }) {
const userId = ctx.session.user.id const userId = ctx.session.user.id;
const result = await ctx.prisma.user.findFirst({ const result = await ctx.prisma.user.findFirst({
include: { include: {
OffersProfile: { OffersProfile: {
@ -59,42 +59,42 @@ export const offersUserProfileRouter = createProtectedRouter()
company: true, company: true,
offersFullTime: { offersFullTime: {
include: { include: {
totalCompensation: true totalCompensation: true,
} },
}, },
offersIntern: { offersIntern: {
include: { include: {
monthlySalary: true monthlySalary: true,
} },
} },
} },
} },
} },
} },
}, },
where: { where: {
id: userId id: userId,
} },
}) });
return getUserProfileResponseMapper(result) return getUserProfileResponseMapper(result);
} },
}) })
.mutation('removeFromUserProfile', { .mutation('removeFromUserProfile', {
input: z.object({ input: z.object({
profileId: z.string(), profileId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session.user.id const userId = ctx.session.user.id;
const profiles = await ctx.prisma.user.findFirst({ const profiles = await ctx.prisma.user.findFirst({
include: { include: {
OffersProfile: true OffersProfile: true,
}, },
where: { where: {
id: userId id: userId,
} },
}) });
// Validation // Validation
let doesProfileExist = false; let doesProfileExist = false;
@ -102,7 +102,7 @@ export const offersUserProfileRouter = createProtectedRouter()
if (profiles?.OffersProfile) { if (profiles?.OffersProfile) {
for (let i = 0; i < profiles.OffersProfile.length; i++) { for (let i = 0; i < profiles.OffersProfile.length; i++) {
if (profiles.OffersProfile[i].id === input.profileId) { if (profiles.OffersProfile[i].id === input.profileId) {
doesProfileExist = true doesProfileExist = true;
} }
} }
} }
@ -110,22 +110,23 @@ export const offersUserProfileRouter = createProtectedRouter()
if (!doesProfileExist) { if (!doesProfileExist) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'No such profile id saved.' message: 'No such profile id saved.',
}) });
} }
await ctx.prisma.user.update({ await ctx.prisma.user.update({
data: { data: {
OffersProfile: { OffersProfile: {
disconnect: [{ disconnect: [
id: input.profileId {
}] id: input.profileId,
} },
],
},
}, },
where: { where: {
id: userId id: userId,
} },
}) });
},
} });
})

Loading…
Cancel
Save