Lookup district and show on report submission complete
This commit is contained in:
parent
d28e3e2ccc
commit
c5f6db0b73
5 changed files with 78 additions and 10 deletions
|
|
@ -7,13 +7,17 @@ import (
|
|||
"math/big"
|
||||
"strings"
|
||||
|
||||
"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/background"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/sql"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/stephenafamo/scan"
|
||||
)
|
||||
|
||||
type ErrorWithCode struct {
|
||||
|
|
@ -34,6 +38,31 @@ type SomeReport struct {
|
|||
tableName string
|
||||
}
|
||||
|
||||
func (sr SomeReport) districtID(ctx context.Context) *int32 {
|
||||
type _Row struct {
|
||||
OrganizationID int32
|
||||
}
|
||||
|
||||
from := sm.From("no-such-table")
|
||||
switch sr.tableName {
|
||||
case "nuisance":
|
||||
from = sm.From("publicreport.nuisance")
|
||||
case "pool":
|
||||
from = sm.From("publicreport.pool")
|
||||
default:
|
||||
log.Error().Str("table-name", sr.tableName).Msg("Programmer error, non-exhaustive switch statement in SomeReport.districtID")
|
||||
}
|
||||
row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select(
|
||||
from,
|
||||
sm.Columns("organization_id"),
|
||||
sm.Where(psql.Quote("public_id").EQ(psql.Arg(sr.reportID))),
|
||||
), scan.StructMapper[_Row]())
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to query for organization_id")
|
||||
return nil
|
||||
}
|
||||
return &row.OrganizationID
|
||||
}
|
||||
func (sr SomeReport) updateReporterEmail(ctx context.Context, email string) *ErrorWithCode {
|
||||
table := um.Table("so-such-table")
|
||||
switch sr.tableName {
|
||||
|
|
@ -89,6 +118,22 @@ func (sr SomeReport) updateReporterPhone(ctx context.Context, phone text.E164) *
|
|||
return nil
|
||||
}
|
||||
|
||||
func DistrictForReport(ctx context.Context, report_id string) (*models.Organization, error) {
|
||||
some_report, err := findSomeReport(ctx, report_id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to find report %s: %w", report_id, err)
|
||||
}
|
||||
org_id := some_report.districtID(ctx)
|
||||
if org_id == nil {
|
||||
return nil, nil
|
||||
}
|
||||
result, e := models.FindOrganization(ctx, db.PGInstance.BobDB, *org_id)
|
||||
if e != nil {
|
||||
return nil, fmt.Errorf("Failed to load organization %d: %w", org_id, e)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GenerateReportID creates a 12-character random string using only unambiguous
|
||||
// capital letters and numbers
|
||||
func GenerateReportID() (string, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue