import { BadRequestException } from '@nestjs/common'; import { faToEn } from '../modules/customer-auth/digits'; /** * PERS-03: the birth date both customer forms collect (legacy bulkCustomer * `birth_date`, singleCustomer `birth_day`). DateInput sends the Gregorian * 'YYYY-MM-DD' of the Jalali date picked; stored at UTC midnight, as the * import stores legacy's. undefined = not sent (leave as is), '' = cleared. * * Shared by the admin customer forms and the sales expert's «ویرایش مشتری» in * the app, so both read a date the same way. */ export function birthDateOrNull(raw: unknown): Date | null | undefined { if (raw === undefined) return undefined; if (raw === null || String(raw).trim() === '') return null; const m = /^(\d{4})-(\d{1,2})-(\d{1,2})/.exec(String(raw).trim()); const d = m ? new Date(Date.UTC(+m[1], +m[2] - 1, +m[3])) : null; if (!d || d.getUTCMonth() !== +m![2] - 1 || d.getUTCFullYear() < 1900 || d > new Date()) { throw new BadRequestException('تاریخ تولد معتبر نیست'); } return d; } /** * A national code as the operator typed it, digits normalised; empty -> null. * The column is optional and unique (2026-10-07): an absent code is NULL, * never the mobile or '' — two customers without one must not collide, and * the column must only ever hold a national code. */ export function nationalCodeOrNull(raw: unknown): string | null { return faToEn(String(raw ?? '').trim()) || null; }