All files / apps/huella-legal/layers/forms/server/api replies.get.ts

100% Statements 10/10
100% Branches 8/8
100% Functions 2/2
100% Lines 9/9

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    1x   1x 2x     6x   6x   5x 3x         2x     1x                    
import type { ReplyPace } from "../../shared/types/ReplyPace";
 
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
 
const today = (): string =>
    new Intl.DateTimeFormat("en-CA", { timeZone: "Europe/Madrid" }).format(new Date());
 
export default defineEventHandler((event): ReplyPace => {
    const until = useRuntimeConfig(event).forms.slowRepliesUntil;
 
    if (!until) return {};
 
    if (!ISO_DATE.test(until) || Number.isNaN(Date.parse(until)))
        throw createError({
            statusCode: 500,
            statusMessage: "NUXT_FORMS_SLOW_REPLIES_UNTIL is not a YYYY-MM-DD date",
        });
 
    return today() <= until ? { slowUntil: until } : {};
});
 
defineRouteMeta({
    openAPI: {
        tags: ["Forms"],
        summary: "Say until when replies to Contacto and Publicar are slower than usual",
        responses: {
            200: { description: "OK, with `slowUntil` only while that day has not passed" },
            500: { description: "The configured date is not `YYYY-MM-DD`" },
        },
    },
});