Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/features/soci/codice-fiscale-birth.ts
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 17 additions & 5 deletions src/features/soci/socio-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -140,15 +141,19 @@ 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 } = {}
) => (
<FormField label={label} htmlFor={`${id}-${key}`} optional={props.optional} error={errors[key]}>
<Input
id={`${id}-${key}`}
value={draft[key]}
autoComplete="off"
aria-invalid={errors[key] ? true : undefined}
aria-describedby={errors[key] ? fieldHintId(`${id}-${key}`) : undefined}
onChange={(event) => set(key)(event.target.value)}
onChange={(event) => (props.onChange ?? set(key))(event.target.value)}
className={cn("h-9", props.mono && "font-mono uppercase", fieldControl)}
/>
</FormField>
Expand Down Expand Up @@ -176,7 +181,13 @@ export function DetailsFields({ id, draft, onChange, errors, required, register,
className={cn("h-9", fieldControl)}
/>
</FormField>
{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 ? (
<InlineAlert tone="danger">
This codice fiscale belongs to socio {formatMemberNumber(holders.active.number)}, {socioName(holders.active)},
Expand All @@ -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)}
>
<DateField
Expand All @@ -208,7 +220,7 @@ export function DetailsFields({ id, draft, onChange, errors, required, register,
pastYears={100}
futureYears={0}
invalid={!!errors.birthDate || dateMismatch}
describedBy={errors.birthDate || dateMismatch ? fieldHintId(`${id}-birthDate`) : undefined}
describedBy={fieldHintId(`${id}-birthDate`)}
/>
</FormField>
{text("birthPlace", "Birth place", { optional })}
Expand Down
20 changes: 20 additions & 0 deletions tests/soci.test.mjs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
Loading