Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/pages/api/comment/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { NextApiResponse } from 'next';
import { z } from 'zod';

import logger from '@/lib/logger';
import { prisma } from '@/prisma';
import { type CommentRefType, type CommentType } from '@/prisma/enums';
import { CommentRefType, CommentType } from '@/prisma/enums';
import { safeStringify } from '@/utils/safeStringify';

import { type NextApiRequestWithUser } from '@/features/auth/types';
Expand All @@ -21,6 +22,18 @@ type CreateCommentInput = {
type?: CommentType;
};

const createCommentSchema = z.object({
message: z.string().trim().min(1).max(280),
refId: z.string().min(1),
refType: z.nativeEnum(CommentRefType),
pocId: z.string().nullish(),
replyToId: z.string().nullish(),
submissionId: z.string().nullish(),
replyToUserId: z.string().nullish(),
isPinned: z.boolean().optional(),
type: z.nativeEnum(CommentType).optional(),
});

export async function createComment(
userId: string,
input: CreateCommentInput,
Expand Down Expand Up @@ -216,7 +229,15 @@ async function commentHandler(
);

try {
const result = await createComment(userId as string, req.body, {
const parsed = createCommentSchema.safeParse(req.body);
if (!parsed.success) {
logger.warn(
`[CommentCreateAPI] Invalid comment payload: ${safeStringify(parsed.error.flatten())}`,
);
return res.status(400).json({ message: 'Invalid comment payload.' });
}

const result = await createComment(userId as string, parsed.data, {
logPrefix: 'CommentCreateAPI',
});
return res.status(200).json(result);
Expand Down