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

@@ -203,16 +203,47 @@ interface LocationDiffProps {
}
export function LocationDiff({ oldLocation, newLocation, compact = false }: LocationDiffProps) {
// Check if we're creating a new location entity
const isCreatingNewLocation = oldLocation?.location_id && !oldLocation?.city && newLocation?.city;
const formatLocation = (loc: any) => {
if (!loc) return 'None';
if (typeof loc === 'string') return loc;
// Handle location_id reference
if (loc.location_id && !loc.city) {
return `Location ID: ${loc.location_id.substring(0, 8)}...`;
}
if (typeof loc === 'object') {
const parts = [loc.city, loc.state_province, loc.country].filter(Boolean);
return parts.join(', ') || 'Unknown';
const parts = [];
if (loc.city) parts.push(loc.city);
if (loc.state_province) parts.push(loc.state_province);
if (loc.country && loc.country !== loc.state_province) parts.push(loc.country);
if (loc.postal_code) parts.push(loc.postal_code);
let locationStr = parts.join(', ') || 'Unknown';
// Add coordinates if available
if (loc.latitude && loc.longitude) {
const lat = Number(loc.latitude).toFixed(6);
const lng = Number(loc.longitude).toFixed(6);
locationStr += ` (${lat}°, ${lng}°)`;
}
return locationStr;
}
return String(loc);
};
// Check if only coordinates changed
const onlyCoordinatesChanged = oldLocation?.city === newLocation?.city &&
oldLocation?.state_province === newLocation?.state_province &&
oldLocation?.country === newLocation?.country &&
oldLocation?.postal_code === newLocation?.postal_code &&
(Number(oldLocation?.latitude) !== Number(newLocation?.latitude) ||
Number(oldLocation?.longitude) !== Number(newLocation?.longitude));
if (compact) {
const oldLoc = formatLocation(oldLocation);
const newLoc = formatLocation(newLocation);
@@ -235,14 +266,24 @@ export function LocationDiff({ oldLocation, newLocation, compact = false }: Loca
return (
<Badge variant="outline" className="text-amber-600 dark:text-amber-400">
Location: {oldLoc} {newLoc}
Location{isCreatingNewLocation && ' (New Entity)'}
{onlyCoordinatesChanged && ' (GPS Refined)'}
: {oldLoc} {newLoc}
</Badge>
);
}
return (
<div className="flex flex-col gap-1 p-2 rounded-md bg-muted/50">
<div className="text-sm font-medium">Location</div>
<div className="text-sm font-medium">
Location
{isCreatingNewLocation && (
<Badge variant="secondary" className="ml-2">New location entity</Badge>
)}
{onlyCoordinatesChanged && (
<Badge variant="outline" className="ml-2">GPS coordinates refined</Badge>
)}
</div>
<div className="flex items-center gap-2 text-sm">
{oldLocation && (

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;
}