2026-01-15 22:56:32 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-01-22 03:27:32 +00:00
|
|
|
"errors"
|
2026-01-15 22:56:32 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
2026-01-27 18:44:02 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
2026-01-15 22:56:32 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-04-10 15:38:05 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-01-15 22:56:32 +00:00
|
|
|
|
2026-01-22 03:27:32 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-01-15 22:56:32 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-14 15:53:16 +00:00
|
|
|
func DistrictCatchall(ctx context.Context) (*models.Organization, error) {
|
|
|
|
|
return models.Organizations.Query(
|
|
|
|
|
models.SelectWhere.Organizations.IsCatchall.EQ(true),
|
|
|
|
|
).One(ctx, db.PGInstance.BobDB)
|
|
|
|
|
}
|
2026-02-17 05:33:12 +00:00
|
|
|
func DistrictForLocation(ctx context.Context, lng float64, lat float64) (*models.Organization, error) {
|
|
|
|
|
organizations, err := models.Organizations.Query(
|
2026-01-15 22:56:32 +00:00
|
|
|
sm.Where(
|
2026-02-17 05:33:12 +00:00
|
|
|
psql.F("ST_Contains", psql.Raw("service_area_geometry"), psql.F("ST_SetSRID", psql.F("ST_MakePoint", psql.Arg(lng), psql.Arg(lat)), psql.Arg(4326))),
|
2026-01-15 22:56:32 +00:00
|
|
|
),
|
|
|
|
|
).All(ctx, db.PGInstance.BobDB)
|
2026-01-22 03:27:32 +00:00
|
|
|
|
2026-02-17 05:33:12 +00:00
|
|
|
log.Debug().Int("len", len(organizations)).Float64("lng", lng).Float64("lat", lat).Msg("Attempting district match")
|
2026-01-15 22:56:32 +00:00
|
|
|
if err != nil {
|
2026-02-17 05:33:12 +00:00
|
|
|
return nil, fmt.Errorf("failed to query organization: %w", err)
|
2026-01-15 22:56:32 +00:00
|
|
|
}
|
2026-02-17 05:33:12 +00:00
|
|
|
switch len(organizations) {
|
2026-01-15 22:56:32 +00:00
|
|
|
case 0:
|
2026-02-17 05:33:12 +00:00
|
|
|
return nil, nil
|
2026-01-15 22:56:32 +00:00
|
|
|
case 1:
|
2026-02-17 05:33:12 +00:00
|
|
|
org := organizations[0]
|
|
|
|
|
return org, nil
|
2026-01-15 22:56:32 +00:00
|
|
|
default:
|
2026-02-17 05:33:12 +00:00
|
|
|
return nil, errors.New("too many organizations")
|
2026-01-15 22:56:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-20 16:21:08 +00:00
|
|
|
func matchDistrict(ctx context.Context, location *types.Location, images []ImageUpload) (int32, error) {
|
2026-03-13 17:33:39 +00:00
|
|
|
var err error
|
|
|
|
|
var org *models.Organization
|
|
|
|
|
for _, image := range images {
|
|
|
|
|
if image.Exif == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if image.Exif.GPS == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
org, err = DistrictForLocation(ctx, image.Exif.GPS.Longitude, image.Exif.GPS.Latitude)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn().Err(err).Msg("Failed to get district for location")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if org != nil {
|
2026-04-20 16:21:08 +00:00
|
|
|
return org.ID, nil
|
2026-03-13 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-10 15:38:05 +00:00
|
|
|
if location != nil {
|
|
|
|
|
if location.Longitude == 0 || location.Latitude == 0 {
|
|
|
|
|
org, err = DistrictCatchall(ctx)
|
|
|
|
|
if err != nil {
|
2026-04-20 16:21:08 +00:00
|
|
|
return 0, fmt.Errorf("get catchall: %w", err)
|
2026-04-10 15:38:05 +00:00
|
|
|
}
|
|
|
|
|
log.Debug().Int32("id", org.ID).Msg("No location from images, no latlng for the report itself, using catchall")
|
2026-04-20 16:21:08 +00:00
|
|
|
return org.ID, nil
|
2026-04-10 15:38:05 +00:00
|
|
|
}
|
|
|
|
|
org, err = DistrictForLocation(ctx, location.Longitude, location.Latitude)
|
2026-03-14 15:53:16 +00:00
|
|
|
if err != nil {
|
2026-04-10 15:38:05 +00:00
|
|
|
log.Warn().Err(err).Msg("Failed to get district for location")
|
2026-04-20 16:21:08 +00:00
|
|
|
return 0, fmt.Errorf("Failed to get district for location: %w", err)
|
2026-03-14 15:53:16 +00:00
|
|
|
}
|
2026-03-13 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
if org == nil {
|
2026-03-14 15:53:16 +00:00
|
|
|
org, err = DistrictCatchall(ctx)
|
|
|
|
|
if err != nil {
|
2026-04-20 16:21:08 +00:00
|
|
|
return 0, fmt.Errorf("get catchall: %w", err)
|
2026-03-14 15:53:16 +00:00
|
|
|
}
|
2026-04-10 15:38:05 +00:00
|
|
|
log.Debug().Err(err).Int32("id", org.ID).Msg("No district match by report location, using catchall")
|
2026-04-20 16:21:08 +00:00
|
|
|
return org.ID, nil
|
2026-03-13 17:33:39 +00:00
|
|
|
}
|
2026-04-10 15:38:05 +00:00
|
|
|
log.Debug().Err(err).Int32("org_id", org.ID).Msg("Found district match by report location")
|
2026-04-20 16:21:08 +00:00
|
|
|
return org.ID, nil
|
2026-03-13 17:33:39 +00:00
|
|
|
}
|