This means pushing the types into the common types module, which required a refactor of a bunch of other libraries.
119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
package publicreport
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Gleipnir-Technology/bob"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
|
//"github.com/Gleipnir-Technology/nidus-sync/config"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
//"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/stephenafamo/scan"
|
|
)
|
|
|
|
func ReportsForOrganization(ctx context.Context, org_id int32) ([]*types.Report, error) {
|
|
query := reportQuery(org_id)
|
|
query.Apply(
|
|
sm.Where(psql.Quote("publicreport", "report", "reviewed").IsNull()),
|
|
)
|
|
return reportQueryToRows(ctx, org_id, query)
|
|
}
|
|
func reportQueryToRows(ctx context.Context, org_id int32, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.Report, error) {
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[types.Report]())
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get reports: %w", err)
|
|
}
|
|
report_ids := make([]int32, len(rows))
|
|
for i, row := range rows {
|
|
report_ids[i] = row.ID
|
|
}
|
|
images_by_id, err := loadImagesForReport(ctx, org_id, report_ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("images for report: %w", err)
|
|
}
|
|
logs_by_report_id, err := logEntriesByReportID(ctx, report_ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("log entries for reports: %w", err)
|
|
}
|
|
nuisances_by_report_id, err := nuisancesByReportID(ctx, report_ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("nuisances: %w", err)
|
|
}
|
|
waters_by_report_id, err := watersByReportID(ctx, report_ids)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("waters: %w", err)
|
|
}
|
|
|
|
results := make([]*types.Report, len(rows))
|
|
for i, row := range rows {
|
|
images, ok := images_by_id[row.ID]
|
|
if ok {
|
|
row.Images = images
|
|
} else {
|
|
row.Images = []types.Image{}
|
|
}
|
|
row.Log = logs_by_report_id[row.ID]
|
|
row.Nuisance = nuisances_by_report_id[row.ID]
|
|
row.Water = waters_by_report_id[row.ID]
|
|
if row.Location.Latitude == 0.0 || row.Location.Longitude == 0.0 {
|
|
row.Location = nil
|
|
}
|
|
results[i] = &row
|
|
}
|
|
return results, nil
|
|
}
|
|
func Reports(ctx context.Context, org_id int32, ids []int32) ([]*types.Report, error) {
|
|
query := reportQuery(org_id)
|
|
query.Apply(
|
|
sm.Where(psql.Quote("publicreport", "report", "reviewed").IsNull()),
|
|
)
|
|
return reportQueryToRows(ctx, org_id, query)
|
|
}
|
|
func ReportsForOrganizationCount(ctx context.Context, org_id int32) (uint, error) {
|
|
type _Row struct {
|
|
Count uint `db:"count"`
|
|
}
|
|
row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select(
|
|
sm.Columns(
|
|
"COUNT(*) AS count",
|
|
),
|
|
sm.From("publicreport.report"),
|
|
sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))),
|
|
), scan.StructMapper[_Row]())
|
|
if err != nil {
|
|
return 0, fmt.Errorf("query count: %w", err)
|
|
}
|
|
return row.Count, nil
|
|
}
|
|
func reportQuery(org_id int32) bob.BaseQuery[*dialect.SelectQuery] {
|
|
return psql.Select(
|
|
sm.Columns(
|
|
"address_country AS \"address.country\"",
|
|
"address_locality AS \"address.locality\"",
|
|
"address_number AS \"address.number\"",
|
|
"address_postal_code AS \"address.postal_code\"",
|
|
"address_raw AS address_raw",
|
|
"address_region AS \"address.region\"",
|
|
"address_street AS \"address.street\"",
|
|
"created",
|
|
"id",
|
|
"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\"",
|
|
"public_id",
|
|
"report_type",
|
|
"reporter_email AS \"reporter.email\"",
|
|
"reporter_name AS \"reporter.name\"",
|
|
"reporter_phone AS \"reporter.phone\"",
|
|
"status",
|
|
),
|
|
sm.From("publicreport.report"),
|
|
sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))),
|
|
)
|
|
}
|