|
|
|
@ -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.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
await ctx.prisma.user.update({
|
|
|
|
|
data: {
|
|
|
|
|
OffersProfile: {
|
|
|
|
|
disconnect: [
|
|
|
|
|
{
|
|
|
|
|
id: input.profileId,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: userId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|