From 12aedaf54338da037abcf358ffdc66ae0d69fb73 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Fri, 10 Apr 2026 20:29:26 +0000 Subject: [PATCH] Update the address when provided on a report --- platform/publicreport.go | 18 +++++++++++++++++- platform/publicreport/address.go | 25 +++++++++++++++++++++++++ platform/publicreport/report.go | 20 +++++++++++++++++++- platform/types/address.go | 20 ++++++++++++++++++++ resource/publicreport.go | 6 +----- ts/components/MapLocator.vue | 2 -- 6 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 platform/publicreport/address.go diff --git a/platform/publicreport.go b/platform/publicreport.go index 77a18e75..f78d3d79 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -87,7 +87,7 @@ func PublicReportMessageCreate(ctx context.Context, user User, report_id, messag 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) if err != nil { 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 { 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 { err = reportUpdateLocation(ctx, txn, report.ID, *location) if err != nil { @@ -254,6 +260,16 @@ func reportFromID(ctx context.Context, report_id string) (*models.PublicreportRe } 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 { h3cell, _ := location.H3Cell() geom_query, _ := location.GeometryQuery() diff --git a/platform/publicreport/address.go b/platform/publicreport/address.go new file mode 100644 index 00000000..c480685f --- /dev/null +++ b/platform/publicreport/address.go @@ -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 +} diff --git a/platform/publicreport/report.go b/platform/publicreport/report.go index 813ff67e..ace9fc18 100644 --- a/platform/publicreport/report.go +++ b/platform/publicreport/report.go @@ -13,7 +13,7 @@ import ( //"github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/platform/types" //"github.com/google/uuid" - //"github.com/rs/zerolog/log" + "github.com/rs/zerolog/log" "github.com/stephenafamo/scan" ) @@ -31,9 +31,17 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ if err != nil { return nil, fmt.Errorf("get reports: %w", err) } + address_ids := make([]int32, 0) report_ids := make([]int32, len(rows)) for i, row := range rows { 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) if err != nil { @@ -54,6 +62,14 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ results := make([]*types.Report, len(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] if ok { row.Images = images @@ -112,6 +128,7 @@ func reportQuery() bob.BaseQuery[*dialect.SelectQuery] { return psql.Select( sm.Columns( "address_country AS \"address.country\"", + "address_id AS \"address.id\"", "address_gid AS \"address.gid\"", "address_locality AS \"address.locality\"", "address_number AS \"address.number\"", @@ -121,6 +138,7 @@ func reportQuery() bob.BaseQuery[*dialect.SelectQuery] { "address_street AS \"address.street\"", "created", "id", + "latlng_accuracy_value AS \"location.accuracy\"", "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\"", "organization_id", diff --git a/platform/types/address.go b/platform/types/address.go index 3d0b5f19..f07eb6d2 100644 --- a/platform/types/address.go +++ b/platform/types/address.go @@ -4,11 +4,13 @@ import ( "fmt" "github.com/Gleipnir-Technology/nidus-sync/db/enums" + "github.com/Gleipnir-Technology/nidus-sync/db/models" ) type Address struct { Country string `db:"country" json:"country"` GID string `db:"gid" json:"gid" schema:"gid"` + ID *int32 `db:"id" json:"-" schema:"-"` Locality string `db:"locality" json:"locality"` Location *Location `db:"location" json:"location" schema:"location"` Number string `db:"number" json:"number"` @@ -25,3 +27,21 @@ func (a Address) String() string { func (a Address) CountryEnum() enums.Countrytype { 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, + } +} diff --git a/resource/publicreport.go b/resource/publicreport.go index f8d1db9b..f918c7b0 100644 --- a/resource/publicreport.go +++ b/resource/publicreport.go @@ -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") } 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 { //report_setter.Latitude = omit.From(prf.Location.Latitude) //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, 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 { return nil, nhttp.NewError("update report: %w", err) } diff --git a/ts/components/MapLocator.vue b/ts/components/MapLocator.vue index aa414e6f..1c7f3cd3 100644 --- a/ts/components/MapLocator.vue +++ b/ts/components/MapLocator.vue @@ -408,7 +408,6 @@ const frameMarkers = () => { watch( () => props.modelValue, (newCamera) => { - console.log("New map camera", newCamera); if (map.value && newCamera) { map.value.panTo( { @@ -427,7 +426,6 @@ watch( watch( () => props.markers, () => { - console.log("New map markers", props.markers); updateMarkers(); }, { deep: true },