All files / apps/huella-legal/layers/comments/server/api comments.post.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72        5x     7x   7x   6x   5x   5x     1x                                                                                                          
import { newCommentSchema } from "../../shared/schemas/comment";
import type { CommentRequest } from "../../shared/types/CommentRequest";
import { createComment } from "../adapters/wordpress";
 
const schema = newCommentSchema((issue) => issue);
 
export default defineEventHandler(async (event): Promise<void> => {
    const body = (await readBody<Partial<CommentRequest> | null>(event)) ?? {};
 
    if (body.honeypot) return;
 
    await assertChallenge(event, body.token);
 
    const comment = assertForm(schema, body);
 
    await createComment(event, comment);
});
 
defineRouteMeta({
    openAPI: {
        tags: ["Forms"],
        summary: "Submit a reader's comment on an article for moderation",
        requestBody: {
            required: true,
            content: {
                "application/json": {
                    schema: {
                        type: "object",
                        required: [
                            "post",
                            "parent",
                            "name",
                            "email",
                            "content",
                            "consent",
                            "token",
                        ],
                        properties: {
                            post: { type: "string", description: "The article's id" },
                            parent: {
                                type: "string",
                                description: "The comment it replies to, or `0`",
                            },
                            name: { type: "string", description: "Up to 245 characters" },
                            email: {
                                type: "string",
                                format: "email",
                                description: "Up to 100 characters",
                            },
                            content: { type: "string", description: "Up to 5,000 characters" },
                            consent: { type: "boolean", const: true },
                            token: { type: "string", description: "A Turnstile token" },
                            honeypot: { type: "string", description: "Left empty" },
                        },
                    },
                },
            },
        },
        responses: {
            204: {
                description: "Held for moderation, or silently dropped when the honeypot is filled",
            },
            403: { description: "The Turnstile challenge failed" },
            409: { description: "The same comment was already submitted" },
            410: { description: "The article no longer takes comments" },
            422: { description: "One `{ name, message }` per invalid field, under `data`" },
            429: { description: "Too many comments in a short time" },
            502: { description: "WordPress or Turnstile is unavailable" },
        },
    },
});