[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[]
user User? @relation(fields: [userId], references: [id])
userId String?
users User[]
analysis OffersAnalysis?
}

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

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

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

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

Loading…
Cancel
Save