mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-05-12 19:16:03 +08:00
[offers][feat] create protected router to store user profile endpoints
This commit is contained in:
@@ -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.',
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user