[offers][fix] make offers api open to unauthenticated users

pull/390/head
Stuart Long Chay Boon 3 years ago
parent 699d563902
commit 2ad84aad91

@ -1,9 +1,9 @@
import { z } from 'zod'; import { z } from 'zod';
import * as trpc from '@trpc/server'; import * as trpc from '@trpc/server';
import { createProtectedRouter } from '../context'; import { createRouter } from '../context';
export const offersCommentsRouter = createProtectedRouter() export const offersCommentsRouter = createRouter()
.query('getComments', { .query('getComments', {
input: z.object({ input: z.object({
profileId: z.string(), profileId: z.string(),
@ -71,9 +71,19 @@ export const offersCommentsRouter = createProtectedRouter()
message: z.string(), message: z.string(),
profileId: z.string(), profileId: z.string(),
replyingToId: z.string().optional(), replyingToId: z.string().optional(),
userId: z.string().optional(), token: z.string().optional(),
userId: z.string().optional()
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const profile = await ctx.prisma.offersProfile.findFirst({
where: {
id: input.profileId,
},
});
const profileEditToken = profile?.editToken;
if (input.token === profileEditToken || input.userId) {
const createdReply = await ctx.prisma.offersReply.create({ const createdReply = await ctx.prisma.offersReply.create({
data: { data: {
message: input.message, message: input.message,
@ -135,6 +145,13 @@ export const offersCommentsRouter = createProtectedRouter()
} }
return result; return result;
}
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Missing userId or wrong token.',
});
}, },
}) })
.mutation('update', { .mutation('update', {

Loading…
Cancel
Save