stable: mapping v1.0 restored and verified
This commit is contained in:
@@ -191,6 +191,49 @@ export class GeocodingService {
|
||||
}
|
||||
}
|
||||
|
||||
async upsertPlace(data: Partial<PlaceSyria>) {
|
||||
try {
|
||||
if (!data.latitude || !data.longitude || (!data.name && !data.name_ar)) {
|
||||
throw new HttpException('Missing required fields for upsert', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
const lng = Number(data.longitude);
|
||||
const lat = Number(data.latitude);
|
||||
|
||||
// 1. Check for spatial match (within 10 meters)
|
||||
const existingQuery = `
|
||||
SELECT id FROM places_syria
|
||||
WHERE ST_DistanceSphere(location, ST_SetSRID(ST_MakePoint($1, $2), 4326)) < 10
|
||||
LIMIT 1
|
||||
`;
|
||||
const existing = await this.placesRepository.query(existingQuery, [lng, lat]);
|
||||
|
||||
if (existing && existing.length > 0) {
|
||||
const id = existing[0].id;
|
||||
this.logger.log(`Spatial match found for ${data.name || data.name_ar} (ID: ${id}). Updating...`);
|
||||
|
||||
await this.placesRepository.update(id, {
|
||||
name: data.name || undefined,
|
||||
name_ar: data.name_ar || undefined,
|
||||
address: data.address || undefined,
|
||||
category: data.category || undefined,
|
||||
description: data.description || undefined,
|
||||
city: data.city || undefined,
|
||||
neighbourhood: data.neighbourhood || undefined
|
||||
});
|
||||
|
||||
return { id, action: 'updated' };
|
||||
}
|
||||
|
||||
// 2. No matching place found, create new one
|
||||
const result = await this.addPlace(data);
|
||||
return { id: result.id, action: 'created' };
|
||||
} catch (error) {
|
||||
this.logger.error('Upsert failed:', error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getRecentPlaces(limit: number = 50) {
|
||||
try {
|
||||
return this.placesRepository.find({
|
||||
|
||||
Reference in New Issue
Block a user