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

100% Statements 18/18
100% Branches 16/16
100% Functions 3/3
100% Lines 15/15

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89      5x   1x             9x 9x 9x   9x   8x   8x   7x           7x   9x 1x           3x   9x                     1x                                                                            
import type { Submission } from "../../shared/schemas/submission";
import { isManuscript, MANUSCRIPT_TYPES, submissionSchema } from "../../shared/schemas/submission";
 
const schema = submissionSchema((issue) => issue);
 
const KINDS = {
    article: "Artículo",
    tfg: "TFG",
    tfm: "TFM",
} as const satisfies Record<Submission["kind"], string>;
 
export default defineEventHandler(async (event): Promise<void> => {
    const form = await readFormData(event).catch(() => new FormData());
    const upload = form.get("file");
    const file = upload instanceof File ? upload : undefined;
 
    if (form.get("honeypot")) return;
 
    const token = form.get("token");
 
    await assertChallenge(event, typeof token === "string" ? token : undefined);
 
    const { name, email, kind, title, summary } = assertForm(schema, {
        ...Object.fromEntries(form),
        file,
        consent: form.get("consent") === "true",
    });
 
    const manuscript = file && { name: file.name, bytes: Buffer.from(await file.arrayBuffer()) };
 
    if (manuscript && !isManuscript(manuscript.name, manuscript.bytes))
        throw createError({
            statusCode: 422,
            statusMessage: "Invalid form",
            data: [{ name: "file", message: "fileType" }],
        });
 
    const heading = `Tipo: ${KINDS[kind]}\nTítulo provisional: ${title || "-"}`;
 
    await sendMail(event, {
        to: useRuntimeConfig(event).public.submissionEmail,
        replyTo: email,
        subject: `Propuesta (${KINDS[kind]}): ${name}`,
        text: [heading, summary, `— ${name} <${email}>`].join("\n\n"),
        attachments: manuscript && [
            { filename: manuscript.name, content: manuscript.bytes.toString("base64") },
        ],
    });
});
 
defineRouteMeta({
    openAPI: {
        tags: ["Forms"],
        summary: "Send a publication proposal, optionally with its draft, to the editors",
        requestBody: {
            required: true,
            content: {
                "multipart/form-data": {
                    schema: {
                        type: "object",
                        required: ["name", "email", "kind", "summary", "consent", "token"],
                        properties: {
                            name: { type: "string" },
                            email: { type: "string", format: "email" },
                            kind: { type: "string", enum: ["article", "tfg", "tfm"] },
                            title: { type: "string" },
                            summary: { type: "string" },
                            file: {
                                type: "string",
                                format: "binary",
                                description: `One of ${MANUSCRIPT_TYPES.join(" ")}, at most 4 MB`,
                            },
                            consent: { type: "string", 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" },
        },
    },
});