[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,18 +556,12 @@ 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++) {
// for (let i = 0; i < users.length; i++) { if (users[i].id === inputUserId) {
// if (users[i].id === inputUserId) { profileDto.isSaved = true;
// profileDto.isSaved = true }
// }
// }
// TODO: REMOVE THIS ONCE U CHANGE THE SCHEMA
if (users?.id === inputUserId) {
profileDto.isSaved = true;
} }
} }

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

@ -3,129 +3,130 @@ 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';
export const offersUserProfileRouter = createProtectedRouter() export const offersUserProfileRouter = createProtectedRouter()
.mutation('addToUserProfile', { .mutation('addToUserProfile', {
input: z.object({ input: z.object({
profileId: z.string(), profileId: z.string(),
token: z.string(), token: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const profile = await ctx.prisma.offersProfile.findFirst({ const profile = await ctx.prisma.offersProfile.findFirst({
where: { where: {
id: input.profileId, id: input.profileId,
}, },
}); });
const profileEditToken = profile?.editToken; const profileEditToken = profile?.editToken;
if (profileEditToken === input.token) { 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 return addToProfileResponseMapper(updated);
const updated = await ctx.prisma.offersProfile.update({ }
data: {
user: { throw new trpc.TRPCError({
connect: { code: 'UNAUTHORIZED',
id: userId, 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.',
});
}, },
}) where: {
.mutation('getUserProfiles', { id: userId,
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
const profiles = await ctx.prisma.user.findFirst({ return getUserProfileResponseMapper(result);
include: { },
OffersProfile: true })
}, .mutation('removeFromUserProfile', {
where: { input: z.object({
id: userId profileId: z.string(),
} }),
}) async resolve({ ctx, input }) {
const userId = ctx.session.user.id;
// Validation const profiles = await ctx.prisma.user.findFirst({
let doesProfileExist = false; include: {
OffersProfile: true,
},
where: {
id: userId,
},
});
if (profiles?.OffersProfile) { // Validation
for (let i = 0; i < profiles.OffersProfile.length; i++) { let doesProfileExist = false;
if (profiles.OffersProfile[i].id === input.profileId) {
doesProfileExist = true
}
}
}
if (!doesProfileExist) { if (profiles?.OffersProfile) {
throw new TRPCError({ for (let i = 0; i < profiles.OffersProfile.length; i++) {
code: 'NOT_FOUND', if (profiles.OffersProfile[i].id === input.profileId) {
message: 'No such profile id saved.' doesProfileExist = true;
}) }
} }
}
await ctx.prisma.user.update({ if (!doesProfileExist) {
data: { throw new TRPCError({
OffersProfile: { code: 'NOT_FOUND',
disconnect: [{ message: 'No such profile id saved.',
id: input.profileId });
}] }
}
},
where: {
id: userId
}
})
} await ctx.prisma.user.update({
}) data: {
OffersProfile: {
disconnect: [
{
id: input.profileId,
},
],
},
},
where: {
id: userId,
},
});
},
});

Loading…
Cancel
Save