2026-03-18 15:36:20 +00:00
|
|
|
package publicreport
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/bob"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
2026-03-21 01:19:36 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
|
2026-03-18 15:36:20 +00:00
|
|
|
"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"
|
2026-04-10 20:29:26 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-03-18 15:36:20 +00:00
|
|
|
"github.com/stephenafamo/scan"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-21 01:19:36 +00:00
|
|
|
func ReportsForOrganization(ctx context.Context, org_id int32) ([]*types.Report, error) {
|
2026-04-08 23:37:00 +00:00
|
|
|
query := reportQuery()
|
2026-03-21 01:19:36 +00:00
|
|
|
query.Apply(
|
2026-04-08 23:37:00 +00:00
|
|
|
sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))),
|
2026-03-18 15:36:20 +00:00
|
|
|
sm.Where(psql.Quote("publicreport", "report", "reviewed").IsNull()),
|
2026-03-21 01:19:36 +00:00
|
|
|
)
|
2026-04-08 23:37:00 +00:00
|
|
|
return reportQueryToRows(ctx, query)
|
2026-03-21 01:19:36 +00:00
|
|
|
}
|
2026-04-08 23:37:00 +00:00
|
|
|
func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.Report, error) {
|
2026-03-21 01:19:36 +00:00
|
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[types.Report]())
|
2026-03-18 15:36:20 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("get reports: %w", err)
|
|
|
|
|
}
|
2026-04-10 20:29:26 +00:00
|
|
|
address_ids := make([]int32, 0)
|
2026-03-18 15:36:20 +00:00
|
|
|
report_ids := make([]int32, len(rows))
|
|
|
|
|
for i, row := range rows {
|
|
|
|
|
report_ids[i] = row.ID
|
2026-04-10 20:29:26 +00:00
|
|
|
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)
|
2026-03-18 15:36:20 +00:00
|
|
|
}
|
2026-04-08 23:37:00 +00:00
|
|
|
images_by_id, err := loadImagesForReport(ctx, report_ids)
|
2026-03-18 15:36:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("images for report: %w", err)
|
|
|
|
|
}
|
2026-03-18 18:56:51 +00:00
|
|
|
logs_by_report_id, err := logEntriesByReportID(ctx, report_ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("log entries for reports: %w", err)
|
|
|
|
|
}
|
2026-03-18 15:36:20 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 01:19:36 +00:00
|
|
|
results := make([]*types.Report, len(rows))
|
2026-03-18 18:56:51 +00:00
|
|
|
for i, row := range rows {
|
2026-04-10 20:29:26 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-18 15:36:20 +00:00
|
|
|
images, ok := images_by_id[row.ID]
|
|
|
|
|
if ok {
|
|
|
|
|
row.Images = images
|
|
|
|
|
} else {
|
|
|
|
|
row.Images = []types.Image{}
|
|
|
|
|
}
|
2026-03-18 18:56:51 +00:00
|
|
|
row.Log = logs_by_report_id[row.ID]
|
2026-03-18 15:36:20 +00:00
|
|
|
row.Nuisance = nuisances_by_report_id[row.ID]
|
|
|
|
|
row.Water = waters_by_report_id[row.ID]
|
2026-03-19 19:16:39 +00:00
|
|
|
if row.Location.Latitude == 0.0 || row.Location.Longitude == 0.0 {
|
|
|
|
|
row.Location = nil
|
|
|
|
|
}
|
2026-03-18 18:56:51 +00:00
|
|
|
results[i] = &row
|
2026-03-18 15:36:20 +00:00
|
|
|
}
|
2026-03-18 18:56:51 +00:00
|
|
|
return results, nil
|
2026-03-18 15:36:20 +00:00
|
|
|
}
|
2026-04-08 23:37:00 +00:00
|
|
|
func Report(ctx context.Context, public_id string) (*types.Report, error) {
|
|
|
|
|
query := reportQuery()
|
|
|
|
|
query.Apply(
|
|
|
|
|
sm.Where(psql.Quote("publicreport", "report", "public_id").EQ(psql.Arg(public_id))),
|
|
|
|
|
)
|
|
|
|
|
reports, err := reportQueryToRows(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query to rows: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if len(reports) != 1 {
|
|
|
|
|
return nil, fmt.Errorf("reports returned: %d", len(reports))
|
|
|
|
|
}
|
|
|
|
|
return reports[0], nil
|
|
|
|
|
}
|
2026-03-21 01:19:36 +00:00
|
|
|
func Reports(ctx context.Context, org_id int32, ids []int32) ([]*types.Report, error) {
|
2026-04-08 23:37:00 +00:00
|
|
|
query := reportQuery()
|
2026-03-21 01:19:36 +00:00
|
|
|
query.Apply(
|
2026-04-08 23:37:00 +00:00
|
|
|
sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))),
|
2026-03-24 08:24:36 -07:00
|
|
|
sm.Where(psql.Quote("publicreport", "report", "id").EQ(psql.Any(ids))),
|
2026-03-21 01:19:36 +00:00
|
|
|
)
|
2026-04-08 23:37:00 +00:00
|
|
|
return reportQueryToRows(ctx, query)
|
2026-03-21 01:19:36 +00:00
|
|
|
}
|
2026-03-18 15:36:20 +00:00
|
|
|
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
|
|
|
|
|
}
|
2026-04-08 23:37:00 +00:00
|
|
|
func reportQuery() bob.BaseQuery[*dialect.SelectQuery] {
|
2026-03-21 01:19:36 +00:00
|
|
|
return psql.Select(
|
|
|
|
|
sm.Columns(
|
|
|
|
|
"address_country AS \"address.country\"",
|
2026-04-10 20:29:26 +00:00
|
|
|
"address_id AS \"address.id\"",
|
2026-04-08 23:37:00 +00:00
|
|
|
"address_gid AS \"address.gid\"",
|
2026-03-21 01:19:36 +00:00
|
|
|
"address_locality AS \"address.locality\"",
|
|
|
|
|
"address_number AS \"address.number\"",
|
|
|
|
|
"address_postal_code AS \"address.postal_code\"",
|
2026-03-24 05:44:27 +00:00
|
|
|
"address_raw AS \"address.raw\"",
|
2026-03-21 01:19:36 +00:00
|
|
|
"address_region AS \"address.region\"",
|
|
|
|
|
"address_street AS \"address.street\"",
|
|
|
|
|
"created",
|
|
|
|
|
"id",
|
2026-04-10 20:29:26 +00:00
|
|
|
"latlng_accuracy_value AS \"location.accuracy\"",
|
2026-03-21 01:19:36 +00:00
|
|
|
"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\"",
|
2026-04-08 23:37:00 +00:00
|
|
|
"organization_id",
|
2026-03-21 01:19:36 +00:00
|
|
|
"public_id",
|
|
|
|
|
"report_type",
|
|
|
|
|
"reporter_email AS \"reporter.email\"",
|
|
|
|
|
"reporter_name AS \"reporter.name\"",
|
|
|
|
|
"reporter_phone AS \"reporter.phone\"",
|
|
|
|
|
"status",
|
|
|
|
|
),
|
|
|
|
|
sm.From("publicreport.report"),
|
|
|
|
|
)
|
|
|
|
|
}
|