[offer][feat] add remove from user profile endpoint

pull/451/head
Stuart Long Chay Boon 2 years ago
parent 2acf394da3
commit a1fc43242e

@ -628,7 +628,7 @@ export const getOffersResponseMapper = (
return getOffersResponse; return getOffersResponse;
}; };
export const getUserProfileResponeMapper = (res: User & { export const getUserProfileResponseMapper = (res: User & {
OffersProfile: Array<OffersProfile & { OffersProfile: Array<OffersProfile & {
offers: Array<OffersOffer & { offers: Array<OffersOffer & {
company: Company; company: Company;

@ -1,11 +1,13 @@
import { z } from 'zod'; import { z } from 'zod';
import * as trpc from '@trpc/server'; import * as trpc from '@trpc/server';
import { TRPCError } from '@trpc/server';
import { import {
addToProfileResponseMapper, getUserProfileResponeMapper, 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({
@ -75,6 +77,55 @@ export const offersUserProfileRouter = createProtectedRouter()
} }
}) })
return getUserProfileResponeMapper(result) 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({
include: {
OffersProfile: true
},
where: {
id: userId
}
})
// Validation
let doesProfileExist = false;
if (profiles?.OffersProfile) {
for (let i = 0; i < profiles.OffersProfile.length; i++) {
if (profiles.OffersProfile[i].id === input.profileId) {
doesProfileExist = true
}
}
}
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
}
})
}
})
Loading…
Cancel
Save