2026-04-14-1

This commit is contained in:
Hamza-Ayed
2026-04-14 03:03:35 +03:00
parent b36e197e09
commit 581eda1ea8
15 changed files with 102069 additions and 304 deletions
@@ -19,6 +19,28 @@ export class GeocodingInitService implements OnModuleInit {
await this.repo.query('CREATE EXTENSION IF NOT EXISTS postgis;');
await this.repo.query('CREATE EXTENSION IF NOT EXISTS pg_trgm;');
// Migration: Rename old admin_level columns to descriptive names if they exist
const tables = ['places_jordan', 'places_syria', 'places_egypt'];
const columnMapping = [
{ old: 'admin_level4_id', new: 'governorate_id' },
{ old: 'admin_level6_id', new: 'district_id' },
{ old: 'admin_level8_id', new: 'sub_district_id' },
{ old: 'admin_level10_id', new: 'neighborhood_id' }
];
for (const table of tables) {
for (const mapping of columnMapping) {
await this.repo.query(`
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '${table}' AND column_name = '${mapping.old}') THEN
ALTER TABLE ${table} RENAME COLUMN ${mapping.old} TO ${mapping.new};
END IF;
END $$;
`);
}
}
// Cleanup: Create a trigger to automatically update 'location' when lat/lng changes
await this.repo.query(`
CREATE OR REPLACE FUNCTION sync_place_location() RETURNS trigger AS $$
@@ -26,10 +48,13 @@ export class GeocodingInitService implements OnModuleInit {
IF NEW.latitude IS NOT NULL AND NEW.longitude IS NOT NULL THEN
NEW.location := ST_SetSRID(ST_MakePoint(CAST(NEW.longitude AS FLOAT), CAST(NEW.latitude AS FLOAT)), 4326);
NEW.admin_level4_id := (SELECT id FROM admin_boundaries WHERE admin_level = 4 AND ST_Contains(geom, NEW.location) LIMIT 1);
NEW.admin_level6_id := (SELECT id FROM admin_boundaries WHERE admin_level = 6 AND ST_Contains(geom, NEW.location) LIMIT 1);
NEW.admin_level8_id := (SELECT id FROM admin_boundaries WHERE admin_level = 8 AND ST_Contains(geom, NEW.location) LIMIT 1);
NEW.admin_level10_id := (SELECT id FROM admin_boundaries WHERE admin_level = 10 AND ST_Contains(geom, NEW.location) LIMIT 1);
-- Keep standard admin boundary logic for higher levels
NEW.governorate_id := (SELECT id FROM admin_boundaries WHERE admin_level = 4 AND ST_Contains(geom, NEW.location) LIMIT 1);
NEW.district_id := (SELECT id FROM admin_boundaries WHERE admin_level = 6 AND ST_Contains(geom, NEW.location) LIMIT 1);
NEW.sub_district_id := (SELECT id FROM admin_boundaries WHERE admin_level = 8 AND ST_Contains(geom, NEW.location) LIMIT 1);
-- ROOT CAUSE FIX: Use the new Voronoi polygons for neighborhoods!
NEW.neighborhood_id := (SELECT id FROM neighborhood_polygons ORDER BY NEW.location::geometry <-> geometry::geometry LIMIT 1);
END IF;
RETURN NEW;
END;