Move all sync pages to authenticatedHandler

Still need to fix many templates
This commit is contained in:
Eli Ribble 2026-02-24 15:34:53 +00:00
parent 85d2d0b95b
commit dac52a879a
No known key found for this signature in database
23 changed files with 409 additions and 476 deletions

View file

@ -1,12 +1,11 @@
package sync
import (
"context"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
"github.com/Gleipnir-Technology/nidus-sync/html"
"github.com/go-chi/chi/v5"
"github.com/uber/h3-go/v4"
)
@ -18,70 +17,48 @@ type contentCell struct {
MapData ComponentMap
Traps []TrapSummary
Treatments []Treatment
URL ContentURL
User User
}
func getCellDetails(w http.ResponseWriter, r *http.Request, user *models.User) {
func getCellDetails(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*response[contentCell], *errorWithStatus) {
cell_str := chi.URLParam(r, "cell")
if cell_str == "" {
respondError(w, "There should always be a cell", nil, http.StatusBadRequest)
return
return nil, newErrorStatus(http.StatusBadRequest, "There should always be a cell")
}
c, err := HexToInt64(cell_str)
if err != nil {
respondError(w, "Cannot convert provided cell to uint64", err, http.StatusBadRequest)
return
}
ctx := r.Context()
org, err := user.Organization().One(ctx, db.PGInstance.BobDB)
if err != nil {
respondError(w, "Failed to get org", err, http.StatusInternalServerError)
return
}
userContent, err := contentForUser(ctx, user)
if err != nil {
respondError(w, "Failed to get user", err, http.StatusInternalServerError)
return
return nil, newErrorStatus(http.StatusBadRequest, "Cannot convert provided cell to uint64")
}
center, err := h3.Cell(c).LatLng()
if err != nil {
respondError(w, "Failed to get center", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get center: %w", err)
}
boundary, err := h3.Cell(c).Boundary()
if err != nil {
respondError(w, "Failed to get boundary", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get boundary: %w", err)
}
inspections, err := inspectionsByCell(ctx, org, h3.Cell(c))
if err != nil {
respondError(w, "Failed to get inspections by cell", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get inspections by cell: %w", err)
}
geojson, err := h3utils.H3ToGeoJSON([]h3.Cell{h3.Cell(c)})
if err != nil {
respondError(w, "Failed to get boundaries", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get boundaries: %w", err)
}
resolution := h3.Cell(c).Resolution()
sources, err := breedingSourcesByCell(ctx, org, h3.Cell(c))
if err != nil {
respondError(w, "Failed to get sources", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get sources: %w", err)
}
traps, err := trapsByCell(ctx, org, h3.Cell(c))
if err != nil {
respondError(w, "Failed to get traps", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get traps: %w", err)
}
treatments, err := treatmentsByCell(ctx, org, h3.Cell(c))
if err != nil {
respondError(w, "Failed to get treatments", err, http.StatusInternalServerError)
return
return nil, newError("Failed to get treatments: %w", err)
}
data := contentCell{
return newResponse("sync/cell.html", contentCell{
BreedingSources: sources,
CellBoundary: boundary,
Inspections: inspections,
@ -95,8 +72,5 @@ func getCellDetails(w http.ResponseWriter, r *http.Request, user *models.User) {
},
Traps: traps,
Treatments: treatments,
URL: newContentURL(),
User: userContent,
}
html.RenderOrError(w, "sync/cell.html", &data)
}), nil
}