diff --git a/htmlpage/html.go b/htmlpage/html.go index a34d8197..0fa93df0 100644 --- a/htmlpage/html.go +++ b/htmlpage/html.go @@ -112,6 +112,7 @@ func makeFuncMap() template.FuncMap { "bigNumber": bigNumber, "GISStatement": gisStatement, "latLngDisplay": latLngDisplay, + "publicReportID": publicReportID, "timeAsRelativeDate": timeAsRelativeDate, "timeDelta": timeDelta, "timeElapsed": timeElapsed, @@ -145,6 +146,13 @@ func parseFromDisk(files []string) (*template.Template, error) { return templ, nil } +func publicReportID(s string) string { + if len(s) != 12 { + return s + } + return s[0:4] + "-" + s[4:8] + "-" + s[8:12] +} + func timeAsRelativeDate(d time.Time) string { return d.Format("01-02") } diff --git a/public-report/endpoint.go b/public-report/endpoint.go index 2b4bd6d2..0eb77bb7 100644 --- a/public-report/endpoint.go +++ b/public-report/endpoint.go @@ -29,13 +29,6 @@ func getRegisterNotificationsComplete(w http.ResponseWriter, r *http.Request) { }, ) } -func getStatus(w http.ResponseWriter, r *http.Request) { - htmlpage.RenderOrError( - w, - Status, - ContextStatus{}, - ) -} func postRegisterNotifications(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { diff --git a/public-report/page.go b/public-report/page.go index 3791ceff..1f03ef15 100644 --- a/public-report/page.go +++ b/public-report/page.go @@ -17,12 +17,10 @@ type ContextRegisterNotificationsComplete struct { ReportID string } type ContextRoot struct{} -type ContextStatus struct{} var ( RegisterNotificationsComplete = buildTemplate("register-notifications-complete", "base") Root = buildTemplate("root", "base") - Status = buildTemplate("status", "base") ) var components = [...]string{"footer", "location-geocode", "location-geocode-header", "photo-upload", "photo-upload-header"} diff --git a/public-report/routes.go b/public-report/routes.go index 3bdd6b32..a0a55bb6 100644 --- a/public-report/routes.go +++ b/public-report/routes.go @@ -22,6 +22,7 @@ func Router() chi.Router { r.Post("/register-notifications", postRegisterNotifications) r.Get("/register-notifications-complete", getRegisterNotificationsComplete) r.Get("/status", getStatus) + r.Get("/status/{report_id}", getStatusByID) localFS := http.Dir("./static") htmlpage.FileServer(r, "/static", localFS, EmbeddedStaticFS, "static") return r diff --git a/public-report/status.go b/public-report/status.go new file mode 100644 index 00000000..7b14a371 --- /dev/null +++ b/public-report/status.go @@ -0,0 +1,145 @@ +package publicreport + +import ( + "net/http" + + "github.com/Gleipnir-Technology/nidus-sync/htmlpage" + "github.com/go-chi/chi/v5" + /* + "fmt" + "strconv" + "time" + + "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/h3utils" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" + "github.com/rs/zerolog/log" + "github.com/stephenafamo/bob/dialect/psql" + "github.com/stephenafamo/bob/dialect/psql/um" + */) + +type Report struct { + ID string +} + +type ContextStatus struct{} +type ContextStatusByID struct { + Report Report +} + +var ( + Status = buildTemplate("status", "base") + StatusByID = buildTemplate("status-by-id", "base") +) + +func getStatus(w http.ResponseWriter, r *http.Request) { + htmlpage.RenderOrError( + w, + Status, + ContextStatus{}, + ) +} +func getStatusByID(w http.ResponseWriter, r *http.Request) { + report_id := chi.URLParam(r, "report_id") + htmlpage.RenderOrError( + w, + StatusByID, + ContextStatusByID{ + Report: Report{ + ID: report_id, + }, + }, + ) +} + +/* +func getQuick(w http.ResponseWriter, r *http.Request) { + htmlpage.RenderOrError( + w, + Quick, + ContextQuick{}, + ) +} +func getQuickSubmitComplete(w http.ResponseWriter, r *http.Request) { + report := r.URL.Query().Get("report") + htmlpage.RenderOrError( + w, + QuickSubmitComplete, + ContextQuickSubmitComplete{ + ReportID: report, + }, + ) +} +func postQuick(w http.ResponseWriter, r *http.Request) { + err := r.ParseMultipartForm(32 << 10) // 32 MB buffer + if err != nil { + respondError(w, "Failed to parse form", err, http.StatusBadRequest) + return + } + lat := r.FormValue("latitude") + lng := r.FormValue("longitude") + comments := r.FormValue("comments") + //photos := r.FormValue("photos") + + latitude, err := strconv.ParseFloat(lat, 64) + if err != nil { + respondError(w, "Failed to create parse latitude", err, http.StatusBadRequest) + return + } + longitude, err := strconv.ParseFloat(lng, 64) + if err != nil { + respondError(w, "Failed to create parse longitude", err, http.StatusBadRequest) + return + } + u, err := GenerateReportID() + if err != nil { + respondError(w, "Failed to create quick report public ID", err, http.StatusInternalServerError) + return + } + c, err := h3utils.GetCell(longitude, latitude, 15) + setter := models.PublicreportQuickSetter{ + Created: omit.From(time.Now()), + Comments: omit.From(comments), + //Location: omitnull.From(fmt.Sprintf("ST_GeometryFromText(Point(%s %s))", longitude, latitude)), + H3cell: omitnull.From(c.String()), + PublicID: omit.From(u), + ReporterEmail: omit.From(""), + ReporterPhone: omit.From(""), + } + quick, err := models.PublicreportQuicks.Insert(&setter).One(r.Context(), db.PGInstance.BobDB) + if err != nil { + respondError(w, "Failed to create database record", err, http.StatusInternalServerError) + return + } + _, err = psql.Update( + um.Table("publicreport.quick"), + um.SetCol("location").To(fmt.Sprintf("ST_GeometryFromText('Point(%f %f)')", longitude, latitude)), + um.Where(psql.Quote("id").EQ(psql.Arg(quick.ID))), + ).Exec(r.Context(), db.PGInstance.BobDB) + if err != nil { + respondError(w, "Failed to insert publicreport", err, http.StatusInternalServerError) + return + } + log.Info().Float64("latitude", latitude).Float64("longitude", longitude).Msg("Got upload") + photoSetters := make([]*models.PublicreportQuickPhotoSetter, 0) + uploads, err := extractPhotoUploads(r) + if err != nil { + respondError(w, "Failed to extract photo uploads", err, http.StatusInternalServerError) + return + } + for _, u := range uploads { + photoSetters = append(photoSetters, &models.PublicreportQuickPhotoSetter{ + Filename: omit.From(u.Filename), + Size: omit.From(u.Size), + UUID: omit.From(u.UUID), + }) + } + err = quick.InsertQuickPhotos(r.Context(), db.PGInstance.BobDB, photoSetters...) + if err != nil { + respondError(w, "Failed to create photo records", err, http.StatusInternalServerError) + return + } + http.Redirect(w, r, fmt.Sprintf("/quick-submit-complete?report=%s", u), http.StatusFound) +}*/ diff --git a/public-report/template/nuisance-submit-complete.html b/public-report/template/nuisance-submit-complete.html index 6cf6efcf..1e6c6237 100644 --- a/public-report/template/nuisance-submit-complete.html +++ b/public-report/template/nuisance-submit-complete.html @@ -32,7 +32,7 @@
Your report has been successfully submitted.
Your report has been successfully submitted.
Thank you for helping us control mosquito populations in your area!
Please save this ID for your reference.
You can check the status of your report at any time using your Report ID.
- + Check Status diff --git a/public-report/template/register-notifications-complete.html b/public-report/template/register-notifications-complete.html index 1ebd1791..44123252 100644 --- a/public-report/template/register-notifications-complete.html +++ b/public-report/template/register-notifications-complete.html @@ -32,7 +32,7 @@Your contact information has been successfully registered for report updates.
Name: Jane Doe
+Phone: (555) 123-4567
+Address: 123 Main Street, Anytown, USA 12345
+Owner: John Smith
+Address: 456 Elm Street, Anytown, USA 12345
+Description: Standing water in abandoned pool
+Site visit scheduled for July 19. Technician: Michael Johnson
+Initial assessment completed. Property requires treatment for mosquito larvae.
+Report assigned to field supervisor for initial assessment.
+New mosquito nuisance report submitted by Jane Doe.
+