diff --git a/apps/portal/src/pages/offers/test/createProfile.tsx b/apps/portal/src/pages/offers/test/createProfile.tsx index 5f625321..3901505f 100644 --- a/apps/portal/src/pages/offers/test/createProfile.tsx +++ b/apps/portal/src/pages/offers/test/createProfile.tsx @@ -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', }); }; diff --git a/apps/portal/src/server/router/index.ts b/apps/portal/src/server/router/index.ts index ac83712f..41a01d5d 100644 --- a/apps/portal/src/server/router/index.ts +++ b/apps/portal/src/server/router/index.ts @@ -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; diff --git a/apps/portal/src/server/router/offers/offers-profile-router.ts b/apps/portal/src/server/router/offers/offers-profile-router.ts index 403ce32b..71c84674 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -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.', diff --git a/apps/portal/src/server/router/offers/offers-user-profile-router.ts b/apps/portal/src/server/router/offers/offers-user-profile-router.ts new file mode 100644 index 00000000..9d25257a --- /dev/null +++ b/apps/portal/src/server/router/offers/offers-user-profile-router.ts @@ -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.', + }); + }, + }); \ No newline at end of file