Fix: Detect location changes and new entity creation

This commit is contained in:
gpt-engineer-app[bot]
2025-10-06 14:20:07 +00:00
parent 035f332912
commit 6919f396e6
2 changed files with 72 additions and 7 deletions

View File

@@ -6,6 +6,9 @@ export interface FieldChange {
oldValue: any;
newValue: any;
changeType: 'added' | 'removed' | 'modified';
metadata?: {
isCreatingNewLocation?: boolean;
};
}
export interface ImageChange {
@@ -127,17 +130,28 @@ export async function detectChanges(
const newValue = itemData[key];
// Handle location changes specially - compare objects not IDs
if (key === 'location') {
if (key === 'location' || key === 'location_id') {
// Skip location_id if we already have a location object
if (key === 'location_id' && itemData.location) {
return;
}
const oldLoc = originalData.location;
const newLoc = itemData.location;
// Check if new location entity is being created (old has location_id, new has location object)
const isCreatingNewLocation = originalData.location_id && newLoc && typeof newLoc === 'object' && !oldLoc;
// Only compare if we have location objects with actual data
if (newLoc && typeof newLoc === 'object' && oldLoc && typeof oldLoc === 'object') {
// Compare actual location data (city, state, country)
// Compare all location data including coordinates
const locChanged =
oldLoc.city !== newLoc.city ||
oldLoc.state_province !== newLoc.state_province ||
oldLoc.country !== newLoc.country;
oldLoc.country !== newLoc.country ||
oldLoc.postal_code !== newLoc.postal_code ||
Number(oldLoc.latitude) !== Number(newLoc.latitude) ||
Number(oldLoc.longitude) !== Number(newLoc.longitude);
if (locChanged) {
hasLocationChange = true;
@@ -148,6 +162,16 @@ export async function detectChanges(
changeType: 'modified',
});
}
} else if (isCreatingNewLocation) {
// New location entity is being created - mark as location change
hasLocationChange = true;
fieldChanges.push({
field: 'location',
oldValue: { location_id: originalData.location_id },
newValue: newLoc,
changeType: 'modified',
metadata: { isCreatingNewLocation: true },
});
}
return;
}