Update the address when provided on a report
This commit is contained in:
parent
bac55774f8
commit
12aedaf543
6 changed files with 82 additions and 9 deletions
|
|
@ -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()
|
||||
|
|
|
|||
25
platform/publicreport/address.go
Normal file
25
platform/publicreport/address.go
Normal 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
|
||||
}
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue