[offers][feat] create protected router to store user profile endpoints

pull/451/head
Stuart Long Chay Boon 2 years ago
parent d12f47cff5
commit 7e5300d9fc

@ -16,7 +16,7 @@ function Test() {
});
const addToUserProfileMutation = trpc.useMutation(
['offers.profile.addToUserProfile'],
['offers.user.profile.addToUserProfile'],
{
onError(err) {
alert(err);
@ -85,7 +85,7 @@ function Test() {
addToUserProfileMutation.mutate({
profileId: 'cl9efyn9p004ww3u42mjgl1vn',
token: '24bafa6fef803f447d7f2e229b14cb8ee43f0c22dffbe41ee1c1e5e6e870f117',
userId: 'cl9ehvpng0000w3ec2mpx0bdd',
// UserId: 'cl9ehvpng0000w3ec2mpx0bdd',
});
};

@ -6,6 +6,7 @@ import { offersRouter } from './offers/offers';
import { offersAnalysisRouter } from './offers/offers-analysis-router';
import { offersCommentsRouter } from './offers/offers-comments-router';
import { offersProfileRouter } from './offers/offers-profile-router';
import { offersUserProfileRouter } from './offers/offers-user-profile-router';
import { protectedExampleRouter } from './protected-example-router';
import { questionsAnswerCommentRouter } from './questions/questions-answer-comment-router';
import { questionsAnswerCommentUserRouter } from './questions/questions-answer-comment-user-router';
@ -64,7 +65,8 @@ export const appRouter = createRouter()
.merge('offers.', offersRouter)
.merge('offers.profile.', offersProfileRouter)
.merge('offers.analysis.', offersAnalysisRouter)
.merge('offers.comments.', offersCommentsRouter);
.merge('offers.comments.', offersCommentsRouter)
.merge('offers.user.profile.', offersUserProfileRouter);
// Export type definition of API
export type AppRouter = typeof appRouter;

@ -4,7 +4,6 @@ import { JobType } from '@prisma/client';
import * as trpc from '@trpc/server';
import {
addToProfileResponseMapper,
createOfferProfileResponseMapper,
profileDtoMapper,
} from '~/mappers/offers-mappers';
@ -1407,44 +1406,6 @@ export const offersProfileRouter = createRouter()
});
}
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Invalid token.',
});
},
})
.mutation('addToUserProfile', {
input: z.object({
profileId: z.string(),
token: z.string(),
userId: 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 updated = await ctx.prisma.offersProfile.update({
data: {
user: {
connect: {
id: input.userId,
},
},
},
where: {
id: input.profileId,
},
});
return addToProfileResponseMapper(updated);
}
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Invalid token.',

@ -0,0 +1,47 @@
import { z } from 'zod';
import * as trpc from '@trpc/server';
import {
addToProfileResponseMapper,
} 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,
},
});
const profileEditToken = profile?.editToken;
if (profileEditToken === input.token) {
const userId = ctx.session.user.id
const updated = await ctx.prisma.offersProfile.update({
data: {
user: {
connect: {
id: userId,
},
},
},
where: {
id: input.profileId,
},
});
return addToProfileResponseMapper(updated);
}
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Invalid token.',
});
},
});
Loading…
Cancel
Save