From 88e6a07c3642b44edf00692a8f94c39a3f5d9074 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Thu, 12 Nov 2026 19:44:50 +0100 Subject: [PATCH] feat(soci): fill in the birth date from the codice fiscale Typing a valid codice fiscale in Admit, Register or Edit sets the birth date it encodes. The code keeps only two digits of the year, so the century is the latest that doesn't put the birth after today. A date that already agrees with the code (e.g. one a century earlier, typed by hand) is kept, and the field stays editable. --- src/features/soci/codice-fiscale-birth.ts | 33 +++++++++++++++++++++++ src/features/soci/socio-form.tsx | 22 +++++++++++---- tests/soci.test.mjs | 20 ++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/features/soci/codice-fiscale-birth.ts diff --git a/src/features/soci/codice-fiscale-birth.ts b/src/features/soci/codice-fiscale-birth.ts new file mode 100644 index 0000000..f408213 --- /dev/null +++ b/src/features/soci/codice-fiscale-birth.ts @@ -0,0 +1,33 @@ +import { codiceFiscaleProblem, encodedBirth, matchesBirthDate, normalizeCodiceFiscale } from "./codice-fiscale.ts" + +const pad = (value: number) => String(value).padStart(2, "0") + +/** + * The birth date (`YYYY-MM-DD`) a valid codice fiscale encodes, or null. The code keeps only two digits of the year, so + * the century is the latest that doesn't put the birth after `today` (`YYYY-MM-DD`): on 2026-11-12, `80` is 1980 and + * `05` is 2005. 29 February falls back a century when the nearer year isn't a leap year. + */ +export function birthDateOf(code: string, today: string): string | null { + const normalized = normalizeCodiceFiscale(code) + if (codiceFiscaleProblem(normalized)) return null + const birth = encodedBirth(normalized) + if (!birth) return null + const century = Math.floor(Number(today.slice(0, 4)) / 100) * 100 + for (const year of [century + birth.yy, century - 100 + birth.yy]) { + const date = new Date(Date.UTC(year, birth.month - 1, birth.day)) + const iso = `${year}-${pad(birth.month)}-${pad(birth.day)}` + if (date.getUTCDate() === birth.day && iso <= today) return iso + } + return null +} + +/** + * The birth date to keep once the codice fiscale reads `code`: the one the code encodes, unless the code isn't valid yet + * or `birthDate` already agrees with it (a date a century earlier, typed by hand, stays). + */ +export function birthDateFor(code: string, birthDate: string, today: string) { + const encoded = birthDateOf(code, today) + if (!encoded) return birthDate + if (birthDate !== "" && matchesBirthDate(normalizeCodiceFiscale(code), birthDate)) return birthDate + return encoded +} diff --git a/src/features/soci/socio-form.tsx b/src/features/soci/socio-form.tsx index f696221..acb32c3 100644 --- a/src/features/soci/socio-form.tsx +++ b/src/features/soci/socio-form.tsx @@ -3,10 +3,11 @@ import type { z } from "zod" import { DateField, fieldControl, fieldHintId, FormField, InlineAlert } from "@/components/primitives" import { Input } from "@/components/ui/input" -import { formatDay } from "@/features/people/people-format" +import { formatDay, todayIso } from "@/features/people/people-format" import { cn } from "@/lib/utils" import { codiceFiscaleProblem, matchesBirthDate, normalizeCodiceFiscale } from "./codice-fiscale" +import { birthDateFor } from "./codice-fiscale-birth" import { formatMemberNumber, socioName } from "./soci-format" import type { SocioDetails, SocioRow } from "./soci.contract" @@ -140,7 +141,11 @@ export function DetailsFields({ id, draft, onChange, errors, required, register, !codiceFiscaleProblem(code) && !matchesBirthDate(code, draft.birthDate) - const text = (key: keyof DetailsDraft, label: string, props: { mono?: boolean; optional?: boolean } = {}) => ( + const text = ( + key: keyof DetailsDraft, + label: string, + props: { mono?: boolean; optional?: boolean; onChange?: (value: string) => void } = {} + ) => ( set(key)(event.target.value)} + onChange={(event) => (props.onChange ?? set(key))(event.target.value)} className={cn("h-9", props.mono && "font-mono uppercase", fieldControl)} /> @@ -176,7 +181,13 @@ export function DetailsFields({ id, draft, onChange, errors, required, register, className={cn("h-9", fieldControl)} /> - {text("codiceFiscale", "Codice fiscale", { mono: true, optional })} + {text("codiceFiscale", "Codice fiscale", { + mono: true, + optional, + // A valid code fills in the birth date it encodes, which stays editable. + onChange: (value) => + onChange({ ...draft, codiceFiscale: value, birthDate: birthDateFor(value, draft.birthDate, todayIso()) }), + })} {holders.active ? ( This codice fiscale belongs to socio {formatMemberNumber(holders.active.number)}, {socioName(holders.active)}, @@ -199,6 +210,7 @@ export function DetailsFields({ id, draft, onChange, errors, required, register, label="Birth date" htmlFor={`${id}-birthDate`} optional={optional} + hint="Filled in from the codice fiscale." error={errors.birthDate ?? (dateMismatch ? "The birth date doesn't match the codice fiscale." : undefined)} > {text("birthPlace", "Birth place", { optional })} diff --git a/tests/soci.test.mjs b/tests/soci.test.mjs index b0da0c3..4fbaef6 100644 --- a/tests/soci.test.mjs +++ b/tests/soci.test.mjs @@ -1,6 +1,7 @@ import assert from "node:assert/strict" import { test } from "node:test" +import { birthDateFor, birthDateOf } from "../src/features/soci/codice-fiscale-birth.ts" import { checkCharacter, codiceFiscaleProblem, matchesBirthDate } from "../src/features/soci/codice-fiscale.ts" import { isMemberNumber } from "../src/features/soci/member-number.ts" import { @@ -42,6 +43,25 @@ void test("the codice fiscale is checked like the backend: format, omocodia, che assert.equal(codiceFiscaleSchema.parse(" rssmra80a01 h501u "), "RSSMRA80A01H501U") }) +void test("a valid codice fiscale gives the birth date, in the latest century not after today", () => { + const today = "2026-11-12" + const code = (first15) => `${first15}${checkCharacter(first15)}` + assert.equal(birthDateOf("rssmra80a01h501u", today), "1980-01-01") + assert.equal(birthDateOf(code("RSSMRA05M41F205"), today), "2005-08-01") + assert.equal(birthDateOf(code("RSSMRA26S30F205"), today), "1926-11-30") + assert.equal(birthDateOf(code("RSSMRA26S12F205"), today), "2026-11-12") + // 29 February 2001 doesn't exist, 1901 neither; 2000 does. + assert.equal(birthDateOf(code("RSSMRA01B29F205"), today), null) + assert.equal(birthDateOf(code("RSSMRA00B69F205"), today), "2000-02-29") + assert.equal(birthDateOf(code("RSSMRA80A01H50M"), today), "1980-01-01") + for (const invalid of ["", "RSSMRA80A01H501", "RSSMRA80A01H501X"]) assert.equal(birthDateOf(invalid, today), null) + + assert.equal(birthDateFor("RSSMRA80A01H501", "", today), "", "an incomplete code leaves the date") + assert.equal(birthDateFor("RSSMRA80A01H501U", "", today), "1980-01-01") + assert.equal(birthDateFor("RSSMRA80A01H501U", "1999-05-05", today), "1980-01-01", "a wrong date is replaced") + assert.equal(birthDateFor("RSSMRA80A01H501U", "1880-01-01", today), "1880-01-01", "a matching date is kept") +}) + void test("admitting needs every detail but the alias and the phone, normalized as the backend stores it", () => { const parsed = admitInputSchema.parse(admission) assert.equal(parsed.details.codiceFiscale, "RSSMRA80A01H501U")