2026-04-03 22:04:22 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-05-07 10:39:17 +00:00
|
|
|
modelpublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
|
|
|
|
|
//tablepublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
|
|
|
|
|
//querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport"
|
2026-04-03 22:04:22 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
|
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
2026-04-09 17:21:35 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-04-03 22:04:22 +00:00
|
|
|
"github.com/aarondl/opt/omit"
|
2026-04-14 14:50:28 +00:00
|
|
|
"github.com/google/uuid"
|
2026-04-14 15:31:10 +00:00
|
|
|
"github.com/gorilla/mux"
|
2026-04-09 17:21:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-04-03 22:04:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Water(r *router) *waterR {
|
|
|
|
|
return &waterR{
|
|
|
|
|
router: r,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type waterR struct {
|
|
|
|
|
router *router
|
|
|
|
|
}
|
|
|
|
|
type water struct {
|
2026-04-09 17:21:35 +00:00
|
|
|
District string `json:"district"`
|
2026-04-14 15:28:41 +00:00
|
|
|
PublicID string `json:"public_id"`
|
2026-04-09 17:21:35 +00:00
|
|
|
URI string `json:"uri"`
|
2026-04-03 22:04:22 +00:00
|
|
|
}
|
|
|
|
|
type waterForm struct {
|
2026-04-24 22:21:01 +00:00
|
|
|
AccessComments string `schema:"access-comments"`
|
|
|
|
|
AccessDog bool `schema:"access-dog"`
|
|
|
|
|
AccessFence bool `schema:"access-fence"`
|
|
|
|
|
AccessGate bool `schema:"access-gate"`
|
|
|
|
|
AccessLocked bool `schema:"access-locked"`
|
|
|
|
|
AccessOther bool `schema:"access-other"`
|
|
|
|
|
Address types.Address `schema:"address"`
|
|
|
|
|
AddressGID string `schema:"address-gid"`
|
|
|
|
|
ClientID uuid.UUID `schema:"client_id" json:"client_id"`
|
|
|
|
|
Comments string `schema:"comments"`
|
2026-05-07 10:39:17 +00:00
|
|
|
Duration omit.Val[modelpublicreport.Nuisancedurationtype] `schema:"duration"`
|
2026-04-24 22:21:01 +00:00
|
|
|
HasAdult bool `schema:"has-adult"`
|
|
|
|
|
HasBackyardPermission bool `schema:"backyard-permission"`
|
|
|
|
|
HasLarvae bool `schema:"has-larvae"`
|
|
|
|
|
HasPupae bool `schema:"has-pupae"`
|
|
|
|
|
IsReporterConfidential bool `schema:"reporter-confidential"`
|
|
|
|
|
IsReporter_owner bool `schema:"property-ownership"`
|
|
|
|
|
Location types.Location `schema:"location"`
|
|
|
|
|
OwnerEmail string `schema:"owner-email"`
|
|
|
|
|
OwnerName string `schema:"owner-name"`
|
|
|
|
|
OwnerPhone string `schema:"owner-phone"`
|
2026-04-03 22:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 06:36:55 +00:00
|
|
|
func (res *waterR) ByID(ctx context.Context, r *http.Request, u platform.User, query QueryParams) (*types.PublicReportWater, *nhttp.ErrorWithStatus) {
|
|
|
|
|
return res.byID(ctx, r, false)
|
|
|
|
|
}
|
|
|
|
|
func (res *waterR) ByIDPublic(ctx context.Context, r *http.Request, query QueryParams) (*types.PublicReportWater, *nhttp.ErrorWithStatus) {
|
|
|
|
|
return res.byID(ctx, r, true)
|
2026-04-14 15:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-03 22:04:22 +00:00
|
|
|
func (res *waterR) Create(ctx context.Context, r *http.Request, w waterForm) (*water, *nhttp.ErrorWithStatus) {
|
2026-04-14 14:50:28 +00:00
|
|
|
user_agent := r.Header.Get("User-Agent")
|
2026-04-14 15:11:07 +00:00
|
|
|
err := platform.EnsureClient(ctx, w.ClientID, user_agent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("Failed to ensure client: %w", err)
|
|
|
|
|
}
|
2026-04-03 22:04:22 +00:00
|
|
|
|
|
|
|
|
uploads, err := html.ExtractImageUploads(r)
|
2026-04-09 17:21:35 +00:00
|
|
|
log.Info().Int("len", len(uploads)).Msg("extracted water uploads")
|
2026-04-03 22:04:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("Failed to extract image uploads: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 17:21:35 +00:00
|
|
|
accuracy := float32(0.0)
|
|
|
|
|
if w.Location.Accuracy != nil {
|
|
|
|
|
accuracy = *w.Location.Accuracy
|
2026-04-03 22:04:22 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
setter_report := modelpublicreport.Report{
|
|
|
|
|
//AddressID: omitnull.From(...),
|
|
|
|
|
AddressGid: w.Address.GID,
|
|
|
|
|
AddressRaw: w.Address.Raw,
|
|
|
|
|
ClientUUID: &w.ClientID,
|
|
|
|
|
Created: time.Now(),
|
|
|
|
|
//H3cell: omitnull.From(latlng.Cell.String()),
|
|
|
|
|
LatlngAccuracyType: modelpublicreport.Accuracytype_Browser,
|
|
|
|
|
LatlngAccuracyValue: accuracy,
|
|
|
|
|
//Location: omitnull.From(fmt.Sprintf("ST_GeometryFromText(Point(%s %s))", longitude, latitude)),
|
|
|
|
|
Location: nil,
|
|
|
|
|
MapZoom: float32(0.0),
|
|
|
|
|
//OrganizationID: ,
|
|
|
|
|
//PublicID:
|
|
|
|
|
ReporterEmail: "",
|
|
|
|
|
ReporterName: "",
|
|
|
|
|
ReporterPhone: "",
|
|
|
|
|
ReporterPhoneCanSms: true,
|
|
|
|
|
ReportType: modelpublicreport.Reporttype_Water,
|
|
|
|
|
Status: modelpublicreport.Reportstatustype_Reported,
|
2026-04-03 22:04:22 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
setter_water := modelpublicreport.Water{
|
|
|
|
|
AccessComments: w.AccessComments,
|
|
|
|
|
AccessDog: w.AccessDog,
|
|
|
|
|
AccessFence: w.AccessFence,
|
|
|
|
|
AccessGate: w.AccessGate,
|
|
|
|
|
AccessLocked: w.AccessLocked,
|
|
|
|
|
AccessOther: w.AccessOther,
|
|
|
|
|
Comments: w.Comments,
|
|
|
|
|
Duration: w.Duration.GetOr(modelpublicreport.Nuisancedurationtype_None),
|
|
|
|
|
HasAdult: w.HasAdult,
|
|
|
|
|
HasBackyardPermission: w.HasBackyardPermission,
|
|
|
|
|
HasLarvae: w.HasLarvae,
|
|
|
|
|
HasPupae: w.HasPupae,
|
|
|
|
|
IsReporterConfidential: w.IsReporterConfidential,
|
|
|
|
|
IsReporterOwner: w.IsReporter_owner,
|
|
|
|
|
OwnerEmail: w.OwnerEmail,
|
|
|
|
|
OwnerName: w.OwnerName,
|
|
|
|
|
OwnerPhone: w.OwnerPhone,
|
2026-04-03 22:04:22 +00:00
|
|
|
//ReportID omit.Val[int32]
|
|
|
|
|
}
|
2026-04-14 15:11:07 +00:00
|
|
|
report, err := platform.PublicReportWaterCreate(ctx, setter_report, setter_water, w.Location, w.Address, uploads)
|
2026-04-03 22:04:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("Failed to save new report: %w", err)
|
|
|
|
|
}
|
2026-04-28 06:53:58 +00:00
|
|
|
uri, err := res.router.IDStrToURI("publicreport.ByIDGetPublic", report.PublicID)
|
2026-04-09 17:21:35 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("generate uri: %w", err)
|
|
|
|
|
}
|
|
|
|
|
district_uri, err := res.router.IDToURI("district.ByIDGet", int(report.OrganizationID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("generate district uri: %w", err)
|
|
|
|
|
}
|
2026-04-03 22:04:22 +00:00
|
|
|
return &water{
|
2026-04-09 17:21:35 +00:00
|
|
|
District: district_uri,
|
2026-04-14 15:28:41 +00:00
|
|
|
PublicID: report.PublicID,
|
2026-04-09 17:21:35 +00:00
|
|
|
URI: uri,
|
2026-04-03 22:04:22 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|
2026-04-28 06:36:55 +00:00
|
|
|
func (res *waterR) byID(ctx context.Context, r *http.Request, is_public bool) (*types.PublicReportWater, *nhttp.ErrorWithStatus) {
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
public_id := vars["id"]
|
|
|
|
|
if public_id == "" {
|
|
|
|
|
return nil, nhttp.NewBadRequest("You must provid an ID")
|
|
|
|
|
}
|
|
|
|
|
report, err := platform.PublicReportByIDWater(ctx, public_id, is_public)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("get report: %w", err)
|
|
|
|
|
}
|
|
|
|
|
populateDistrictURI(&report.PublicReport, res.router)
|
2026-04-28 07:12:12 +00:00
|
|
|
populateReportURI(&report.PublicReport, res.router, is_public)
|
2026-04-28 06:36:55 +00:00
|
|
|
return report, nil
|
|
|
|
|
}
|