I added some DB schema to track logos and to relate reports to organizations. I reworked how GPS data comes from EXIF data on images because it wasn't working for JPEGs. I might have broken PNGs in the process. Also made the config options for domain names more standardized.
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/stephenafamo/bob/dialect/psql"
|
|
"github.com/stephenafamo/bob/dialect/psql/sm"
|
|
)
|
|
|
|
func DistrictForLocation(ctx context.Context, lng float64, lat float64) (*models.ImportDistrict, *models.Organization, error) {
|
|
districts, err := models.ImportDistricts.Query(
|
|
sm.Where(
|
|
psql.F("ST_Contains", psql.Raw("geom_4326"), psql.F("ST_SetSRID", psql.F("ST_MakePoint", psql.Arg(lng), psql.Arg(lat)), psql.Arg(4326))),
|
|
),
|
|
).All(ctx, db.PGInstance.BobDB)
|
|
|
|
log.Debug().Int("len", len(districts)).Float64("lng", lng).Float64("lat", lat).Msg("Attempting district match")
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to query district: %w", err)
|
|
}
|
|
switch len(districts) {
|
|
case 0:
|
|
return nil, nil, nil
|
|
case 1:
|
|
district := districts[0]
|
|
organizations, err := models.Organizations.Query(
|
|
sm.Where(
|
|
models.Organizations.Columns.ImportDistrictGid.EQ(psql.Arg(district.Gid)),
|
|
),
|
|
).All(ctx, db.PGInstance.BobDB)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to query organization: %w", err)
|
|
}
|
|
switch len(organizations) {
|
|
case 0:
|
|
return nil, nil, nil
|
|
case 1:
|
|
return district, organizations[0], nil
|
|
default:
|
|
return nil, nil, errors.New("too many organizations")
|
|
}
|
|
default:
|
|
return nil, nil, errors.New("too many districts")
|
|
}
|
|
}
|