This commit is contained in:
cangui 2025-05-11 19:07:37 +02:00
parent 9b8658c6e8
commit b96dac5888
2 changed files with 97 additions and 1 deletions

View File

@ -3,14 +3,17 @@ package renders
import ( import (
"cangui/whatsapp/backend/models" "cangui/whatsapp/backend/models"
"net/http" "net/http"
"sort"
"strconv" "strconv"
"text/template" "text/template"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm" "gorm.io/gorm"
) )
// les renders permette d afficher les pages html. // les renders permette d afficher les pages html.
func Login(w http.ResponseWriter, r *http.Request){ func Login(w http.ResponseWriter, r *http.Request){
@ -528,6 +531,98 @@ func ClientConsumptionPage(db *gorm.DB) http.HandlerFunc {
renderTemplate(w, "client_consumption", data) renderTemplate(w, "client_consumption", data)
} }
} }
func ConsumptionPageGrouped(db *gorm.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ssoid, ok := r.Context().Value("ssoid").(string)
if !ok || ssoid == "" {
http.Error(w, "Non authentifié", http.StatusUnauthorized)
return
}
// Récupération de l'utilisateur courant
var currentUser models.User
if err := db.Where("sso_id = ?", ssoid).First(&currentUser).Error; err != nil {
http.Error(w, "Utilisateur introuvable", http.StatusUnauthorized)
return
}
// Filtrage admin / client ciblé
clientIDStr := r.URL.Query().Get("client")
var clientID uint
if currentUser.Role == models.ROLE_ADMIN && clientIDStr != "" {
if id, err := strconv.Atoi(clientIDStr); err == nil {
clientID = uint(id)
}
} else {
clientID = currentUser.ID
}
// Période
period := r.URL.Query().Get("period")
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{}
}
// Récupération des consommations
var consumptions []models.Consumption
query := db.Where("user_id = ?", clientID)
if !fromDate.IsZero() {
query = query.Where("created_at >= ?", fromDate)
}
query.Order("created_at desc").Find(&consumptions)
// Groupe par jour
type DailyGroup struct {
Date string
Total uint
Lines []models.Consumption
}
grouped := map[string]*DailyGroup{}
for _, c := range consumptions {
day := c.CreatedAt.Format("2006-01-02")
if _, exists := grouped[day]; !exists {
grouped[day] = &DailyGroup{Date: day}
}
grouped[day].Total += c.CreditsUsed
grouped[day].Lines = append(grouped[day].Lines, c)
}
// Conversion en slice et tri
var groupedSlice []*DailyGroup
for _, v := range grouped {
groupedSlice = append(groupedSlice, v)
}
sort.Slice(groupedSlice, func(i, j int) bool {
return groupedSlice[i].Date > groupedSlice[j].Date
})
data := map[string]interface{}{
"User": currentUser,
"GroupedConsumptions": groupedSlice,
"SelectedClientID": clientID,
}
// Pour ADMIN : liste des clients
if currentUser.Role == models.ROLE_ADMIN {
var clients []models.User
db.Where("role = ?", models.ROLE_CLIENT).Find(&clients)
data["Clients"] = clients
}
renderTemplate(w, "client_consumption", data)
}
}
// pour un rendu complet de la page // pour un rendu complet de la page

View File

@ -76,7 +76,8 @@ func RoutesPublic(r *mux.Router, db *gorm.DB) {
r.HandleFunc("/test/send2", renders.TestMessagesPages2) r.HandleFunc("/test/send2", renders.TestMessagesPages2)
r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET") r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET")
r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST") r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
r.HandleFunc("/admin/consumption", renders.ClientConsumptionPage(db)) // r.HandleFunc("/admin/consumption", renders.ClientConsumptionPage(db))
r.HandleFunc("/admin/consumption", renders.ConsumptionPageGrouped(db))
r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{