Fix founded year/date handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-13 17:48:52 +00:00
parent 5feee9f4bc
commit b3ffb60ffb
4 changed files with 79 additions and 47 deletions

View File

@@ -101,18 +101,10 @@ export const companyValidationSchema = z.object({
company_type: z.enum(['manufacturer', 'designer', 'operator', 'property_owner']),
description: z.string().max(2000, 'Description must be less than 2000 characters').optional(),
person_type: z.enum(['company', 'individual', 'firm', 'organization']),
founded_year: z.string()
.optional()
.transform(val => {
if (!val || val.trim() === '') return undefined;
const num = Number(val);
return isNaN(num) ? undefined : num;
})
.refine(val => val === undefined || (typeof val === 'number' && val >= 1800 && val <= currentYear), {
message: `Founded year must be between 1800 and ${currentYear}`
}),
// Support both founded_date (preferred) and founded_year (legacy)
founded_date: z.string().optional(),
founded_date_precision: z.enum(['day', 'month', 'year']).optional(),
founded_year: z.number().int().min(1800).max(currentYear).optional().nullable(),
headquarters_location: z.string().max(200, 'Location must be less than 200 characters').optional(),
website_url: z.string().optional().refine((val) => {
if (!val || val === '') return true;

View File

@@ -8,6 +8,9 @@ export interface FieldChange {
changeType: 'added' | 'removed' | 'modified';
metadata?: {
isCreatingNewLocation?: boolean;
precision?: 'day' | 'month' | 'year';
oldPrecision?: 'day' | 'month' | 'year';
newPrecision?: 'day' | 'month' | 'year';
};
}
@@ -234,28 +237,32 @@ export async function detectChanges(
// Check for changes
if (!isEqual(oldValue, newValue)) {
if (oldEmpty && !newEmpty) {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'added',
});
} else if (newEmpty && !oldEmpty) {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'removed',
});
} else {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'modified',
});
const fieldChange: FieldChange = {
field: key,
oldValue,
newValue,
changeType: oldEmpty && !newEmpty ? 'added' :
newEmpty && !oldEmpty ? 'removed' :
'modified',
};
// Add precision metadata for date fields
if (key.endsWith('_date') && !key.endsWith('_precision')) {
const precisionKey = `${key}_precision`;
const newPrecision = itemData[precisionKey];
const oldPrecision = originalData[precisionKey];
if (newPrecision || oldPrecision) {
fieldChange.metadata = {
...fieldChange.metadata,
precision: newPrecision || oldPrecision,
oldPrecision,
newPrecision,
};
}
}
fieldChanges.push(fieldChange);
}
});
@@ -490,14 +497,23 @@ function formatEntityType(entityType: string): string {
/**
* Format field value for display
*/
export function formatFieldValue(value: any): string {
export function formatFieldValue(value: any, precision?: 'day' | 'month' | 'year'): string {
if (value === null || value === undefined) return 'None';
if (typeof value === 'boolean') return value ? 'Yes' : 'No';
// Handle dates
// Handle dates with precision support
if (value instanceof Date || (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value))) {
try {
const date = new Date(value);
// Apply precision if provided
if (precision === 'year') {
return date.getFullYear().toString();
} else if (precision === 'month') {
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long' });
}
// Default: full date
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
} catch {
return String(value);
@@ -525,6 +541,15 @@ export function formatFieldValue(value: any): string {
return entries.map(([k, v]) => `${k}: ${v}`).join(', ');
}
// Handle year-like numbers (prevent comma formatting for founded_year)
if (typeof value === 'number') {
const currentYear = new Date().getFullYear();
if (value >= 1800 && value <= currentYear + 10) {
return value.toString(); // Don't add commas for year values
}
return value.toLocaleString(); // Add commas for other numbers
}
// Handle URLs
if (typeof value === 'string' && value.startsWith('http')) {
try {