2026-02-16 18:49:07 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-24 15:34:53 +00:00
|
|
|
"context"
|
2026-02-16 18:49:07 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-03-27 06:08:55 -07:00
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/h3utils"
|
2026-03-03 17:08:58 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
|
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
2026-03-12 23:49:16 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
2026-02-16 18:49:07 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/uber/h3-go/v4"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type contentCell struct {
|
2026-03-12 23:49:16 +00:00
|
|
|
BreedingSources []platform.BreedingSourceSummary
|
2026-02-16 18:49:07 +00:00
|
|
|
CellBoundary h3.CellBoundary
|
2026-03-12 23:49:16 +00:00
|
|
|
Inspections []platform.Inspection
|
|
|
|
|
Traps []platform.TrapSummary
|
|
|
|
|
Treatments []platform.Treatment
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func getCellDetails(ctx context.Context, r *http.Request, user platform.User) (*html.Response[contentCell], *nhttp.ErrorWithStatus) {
|
2026-02-16 18:49:07 +00:00
|
|
|
cell_str := chi.URLParam(r, "cell")
|
|
|
|
|
if cell_str == "" {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewErrorStatus(http.StatusBadRequest, "There should always be a cell")
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
c, err := HexToInt64(cell_str)
|
|
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewErrorStatus(http.StatusBadRequest, "Cannot convert provided cell to uint64")
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
boundary, err := h3.Cell(c).Boundary()
|
|
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewError("Failed to get boundary: %w", err)
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
inspections, err := platform.InspectionsByCell(ctx, user.Organization, h3.Cell(c))
|
2026-02-16 18:49:07 +00:00
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewError("Failed to get inspections by cell: %w", err)
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
2026-03-27 06:08:55 -07:00
|
|
|
/*
|
2026-03-27 14:06:50 -07:00
|
|
|
center, err := h3.Cell(c).LatLng()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("Failed to get center: %w", err)
|
|
|
|
|
}
|
|
|
|
|
geojson, err := h3utils.H3ToGeoJSON([]h3.Cell{h3.Cell(c)})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("Failed to get boundaries: %w", err)
|
|
|
|
|
}
|
|
|
|
|
resolution := h3.Cell(c).Resolution()
|
2026-03-27 06:08:55 -07:00
|
|
|
*/
|
2026-03-12 23:49:16 +00:00
|
|
|
sources, err := platform.BreedingSourcesByCell(ctx, user.Organization, h3.Cell(c))
|
2026-02-16 18:49:07 +00:00
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewError("Failed to get sources: %w", err)
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
traps, err := platform.TrapsByCell(ctx, user.Organization, h3.Cell(c))
|
2026-02-16 18:49:07 +00:00
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewError("Failed to get traps: %w", err)
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
treatments, err := platform.TreatmentsByCell(ctx, user.Organization, h3.Cell(c))
|
2026-02-16 18:49:07 +00:00
|
|
|
if err != nil {
|
2026-03-03 17:08:58 +00:00
|
|
|
return nil, nhttp.NewError("Failed to get treatments: %w", err)
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|
2026-03-03 17:08:58 +00:00
|
|
|
return html.NewResponse("sync/cell.html", contentCell{
|
2026-02-16 18:49:07 +00:00
|
|
|
BreedingSources: sources,
|
|
|
|
|
CellBoundary: boundary,
|
|
|
|
|
Inspections: inspections,
|
2026-03-27 14:06:50 -07:00
|
|
|
Traps: traps,
|
|
|
|
|
Treatments: treatments,
|
2026-02-24 15:34:53 +00:00
|
|
|
}), nil
|
2026-02-16 18:49:07 +00:00
|
|
|
}
|