All files / apps/huella-legal/layers/interest/app/schemas interestAdvancedForm.ts

88.57% Statements 31/35
84.61% Branches 11/13
84.61% Functions 22/26
87.87% Lines 29/33

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125            3x     3x   174x                       330x   3x 55x   3x 55x   3x     19x       29x 2x                       19x               5x 2x   1x             29x 3x               29x   5x             29x 3x               29x   1x               29x 29x   29x   2x       11x                  
import * as v from "valibot";
import { calendarDate, parseDecimal } from "../utils/interest";
 
type InterestAdvancedFormIssue =
    "principal" | "customPercent" | "from" | "fromInvalid" | "to" | "toInvalid" | "order";
 
const CONCEPT_MAX_LENGTH = 120;
 
// An emptied number input binds "", which the date check below words, so a part never raises its own
const datePart = v.pipe(
    v.unknown(),
    v.transform((value) => (typeof value === "number" ? value : Number.NaN)),
);
 
interface DateParts {
    fromDay: number;
    fromMonth: number;
    fromYear: number;
    toDay: number;
    toMonth: number;
    toYear: number;
}
 
const isComplete = (...parts: number[]): boolean => parts.every((part) => !Number.isNaN(part));
 
const fromDate = (parts: Pick<DateParts, "fromDay" | "fromMonth" | "fromYear">) =>
    calendarDate(parts.fromDay, parts.fromMonth, parts.fromYear);
 
const toDate = (parts: Pick<DateParts, "toDay" | "toMonth" | "toYear">) =>
    calendarDate(parts.toDay, parts.toMonth, parts.toYear);
 
export const interestAdvancedFormSchema = (
    message: (issue: InterestAdvancedFormIssue) => string,
) => {
    const common = {
        concept: v.pipe(v.optional(v.string(), ""), v.trim(), v.maxLength(CONCEPT_MAX_LENGTH)),
        principal: v.pipe(
            v.string(() => message("principal")),
            v.transform((text) => parseDecimal(text, { grouping: true })),
            v.number(() => message("principal")),
            v.minValue(0.01, () => message("principal")),
        ),
        fromDay: datePart,
        fromMonth: datePart,
        fromYear: datePart,
        toDay: datePart,
        toMonth: datePart,
        toYear: datePart,
    };
 
    // Each date reports once, on its day field, so the summary links to the first of its three inputs
    return v.pipe(
        v.variant("rate", [
            v.object({ ...common, rate: v.picklist(["legal", "judicial"]) }),
            v.object({
                ...common,
                rate: v.literal("custom"),
                customPercent: v.pipe(
                    v.string(() => message("customPercent")),
                    v.transform((text) => parseDecimal(text, { grouping: false })),
                    v.number(() => message("customPercent")),
                    v.gtValue(0, () => message("customPercent")),
                    v.maxValue(100, () => message("customPercent")),
                ),
            }),
        ]),
        v.forward(
            v.partialCheck(
                [["fromDay"], ["fromMonth"], ["fromYear"]],
                ({ fromDay, fromMonth, fromYear }) => isComplete(fromDay, fromMonth, fromYear),
                () => message("from"),
            ),
            ["fromDay"],
        ),
        v.forward(
            v.partialCheck(
                [["fromDay"], ["fromMonth"], ["fromYear"]],
                (parts) =>
                    !isComplete(parts.fromDay, parts.fromMonth, parts.fromYear) ||
                    fromDate(parts) !== undefined,
                () => message("fromInvalid"),
            ),
            ["fromDay"],
        ),
        v.forward(
            v.partialCheck(
                [["toDay"], ["toMonth"], ["toYear"]],
                ({ toDay, toMonth, toYear }) => isComplete(toDay, toMonth, toYear),
                () => message("to"),
            ),
            ["toDay"],
        ),
        v.forward(
            v.partialCheck(
                [["toDay"], ["toMonth"], ["toYear"]],
                (parts) =>
                    !isComplete(parts.toDay, parts.toMonth, parts.toYear) ||
                    toDate(parts) !== undefined,
                () => message("toInvalid"),
            ),
            ["toDay"],
        ),
        v.forward(
            v.partialCheck(
                [["fromDay"], ["fromMonth"], ["fromYear"], ["toDay"], ["toMonth"], ["toYear"]],
                (parts) => {
                    const from = fromDate(parts);
                    const to = toDate(parts);
 
                    return from === undefined || to === undefined || to >= from;
                },
                () => message("order"),
            ),
            ["toDay"],
        ),
        v.transform(({ fromDay, fromMonth, fromYear, toDay, toMonth, toYear, ...rest }) => ({
            ...rest,
            from: calendarDate(fromDay, fromMonth, fromYear) ?? "",
            to: calendarDate(toDay, toMonth, toYear) ?? "",
        })),
    );
};
 
export type InterestAdvancedForm = v.InferOutput<ReturnType<typeof interestAdvancedFormSchema>>;