All files / apps/huella-legal/layers/contact/server/api contact.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      3x     6x   6x   5x   4x   4x               1x                                                              
import type { ContactRequest } from "../../shared/types/ContactRequest";
import { contactSchema } from "../../shared/schemas/contact";
 
const schema = contactSchema((issue) => issue);
 
export default defineEventHandler(async (event): Promise<void> => {
    const body = (await readBody<Partial<ContactRequest> | null>(event)) ?? {};
 
    if (body.honeypot) return;
 
    await assertChallenge(event, body.token);
 
    const { name, email, message } = assertForm(schema, body);
 
    await sendMail(event, {
        to: useRuntimeConfig(event).mail.to,
        replyTo: email,
        subject: `Contacto: ${name}`,
        text: `${message}\n\n— ${name} <${email}>`,
    });
});
 
defineRouteMeta({
    openAPI: {
        tags: ["Forms"],
        summary: "Send a contact message to the editors",
        requestBody: {
            required: true,
            content: {
                "application/json": {
                    schema: {
                        type: "object",
                        required: ["name", "email", "message", "consent", "token"],
                        properties: {
                            name: { type: "string" },
                            email: { type: "string", format: "email" },
                            message: { type: "string" },
                            consent: { type: "boolean", const: true },
                            token: { type: "string", description: "A Turnstile token" },
                            honeypot: { type: "string", description: "Left empty" },
                        },
                    },
                },
            },
        },
        responses: {
            204: { description: "Sent, or silently dropped when the honeypot is filled" },
            403: { description: "The Turnstile challenge failed" },
            422: { description: "One `{ name, message }` per invalid field, under `data`" },
            502: { description: "Resend or Turnstile is unavailable" },
        },
    },
});