Fix — "Date must be in yyyy-mm-dd format" on Add to Database
File: server/src/sql/insert4.ts
Status: ✅ Verified — reproduced locally by swapping to customer DB, fix confirmed working
Reported by: Customer (AAR CEE Sec-103 / AAR CEE Sec-113 Gurugram bills)
Symptom: Clicking "Add to Database" fails with TRPCError: Date must be in yyyy-mm-dd format even though the date shown in the form (2026-03-09) is already valid ISO format.
Root Cause
In insertRowsWithRelations, the payload's data.documents is an array (consistent with every other table in the payload — products, customers, etc. are all arrays). But the code destructures and immediately accesses it as a plain object:
// line 150 — BUG
const { documents, ...tablesData } = data;
// then accessed as object:
document_number: documents['document_number'], // → undefined
document_type: documents['document_type'], // → undefined
date: dateToPostgresFormat(documents['date']), // → dateToPostgresFormat(undefined)
dynamic_columns: { ...documents, documentUrl }, // → spreads array indices, not fields
documents['date'] on an array is undefined. dateToPostgresFormat(undefined) fails the yyyy-mm-dd regex, fails new Date(undefined) (returns Invalid Date), and throws.
The error message is misleading — the date itself was fine. The problem was that it never reached the date value.
Why It Worked Locally
The local database was Shanti's own data, where documents happened to be stored as a plain object. When the local database was replaced with the customer's (AAR CEE) database, the same error appeared locally — confirming it was a data shape difference, not an environment difference.
Fix
Unwrap the array at the point of destructuring:
// server/src/sql/insert4.ts — line 150
const { documents: documentsArr, ...tablesData } = data;
const documents = Array.isArray(documentsArr) ? documentsArr[0] : documentsArr;
This handles both shapes (array and plain object) safely. All downstream accesses (documents['date'], documents['document_number'], { ...documents }) then work correctly.
Fix — "Date must be in yyyy-mm-dd format" on Add to Database
File:
server/src/sql/insert4.tsStatus: ✅ Verified — reproduced locally by swapping to customer DB, fix confirmed working
Reported by: Customer (AAR CEE Sec-103 / AAR CEE Sec-113 Gurugram bills)
Symptom: Clicking "Add to Database" fails with
TRPCError: Date must be in yyyy-mm-dd formateven though the date shown in the form (2026-03-09) is already valid ISO format.Root Cause
In
insertRowsWithRelations, the payload'sdata.documentsis an array (consistent with every other table in the payload —products,customers, etc. are all arrays). But the code destructures and immediately accesses it as a plain object:documents['date']on an array isundefined.dateToPostgresFormat(undefined)fails the yyyy-mm-dd regex, failsnew Date(undefined)(returnsInvalid Date), and throws.The error message is misleading — the date itself was fine. The problem was that it never reached the date value.
Why It Worked Locally
The local database was Shanti's own data, where
documentshappened to be stored as a plain object. When the local database was replaced with the customer's (AAR CEE) database, the same error appeared locally — confirming it was a data shape difference, not an environment difference.Fix
Unwrap the array at the point of destructuring:
This handles both shapes (array and plain object) safely. All downstream accesses (
documents['date'],documents['document_number'],{ ...documents }) then work correctly.