Make lead creation and invalidation for public reports work

The only thing wrong at this point that I can tell is that address
aren't being correctly populated when I reverse geocode.
This commit is contained in:
Eli Ribble 2026-03-14 01:14:30 +00:00
parent 3e1b56a266
commit e2af49a323
No known key found for this signature in database
27 changed files with 821 additions and 365 deletions

View file

@ -3,27 +3,14 @@ package api
import (
"context"
"net/http"
"time"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"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"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform"
"github.com/Gleipnir-Technology/nidus-sync/platform/geom"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/rs/zerolog/log"
"github.com/stephenafamo/scan"
)
type createLead struct {
PoolLocations map[int]Location `json:"pool_locations"`
SignalIDs []int `json:"signal_ids"`
PoolLocations map[int]platform.Location `json:"pool_locations"`
SignalIDs []int `json:"signal_ids"`
}
type createdLead struct {
ID int32 `json:"id"`
@ -48,75 +35,21 @@ func postLeads(ctx context.Context, r *http.Request, user platform.User, req cre
return nil, nhttp.NewErrorStatus(http.StatusBadRequest, "can't make a lead with multiple signals yet")
}
signal_id := req.SignalIDs[0]
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
defer txn.Rollback(ctx)
if err != nil {
return nil, nhttp.NewError("start transaction: %w", err)
}
type _Row struct {
ID int32 `db:"site_id"`
}
site, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"pool.site_id AS site_id",
),
sm.From("signal_pool"),
sm.InnerJoin("pool").OnEQ(
psql.Quote("signal_pool", "pool_id"),
psql.Quote("pool", "id"),
),
sm.InnerJoin("site").On(
psql.Quote("pool", "site_id").EQ(psql.Quote("site", "id")),
),
sm.Where(psql.Quote("signal_pool", "signal_id").EQ(psql.Arg(signal_id))),
sm.Where(psql.Quote("site", "organization_id").EQ(psql.Arg(user.Organization.ID()))),
), scan.StructMapper[_Row]())
if err != nil {
if err.Error() == "sql: no rows in result set" {
return nil, nhttp.NewErrorStatus(http.StatusBadRequest, "Can't make a lead from signal %d: %w", signal_id, err)
}
return nil, nhttp.NewError("failed getting site: %w", err)
}
lead, err := models.Leads.Insert(&models.LeadSetter{
Created: omit.From(time.Now()),
Creator: omit.From(int32(user.ID)),
// ID
OrganizationID: omit.From(int32(user.Organization.ID())),
SiteID: omitnull.From(site.ID),
Type: omit.From(enums.LeadtypeGreenPool),
}).One(ctx, txn)
if err != nil {
return nil, nhttp.NewError("failed to create lead: %w", err)
}
_, err = psql.Update(
um.Table("signal"),
um.SetCol("addressed").ToArg(time.Now()),
um.SetCol("addressor").ToArg(user.ID),
um.Where(psql.Quote("id").EQ(psql.Arg(signal_id))),
).Exec(ctx, txn)
if err != nil {
return nil, nhttp.NewError("failed to update signal %d: %w", signal_id, err)
}
pool_location, ok := req.PoolLocations[signal_id]
var pool_location *platform.Location
l, ok := req.PoolLocations[signal_id]
if ok {
log.Info().Float64("lat", pool_location.Latitude).Float64("lng", pool_location.Longitude).Msg("got pool location")
geom_query := geom.PostgisPointQuery(pool_location.Longitude, pool_location.Latitude)
_, err = psql.Update(
um.Table("pool"),
um.SetCol("geometry").To(geom_query),
um.From("signal_pool"),
um.Where(psql.Quote("signal_pool", "pool_id").EQ(psql.Quote("pool", "id"))),
um.Where(psql.Quote("signal_pool", "signal_id").EQ(psql.Arg(signal_id))),
).Exec(ctx, txn)
if err != nil {
return nil, nhttp.NewError("failed to update pool through signal %d: %w", signal_id, err)
}
pool_location = &l
}
site_id, err := platform.SiteFromSignal(ctx, user, int32(signal_id))
if err != nil || site_id == nil {
return nil, nhttp.NewError("site from signal: %w", err)
}
lead_id, err := platform.LeadCreate(ctx, user, int32(signal_id), *site_id, pool_location)
if err != nil || lead_id == nil {
return nil, nhttp.NewError("lead create: %w", err)
}
txn.Commit(ctx)
return &createdLead{
ID: lead.ID,
ID: *lead_id,
}, nil
}

41
api/publicreport.go Normal file
View file

@ -0,0 +1,41 @@
package api
import (
"context"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/config"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform"
)
type formPublicreportLead struct {
ReportID string `json:"reportID"`
}
func postPublicreportLead(ctx context.Context, r *http.Request, user platform.User, req formPublicreportLead) (*createdLead, *nhttp.ErrorWithStatus) {
lead_id, err := platform.LeadCreateFromPublicreport(ctx, user, req.ReportID)
if err != nil {
return nil, nhttp.NewError("create lead: %w", err)
}
return &createdLead{
ID: *lead_id,
}, nil
}
type formPublicreportInvalid struct {
ReportID string `json:"reportID"`
}
type createdReport struct {
URI string `json:"uri"`
}
func postPublicreportInvalid(ctx context.Context, r *http.Request, user platform.User, req formPublicreportLead) (*createdReport, *nhttp.ErrorWithStatus) {
err := platform.PublicreportInvalid(ctx, user, req.ReportID)
if err != nil {
return nil, nhttp.NewError("create lead: %w", err)
}
return &createdReport{
URI: config.MakeURLNidus("/publicreport/%s", req.ReportID),
}, nil
}

View file

@ -22,7 +22,7 @@ type reviewTaskPool struct {
Created time.Time `json:"created"`
Creator platform.User `json:"creator"`
ID int32 `json:"id"`
Location Location `json:"location"`
Location types.Location `json:"location"`
Reviewed *time.Time `json:"addressed"`
Reviewer *platform.User `json:"addressor"`
}
@ -122,7 +122,7 @@ func listReviewTaskPool(ctx context.Context, r *http.Request, user platform.User
Created: row.Created,
Creator: *users_by_id[row.CreatorID],
ID: row.ID,
Location: Location{
Location: types.Location{
Latitude: row.Latitude,
Longitude: row.Longitude,
},

View file

@ -21,6 +21,8 @@ func AddRoutes(r chi.Router) {
r.Method("GET", "/leads", authenticatedHandlerJSON(listLead))
r.Method("POST", "/leads", authenticatedHandlerJSONPost(postLeads))
r.Method("GET", "/mosquito-source", auth.NewEnsureAuth(apiMosquitoSource))
r.Method("POST", "/publicreport/invalid", authenticatedHandlerJSONPost(postPublicreportInvalid))
r.Method("POST", "/publicreport/lead", authenticatedHandlerJSONPost(postPublicreportLead))
r.Method("POST", "/review/pool", authenticatedHandlerJSONPost(postReviewPool))
r.Method("GET", "/review-task/pool", authenticatedHandlerJSON(listReviewTaskPool))
r.Method("GET", "/service-request", auth.NewEnsureAuth(apiServiceRequest))

View file

@ -23,7 +23,7 @@ type signal struct {
Created time.Time `json:"created"`
Creator platform.User `json:"creator"`
ID int32 `json:"id"`
Location Location `json:"location"`
Location types.Location `json:"location"`
Species string `json:"species"`
Title string `json:"title"`
Type string `json:"type"`
@ -34,18 +34,18 @@ type contentListSignal struct {
func listSignal(ctx context.Context, r *http.Request, user platform.User, query queryParams) (*contentListSignal, *nhttp.ErrorWithStatus) {
type _Row struct {
Address types.Address `db:"address"`
Addressed *time.Time `db:"addressed"`
Addressor *int32 `db:"addressor"`
Created time.Time `db:"created"`
Creator int32 `db:"creator_id"`
ID int32 `db:"id"`
Latitude float64 `db:"latitude"`
Longitude float64 `db:"longitude"`
Location Location `db:"location"`
Species *string `db:"species"`
Title string `db:"title"`
Type string `db:"type"`
Address types.Address `db:"address"`
Addressed *time.Time `db:"addressed"`
Addressor *int32 `db:"addressor"`
Created time.Time `db:"created"`
Creator int32 `db:"creator_id"`
ID int32 `db:"id"`
Latitude float64 `db:"latitude"`
Longitude float64 `db:"longitude"`
Location types.Location `db:"location"`
Species *string `db:"species"`
Title string `db:"title"`
Type string `db:"type"`
}
limit := 20
if query.Limit != nil {
@ -118,7 +118,7 @@ func listSignal(ctx context.Context, r *http.Request, user platform.User, query
Created: row.Created,
Creator: *users_by_id[row.Creator],
ID: row.ID,
Location: Location{
Location: types.Location{
Latitude: row.Latitude,
Longitude: row.Longitude,
},

View file

@ -34,11 +34,6 @@ func NewBounds() Bounds {
}
}
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type NoteImagePayload struct {
UUID string `json:"uuid"`
Cell H3Cell `json:"cell"`