diff --git a/backend/renders/renders.go b/backend/renders/renders.go index ae35168..5c6bfa8 100644 --- a/backend/renders/renders.go +++ b/backend/renders/renders.go @@ -459,6 +459,76 @@ func Dashboard(db *gorm.DB) http.HandlerFunc { renderTemplate(w, "dashboard", data) } } +func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + val := r.Context().Value("ssoid") + ssoid, ok := val.(string) + if !ok || ssoid == "" { + http.Error(w, "Non authentifié", http.StatusUnauthorized) + return + } + + var currentUser models.User + if err := db.Where("sso_id = ?", ssoid).First(¤tUser).Error; err != nil { + http.Error(w, "Utilisateur introuvable", http.StatusUnauthorized) + return + } + + period := r.URL.Query().Get("period") + clientIDStr := r.URL.Query().Get("client") + var clientID uint + + if clientIDStr != "" { + idParsed, _ := strconv.Atoi(clientIDStr) + clientID = uint(idParsed) + } + + var fromDate time.Time + now := time.Now() + switch period { + case "today": + fromDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + case "week": + fromDate = now.AddDate(0, 0, -7) + case "month": + fromDate = now.AddDate(0, -1, 0) + case "year": + fromDate = now.AddDate(-1, 0, 0) + default: + fromDate = time.Time{} + } + + query := db.Model(&models.Consumption{}) + if currentUser.Role == models.ROLE_ADMIN { + if clientID > 0 { + query = query.Where("user_id = ?", clientID) + } + } else { + query = query.Where("user_id = ?", currentUser.ID) + } + if !fromDate.IsZero() { + query = query.Where("created_at >= ?", fromDate) + } + + var consumptions []models.Consumption + query.Order("created_at desc").Find(&consumptions) + + data := map[string]interface{}{ + "Consumptions": consumptions, + "User": currentUser, + "SelectedClientID": clientID, + } + + if currentUser.Role == models.ROLE_ADMIN { + var clients []models.User + db.Where("role = ?", "CLIENT").Find(&clients) + data["Clients"] = clients + } + + renderTemplate(w, "client_consumption", data) + } +} + // pour un rendu complet de la page func renderTemplate(w http.ResponseWriter, templ string, data map[string]interface{}) { diff --git a/backend/routes/routes.go b/backend/routes/routes.go index 56e5c2f..c0c5413 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go @@ -76,6 +76,8 @@ func RoutesPublic(r *mux.Router, db *gorm.DB) { r.HandleFunc("/test/send2", renders.TestMessagesPages2) r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET") r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST") + r.HandleFunc("/admin/consumption", renders.ClientConsumptionPage(db)) + r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "token", diff --git a/frontend/templates/client_consumption.pages.tmpl b/frontend/templates/client_consumption.pages.tmpl new file mode 100644 index 0000000..7142d13 --- /dev/null +++ b/frontend/templates/client_consumption.pages.tmpl @@ -0,0 +1,56 @@ +{{ define "client_consumption.pages.tmpl" }} +{{ template "head" . }} +
| Date | +Type | +Description | +Crédits utilisés | +
|---|---|---|---|
| {{ .CreatedAt.Format "02/01/2006 15:04" }} | +{{ .MessageType }} | +{{ .Description }} | +{{ .CreditsUsed }} | +