Update the address when provided on a report

This commit is contained in:
Eli Ribble 2026-04-10 20:29:26 +00:00
parent bac55774f8
commit 12aedaf543
No known key found for this signature in database
6 changed files with 82 additions and 9 deletions

View file

@ -87,7 +87,7 @@ func PublicReportMessageCreate(ctx context.Context, user User, report_id, messag
return nil, errors.New("no contact methods available") return nil, errors.New("no contact methods available")
} }
} }
func PublicReportUpdate(ctx context.Context, report_id string, report_setter models.PublicreportReportSetter, location *types.Location) (*types.Report, error) { func PublicReportUpdate(ctx context.Context, report_id string, report_setter models.PublicreportReportSetter, address *types.Address, location *types.Location) (*types.Report, error) {
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil) txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("create txn: %w", err) return nil, fmt.Errorf("create txn: %w", err)
@ -101,6 +101,12 @@ func PublicReportUpdate(ctx context.Context, report_id string, report_setter mod
if err != nil { if err != nil {
return nil, fmt.Errorf("update report: %w", err) return nil, fmt.Errorf("update report: %w", err)
} }
if address != nil {
err = reportUpdateAddress(ctx, txn, report, *address)
if err != nil {
return nil, fmt.Errorf("update address: %w", err)
}
}
if location != nil { if location != nil {
err = reportUpdateLocation(ctx, txn, report.ID, *location) err = reportUpdateLocation(ctx, txn, report.ID, *location)
if err != nil { if err != nil {
@ -254,6 +260,16 @@ func reportFromID(ctx context.Context, report_id string) (*models.PublicreportRe
} }
return report, nil return report, nil
} }
func reportUpdateAddress(ctx context.Context, txn bob.Executor, report *models.PublicreportReport, address types.Address) error {
err := report.Update(ctx, txn, &models.PublicreportReportSetter{
AddressGid: omit.From(address.GID),
AddressRaw: omit.From(address.Raw),
})
if err != nil {
return fmt.Errorf("update report: %w", err)
}
return nil
}
func reportUpdateLocation(ctx context.Context, txn bob.Executor, id int32, location types.Location) error { func reportUpdateLocation(ctx context.Context, txn bob.Executor, id int32, location types.Location) error {
h3cell, _ := location.H3Cell() h3cell, _ := location.H3Cell()
geom_query, _ := location.GeometryQuery() geom_query, _ := location.GeometryQuery()

View file

@ -0,0 +1,25 @@
package publicreport
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/bob"
//"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
)
func loadAddresses(ctx context.Context, txn bob.Executor, address_ids []int32) (results map[int32]types.Address, err error) {
rows, err := models.Addresses.Query(
models.SelectWhere.Addresses.ID.In(address_ids...),
).All(ctx, txn)
if err != nil {
return nil, fmt.Errorf("query addresses: %w", err)
}
results = make(map[int32]types.Address, len(rows))
for _, row := range rows {
results[row.ID] = types.AddressFromModel(row)
}
return results, nil
}

View file

@ -13,7 +13,7 @@ import (
//"github.com/Gleipnir-Technology/nidus-sync/db/models" //"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/types" "github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/google/uuid" //"github.com/google/uuid"
//"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/stephenafamo/scan" "github.com/stephenafamo/scan"
) )
@ -31,9 +31,17 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ
if err != nil { if err != nil {
return nil, fmt.Errorf("get reports: %w", err) return nil, fmt.Errorf("get reports: %w", err)
} }
address_ids := make([]int32, 0)
report_ids := make([]int32, len(rows)) report_ids := make([]int32, len(rows))
for i, row := range rows { for i, row := range rows {
report_ids[i] = row.ID report_ids[i] = row.ID
if row.Address.ID != nil {
address_ids = append(address_ids, *row.Address.ID)
}
}
addresses_by_id, err := loadAddresses(ctx, db.PGInstance.BobDB, address_ids)
if err != nil {
return nil, fmt.Errorf("addresses by ID: %w", err)
} }
images_by_id, err := loadImagesForReport(ctx, report_ids) images_by_id, err := loadImagesForReport(ctx, report_ids)
if err != nil { if err != nil {
@ -54,6 +62,14 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ
results := make([]*types.Report, len(rows)) results := make([]*types.Report, len(rows))
for i, row := range rows { for i, row := range rows {
if row.Address.ID != nil {
address, ok := addresses_by_id[*row.Address.ID]
if !ok {
log.Warn().Int32("address.id", *row.Address.ID).Msg("failed to find in addresses_by_id, which means our DB query is wrong")
} else {
row.Address = address
}
}
images, ok := images_by_id[row.ID] images, ok := images_by_id[row.ID]
if ok { if ok {
row.Images = images row.Images = images
@ -112,6 +128,7 @@ func reportQuery() bob.BaseQuery[*dialect.SelectQuery] {
return psql.Select( return psql.Select(
sm.Columns( sm.Columns(
"address_country AS \"address.country\"", "address_country AS \"address.country\"",
"address_id AS \"address.id\"",
"address_gid AS \"address.gid\"", "address_gid AS \"address.gid\"",
"address_locality AS \"address.locality\"", "address_locality AS \"address.locality\"",
"address_number AS \"address.number\"", "address_number AS \"address.number\"",
@ -121,6 +138,7 @@ func reportQuery() bob.BaseQuery[*dialect.SelectQuery] {
"address_street AS \"address.street\"", "address_street AS \"address.street\"",
"created", "created",
"id", "id",
"latlng_accuracy_value AS \"location.accuracy\"",
"COALESCE(ST_Y(location::geometry::geometry(point, 4326)), 0.0) AS \"location.latitude\"", "COALESCE(ST_Y(location::geometry::geometry(point, 4326)), 0.0) AS \"location.latitude\"",
"COALESCE(ST_X(location::geometry::geometry(point, 4326)), 0.0) AS \"location.longitude\"", "COALESCE(ST_X(location::geometry::geometry(point, 4326)), 0.0) AS \"location.longitude\"",
"organization_id", "organization_id",

View file

@ -4,11 +4,13 @@ import (
"fmt" "fmt"
"github.com/Gleipnir-Technology/nidus-sync/db/enums" "github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
) )
type Address struct { type Address struct {
Country string `db:"country" json:"country"` Country string `db:"country" json:"country"`
GID string `db:"gid" json:"gid" schema:"gid"` GID string `db:"gid" json:"gid" schema:"gid"`
ID *int32 `db:"id" json:"-" schema:"-"`
Locality string `db:"locality" json:"locality"` Locality string `db:"locality" json:"locality"`
Location *Location `db:"location" json:"location" schema:"location"` Location *Location `db:"location" json:"location" schema:"location"`
Number string `db:"number" json:"number"` Number string `db:"number" json:"number"`
@ -25,3 +27,21 @@ func (a Address) String() string {
func (a Address) CountryEnum() enums.Countrytype { func (a Address) CountryEnum() enums.Countrytype {
return enums.CountrytypeUsa return enums.CountrytypeUsa
} }
func AddressFromModel(m *models.Address) Address {
return Address{
Country: m.Country.String(),
GID: m.Gid,
ID: &m.ID,
Locality: m.Locality,
Location: &Location{
Latitude: m.LocationY.GetOr(0.0),
Longitude: m.LocationX.GetOr(0.0),
},
Number: m.Number,
PostalCode: m.PostalCode,
Raw: "",
Region: m.Region,
Street: m.Street,
Unit: m.Unit,
}
}

View file

@ -73,10 +73,6 @@ func (res *publicreportR) Update(ctx context.Context, r *http.Request, prf publi
return nil, nhttp.NewBadRequest("You must provide an ID") return nil, nhttp.NewBadRequest("You must provide an ID")
} }
report_setter := models.PublicreportReportSetter{} report_setter := models.PublicreportReportSetter{}
if prf.Address != nil {
report_setter.AddressGid = omit.From(prf.Address.GID)
report_setter.AddressRaw = omit.From(prf.Address.Raw)
}
if prf.Location != nil { if prf.Location != nil {
//report_setter.Latitude = omit.From(prf.Location.Latitude) //report_setter.Latitude = omit.From(prf.Location.Latitude)
//report_setter.Longitude = omit.From(prf.Location.Longitude) //report_setter.Longitude = omit.From(prf.Location.Longitude)
@ -95,7 +91,7 @@ func (res *publicreportR) Update(ctx context.Context, r *http.Request, prf publi
report_setter.ReporterPhone = omit.From(*prf.Reporter.Phone) report_setter.ReporterPhone = omit.From(*prf.Reporter.Phone)
} }
} }
report, err := platform.PublicReportUpdate(ctx, public_id, report_setter, prf.Location) report, err := platform.PublicReportUpdate(ctx, public_id, report_setter, prf.Address, prf.Location)
if err != nil { if err != nil {
return nil, nhttp.NewError("update report: %w", err) return nil, nhttp.NewError("update report: %w", err)
} }

View file

@ -408,7 +408,6 @@ const frameMarkers = () => {
watch( watch(
() => props.modelValue, () => props.modelValue,
(newCamera) => { (newCamera) => {
console.log("New map camera", newCamera);
if (map.value && newCamera) { if (map.value && newCamera) {
map.value.panTo( map.value.panTo(
{ {
@ -427,7 +426,6 @@ watch(
watch( watch(
() => props.markers, () => props.markers,
() => { () => {
console.log("New map markers", props.markers);
updateMarkers(); updateMarkers();
}, },
{ deep: true }, { deep: true },