2026-03-19 17:41:38 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-03-19 19:17:00 +00:00
|
|
|
"strconv"
|
2026-03-19 17:41:38 +00:00
|
|
|
"time"
|
|
|
|
|
|
2026-03-19 19:00:44 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob"
|
2026-03-19 17:41:38 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
2026-03-19 19:00:44 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
2026-03-19 17:41:38 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-03-19 19:17:00 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/event"
|
2026-03-21 01:19:36 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
|
2026-03-19 19:00:44 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-03-19 17:41:38 +00:00
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/platform/geocode"
|
|
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/platform/geom"
|
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
|
"github.com/aarondl/opt/omitnull"
|
2026-03-20 18:03:32 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-03-19 19:00:44 +00:00
|
|
|
"github.com/stephenafamo/scan"
|
2026-03-19 17:41:38 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-19 19:00:44 +00:00
|
|
|
type Signal struct {
|
2026-04-12 17:01:30 +00:00
|
|
|
Address *types.Address `db:"address" json:"address"`
|
|
|
|
|
Addressed *time.Time `db:"addressed" json:"addressed"`
|
|
|
|
|
Addressor *int32 `db:"addressor" json:"addressor"`
|
|
|
|
|
Created time.Time `db:"created" json:"created"`
|
|
|
|
|
Creator int32 `db:"creator" json:"creator"`
|
|
|
|
|
ID int32 `db:"id" json:"id"`
|
|
|
|
|
Location types.Location `db:"location" json:"location"`
|
|
|
|
|
Pool *Pool `db:"pool" json:"pool"`
|
|
|
|
|
Report *types.PublicReport `db:"report" json:"report"`
|
|
|
|
|
Species *string `db:"species" json:"species"`
|
|
|
|
|
Type string `db:"type" json:"type"`
|
2026-03-19 19:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 17:41:38 +00:00
|
|
|
// Create a lead from the given signal and site
|
|
|
|
|
func SignalCreateFromPublicreport(ctx context.Context, user User, report_id string) (*int32, error) {
|
|
|
|
|
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
|
|
|
|
|
defer txn.Rollback(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("start transaction: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
report, err := models.PublicreportReports.Query(
|
|
|
|
|
models.SelectWhere.PublicreportReports.PublicID.EQ(report_id),
|
2026-03-22 01:22:44 +00:00
|
|
|
models.SelectWhere.PublicreportReports.OrganizationID.EQ(user.Organization.ID),
|
2026-03-19 17:41:38 +00:00
|
|
|
).One(ctx, txn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query report existence: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// At this point we have a report. We need to decide where to put it based on either the address or
|
|
|
|
|
// the location.
|
|
|
|
|
var site_id int32
|
2026-03-20 18:03:32 +00:00
|
|
|
var location string
|
2026-03-19 17:41:38 +00:00
|
|
|
if report.AddressID.IsValue() {
|
2026-03-20 18:03:32 +00:00
|
|
|
address_id := report.AddressID.MustGet()
|
|
|
|
|
address, err := models.FindAddress(ctx, txn, address_id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("find address: %w", err)
|
|
|
|
|
}
|
|
|
|
|
site, err := siteFromAddress(ctx, txn, user, address_id)
|
2026-03-19 17:41:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("site from address: %w", err)
|
|
|
|
|
}
|
|
|
|
|
site_id = site.ID
|
2026-04-14 15:43:49 +00:00
|
|
|
lat := address.LocationLatitude.GetOr(0.0)
|
|
|
|
|
lng := address.LocationLongitude.GetOr(0.0)
|
2026-03-20 18:03:32 +00:00
|
|
|
location = fmt.Sprintf("POINT(%f %f)", lng, lat)
|
2026-03-19 17:41:38 +00:00
|
|
|
} else if report.LocationLatitude.IsValue() && report.LocationLongitude.IsValue() {
|
2026-03-20 18:03:32 +00:00
|
|
|
lat := report.LocationLatitude.MustGet()
|
|
|
|
|
lng := report.LocationLongitude.MustGet()
|
2026-04-14 19:59:32 +00:00
|
|
|
site, err := siteFromLocation(ctx, txn, user, types.Location{
|
2026-03-20 18:03:32 +00:00
|
|
|
Latitude: lat,
|
|
|
|
|
Longitude: lng,
|
2026-03-19 17:41:38 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("site from address: %w", err)
|
|
|
|
|
}
|
|
|
|
|
site_id = site.ID
|
2026-03-20 18:03:32 +00:00
|
|
|
location = fmt.Sprintf("POINT(%f %f)", lng, lat)
|
2026-03-19 17:41:38 +00:00
|
|
|
} else if report.AddressRaw != "" {
|
|
|
|
|
// At this point we don't have an address, and we don't have GPS
|
|
|
|
|
// We'll try geocoding and creating an address from that.
|
|
|
|
|
site, err := siteFromAddressRaw(ctx, txn, user, report.AddressRaw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("site from address: %w", err)
|
|
|
|
|
}
|
2026-03-20 18:03:32 +00:00
|
|
|
address, err := models.FindAddress(ctx, txn, site.AddressID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("find address from raw: %w", err)
|
|
|
|
|
}
|
2026-03-19 17:41:38 +00:00
|
|
|
site_id = site.ID
|
2026-04-14 15:43:49 +00:00
|
|
|
lat := address.LocationLatitude.GetOr(0.0)
|
|
|
|
|
lng := address.LocationLongitude.GetOr(0.0)
|
2026-03-20 18:03:32 +00:00
|
|
|
location = fmt.Sprintf("POINT(%f %f)", lng, lat)
|
2026-03-19 17:41:38 +00:00
|
|
|
} else {
|
|
|
|
|
// We have no structured address, no GPS, no unstructued address.
|
|
|
|
|
// There's really nothing we can make this lead from and have it be meaningful
|
2026-03-20 18:03:32 +00:00
|
|
|
return nil, errors.New("Refusing to create a signal with no location data.")
|
2026-03-19 17:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var signal_type enums.Signaltype
|
|
|
|
|
switch report.ReportType {
|
|
|
|
|
case enums.PublicreportReporttypeNuisance:
|
|
|
|
|
signal_type = enums.SignaltypePublicreportNuisance
|
|
|
|
|
case enums.PublicreportReporttypeWater:
|
|
|
|
|
signal_type = enums.SignaltypePublicreportWater
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("Unrecognized report type %s", string(report.ReportType))
|
|
|
|
|
}
|
2026-03-20 18:03:32 +00:00
|
|
|
log.Debug().Str("location", location).Msg("inserting signal")
|
2026-03-19 17:41:38 +00:00
|
|
|
signal, err := models.Signals.Insert(&models.SignalSetter{
|
2026-03-21 01:19:36 +00:00
|
|
|
Addressed: omitnull.FromPtr[time.Time](nil),
|
|
|
|
|
Addressor: omitnull.FromPtr[int32](nil),
|
|
|
|
|
Created: omit.From(time.Now()),
|
|
|
|
|
Creator: omit.From(int32(user.ID)),
|
|
|
|
|
FeaturePoolFeatureID: omitnull.FromPtr[int32](nil),
|
2026-03-19 17:41:38 +00:00
|
|
|
// ID
|
2026-03-22 01:22:44 +00:00
|
|
|
OrganizationID: omit.From(int32(user.Organization.ID)),
|
2026-03-20 18:03:32 +00:00
|
|
|
Location: omit.From(location),
|
2026-03-21 01:19:36 +00:00
|
|
|
ReportID: omitnull.From(report.ID),
|
2026-03-19 17:41:38 +00:00
|
|
|
Species: omitnull.FromPtr[enums.Mosquitospecies](nil),
|
|
|
|
|
SiteID: omitnull.From(site_id),
|
|
|
|
|
Type: omit.From[enums.Signaltype](signal_type),
|
|
|
|
|
}).One(ctx, txn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("create signal: %w", err)
|
|
|
|
|
}
|
|
|
|
|
_, err = psql.Update(
|
|
|
|
|
um.Table(psql.Quote("publicreport", "report")),
|
|
|
|
|
um.SetCol("reviewed").ToArg(time.Now()),
|
|
|
|
|
um.SetCol("reviewer_id").ToArg(user.ID),
|
|
|
|
|
um.SetCol("status").ToArg(enums.PublicreportReportstatustypeReviewed),
|
|
|
|
|
um.Where(psql.Quote("public_id").EQ(psql.Arg(report_id))),
|
|
|
|
|
).Exec(ctx, txn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to update report %d: %w", report_id, err)
|
|
|
|
|
}
|
2026-03-22 01:22:44 +00:00
|
|
|
event.Created(event.TypeSignal, user.Organization.ID, strconv.Itoa(int(signal.ID)))
|
2026-03-19 17:41:38 +00:00
|
|
|
txn.Commit(ctx)
|
|
|
|
|
|
|
|
|
|
return &signal.ID, nil
|
|
|
|
|
}
|
2026-03-19 19:00:44 +00:00
|
|
|
|
2026-03-21 01:19:36 +00:00
|
|
|
func SignalList(ctx context.Context, user User, limit int) ([]*Signal, error) {
|
2026-03-22 01:22:44 +00:00
|
|
|
org_id := user.Organization.ID
|
2026-03-19 19:00:44 +00:00
|
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
|
|
|
|
sm.Columns(
|
|
|
|
|
"signal.addressed AS addressed",
|
|
|
|
|
"signal.addressor AS addressor",
|
|
|
|
|
"signal.created AS created",
|
|
|
|
|
"signal.creator AS creator",
|
|
|
|
|
"signal.id AS id",
|
2026-03-21 01:19:36 +00:00
|
|
|
"COALESCE(signal.feature_pool_feature_id, 0) AS \"pool.id\"",
|
|
|
|
|
"COALESCE(signal.report_id, 0) AS \"report.id\"",
|
2026-03-19 19:00:44 +00:00
|
|
|
"signal.species AS species",
|
|
|
|
|
"signal.type_ AS type",
|
2026-03-23 15:53:31 -07:00
|
|
|
"COALESCE(address.country, 'usa') AS \"address.country\"",
|
2026-03-23 15:47:48 -07:00
|
|
|
"COALESCE(address.locality, '') AS \"address.locality\"",
|
|
|
|
|
"COALESCE(address.number_, '') AS \"address.number\"",
|
|
|
|
|
"COALESCE(address.postal_code, '') AS \"address.postal_code\"",
|
|
|
|
|
"COALESCE(address.region, '') AS \"address.region\"",
|
|
|
|
|
"COALESCE(address.street, '') AS \"address.street\"",
|
|
|
|
|
"COALESCE(address.unit, '') AS \"address.unit\"",
|
2026-03-21 01:19:36 +00:00
|
|
|
// This will work great, up until we add polygons to signal
|
|
|
|
|
"ST_Y(signal.location) AS \"location.latitude\"",
|
|
|
|
|
"ST_X(signal.location) AS \"location.longitude\"",
|
2026-03-19 19:00:44 +00:00
|
|
|
),
|
|
|
|
|
sm.From("signal"),
|
|
|
|
|
sm.LeftJoin("site").OnEQ(
|
|
|
|
|
psql.Quote("signal", "site_id"),
|
|
|
|
|
psql.Quote("site", "id"),
|
|
|
|
|
),
|
|
|
|
|
sm.LeftJoin("address").OnEQ(
|
|
|
|
|
psql.Quote("site", "address_id"),
|
|
|
|
|
psql.Quote("address", "id"),
|
|
|
|
|
),
|
2026-03-21 01:19:36 +00:00
|
|
|
sm.Where(psql.Quote("signal", "organization_id").EQ(psql.Arg(org_id))),
|
2026-03-19 19:00:44 +00:00
|
|
|
sm.Where(psql.Quote("signal", "addressed").IsNull()),
|
|
|
|
|
sm.Limit(limit),
|
2026-03-21 01:19:36 +00:00
|
|
|
), scan.StructMapper[*Signal]())
|
2026-03-20 18:03:32 +00:00
|
|
|
log.Debug().Int("len", len(rows)).Msg("got signals")
|
2026-03-19 19:00:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to get signals: %w", err)
|
|
|
|
|
}
|
2026-03-21 01:19:36 +00:00
|
|
|
report_ids := make([]int32, 0)
|
|
|
|
|
pool_ids := make([]int32, 0)
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
if row.Report.ID != 0 {
|
|
|
|
|
report_ids = append(report_ids, row.Report.ID)
|
|
|
|
|
} else if row.Pool.ID != 0 {
|
|
|
|
|
pool_ids = append(pool_ids, row.Pool.ID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pools, err := poolList(ctx, org_id, pool_ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("getting pools by ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
reports, err := publicreport.Reports(ctx, org_id, report_ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("getting reports by ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
pool_map := make(map[int32]*Pool, len(pools))
|
|
|
|
|
for _, pool := range pools {
|
|
|
|
|
pool_map[pool.ID] = pool
|
|
|
|
|
log.Debug().Int32("pool", pool.ID).Msg("Added to map")
|
|
|
|
|
}
|
2026-04-12 17:01:30 +00:00
|
|
|
report_map := make(map[int32]*types.PublicReport, len(report_ids))
|
2026-03-21 01:19:36 +00:00
|
|
|
for _, report := range reports {
|
|
|
|
|
report_map[report.ID] = report
|
|
|
|
|
}
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
if row.Pool.ID != 0 {
|
|
|
|
|
p, ok := pool_map[row.Pool.ID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("failed to get pool %d for %d", row.Pool.ID, row.ID)
|
|
|
|
|
}
|
|
|
|
|
if p == nil {
|
|
|
|
|
return nil, fmt.Errorf("got nil pool from %d for %d", row.Pool.ID, row.ID)
|
|
|
|
|
}
|
|
|
|
|
row.Pool = p
|
|
|
|
|
row.Report = nil
|
|
|
|
|
} else if row.Report.ID != 0 {
|
2026-03-27 14:06:50 -07:00
|
|
|
report, ok := report_map[row.Report.ID]
|
2026-03-24 08:35:31 -07:00
|
|
|
if !ok {
|
2026-03-25 21:44:06 -07:00
|
|
|
return nil, fmt.Errorf("failed to get report %d for %d", row.Report.ID, row.ID)
|
|
|
|
|
}
|
|
|
|
|
if report == nil {
|
|
|
|
|
return nil, fmt.Errorf("got nil for report %d for %d", row.Report.ID, row.ID)
|
2026-03-24 08:35:31 -07:00
|
|
|
}
|
2026-03-25 21:44:06 -07:00
|
|
|
row.Pool = nil
|
2026-03-24 08:35:31 -07:00
|
|
|
row.Report = report
|
2026-03-25 21:44:06 -07:00
|
|
|
} else {
|
|
|
|
|
log.Debug().Int32("id", row.ID).Msg("has no publicrreport nor pool")
|
|
|
|
|
row.Pool = nil
|
|
|
|
|
row.Report = nil
|
2026-03-21 01:19:36 +00:00
|
|
|
}
|
2026-03-23 15:47:48 -07:00
|
|
|
if row.Address.Street == "" {
|
|
|
|
|
row.Address = nil
|
|
|
|
|
}
|
2026-03-21 01:19:36 +00:00
|
|
|
}
|
2026-03-19 19:00:44 +00:00
|
|
|
return rows, nil
|
|
|
|
|
}
|