248 lines
7.9 KiB
Go
248 lines
7.9 KiB
Go
package application
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *Handler) AdministrationHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Call business logic handler
|
|
result, err := h.applicationHandler.GetAdministrationData(r.Context())
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving administration data")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Render HTTP response
|
|
h.renderer.Administration(
|
|
w, r,
|
|
result.Accounts,
|
|
result.Beneficiaries,
|
|
result.Groups,
|
|
result.Bookings,
|
|
result.Events,
|
|
)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdministrationCreateGroupHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" {
|
|
if err := r.ParseForm(); err != nil {
|
|
log.Error().Err(err).Msg("error parsing form")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Extract and validate form parameters
|
|
name := r.FormValue("name")
|
|
if name == "" {
|
|
log.Error().Str("name", name).Msg("Invalid name")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Extract modules configuration
|
|
modules := map[string]any{
|
|
"beneficiaries": r.FormValue("modules.beneficiaries") == "on",
|
|
"journeys": r.FormValue("modules.journeys") == "on",
|
|
"vehicles": r.FormValue("modules.vehicles") == "on",
|
|
"vehicles_management": r.FormValue("modules.vehicles_management") == "on",
|
|
"events": r.FormValue("modules.events") == "on",
|
|
"agenda": r.FormValue("modules.agenda") == "on",
|
|
"groups": r.FormValue("modules.groups") == "on",
|
|
"administration": r.FormValue("modules.administration") == "on",
|
|
"support": r.FormValue("modules.support") == "on",
|
|
"group_module": r.FormValue("modules.group_module") == "on",
|
|
"organized_carpool": r.FormValue("modules.organized_carpool") == "on",
|
|
"solidarity_transport": r.FormValue("modules.solidarity_transport") == "on",
|
|
}
|
|
|
|
// Call business logic handler
|
|
groupID, err := h.applicationHandler.CreateAdministrationGroup(r.Context(), name, modules)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error creating administration group")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Redirect to group display
|
|
http.Redirect(w, r, "/app/administration/groups/"+groupID, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
// For GET requests, render the create group form
|
|
h.renderer.AdministrationCreateGroup(w, r)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdministrationGroupDisplayHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
groupID := vars["groupid"]
|
|
|
|
// Call business logic handler
|
|
result, err := h.applicationHandler.GetAdministrationGroupData(r.Context(), groupID)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving administration group data")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Render HTTP response
|
|
h.renderer.AdministrationGroupDisplay(w, r, result.Group, result.Members, result.Admins)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdministrationGroupInviteAdminHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
groupID := vars["groupid"]
|
|
|
|
if r.Method == "POST" {
|
|
if err := r.ParseForm(); err != nil {
|
|
log.Error().Err(err).Msg("error parsing form")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Extract form parameters
|
|
username := r.FormValue("username")
|
|
if username == "" {
|
|
log.Error().Str("username", username).Msg("Invalid username")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Call business logic handler
|
|
err := h.applicationHandler.InviteAdministrationGroupAdmin(r.Context(), groupID, username)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error inviting group admin")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Redirect back to group display
|
|
http.Redirect(w, r, "/app/administration/groups/"+groupID, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
// For GET requests, redirect to group display (no separate form)
|
|
http.Redirect(w, r, "/app/administration/groups/"+groupID, http.StatusFound)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdministrationGroupInviteMemberHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
groupID := vars["groupid"]
|
|
|
|
if r.Method == "POST" {
|
|
if err := r.ParseForm(); err != nil {
|
|
log.Error().Err(err).Msg("error parsing form")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Extract form parameters
|
|
username := r.FormValue("username")
|
|
if username == "" {
|
|
log.Error().Str("username", username).Msg("Invalid username")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Call business logic handler
|
|
err := h.applicationHandler.InviteAdministrationGroupMember(r.Context(), groupID, username)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error inviting group member")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Redirect back to group display
|
|
http.Redirect(w, r, "/app/administration/groups/"+groupID, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
// For GET requests, redirect to group display (no separate form)
|
|
http.Redirect(w, r, "/app/administration/groups/"+groupID, http.StatusFound)
|
|
}
|
|
}
|
|
func (h *Handler) AdminStatsVehiclesHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
result, err := h.applicationHandler.GetVehiclesStats()
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving vehicles stats")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.AdminStatVehicles(w, r, result.Vehicles, result.Bookings, result.Groups)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdminStatsBookingsHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Extract filter parameters from query
|
|
status := r.URL.Query().Get("status")
|
|
dateStart := r.URL.Query().Get("date_start")
|
|
dateEnd := r.URL.Query().Get("date_end")
|
|
|
|
// Default to last month if no dates specified
|
|
if dateStart == "" {
|
|
dateStart = time.Now().AddDate(0, -1, 0).Format("2006-01-02")
|
|
}
|
|
if dateEnd == "" {
|
|
dateEnd = time.Now().Format("2006-01-02")
|
|
}
|
|
|
|
result, err := h.applicationHandler.GetBookingsStats(status, dateStart, dateEnd)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving bookings stats")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Prepare filters map for template
|
|
filters := map[string]string{
|
|
"status": status,
|
|
"date_start": dateStart,
|
|
"date_end": dateEnd,
|
|
}
|
|
|
|
h.renderer.AdminStatBookings(w, r, result.Vehicles, result.Bookings, result.Groups, result.BeneficiariesMap, filters)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdminStatsBeneficiariesHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
result, err := h.applicationHandler.GetBeneficiariesStats()
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving beneficiaries stats")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.AdminStatBeneficaires(w, r, result.Beneficiaries, result.CacheID)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) AdminStatsEventsHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
result, err := h.applicationHandler.GetEventsStats()
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving events stats")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.AdminStatEvents(w, r, result.Events, result.Groups)
|
|
}
|
|
}
|