consomation
This commit is contained in:
parent
504969e9b6
commit
d84774a031
@ -459,6 +459,76 @@ func Dashboard(db *gorm.DB) http.HandlerFunc {
|
|||||||
renderTemplate(w, "dashboard", data)
|
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
|
// pour un rendu complet de la page
|
||||||
func renderTemplate(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
func renderTemplate(w http.ResponseWriter, templ string, data map[string]interface{}) {
|
||||||
|
|||||||
@ -76,6 +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("/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{
|
||||||
Name: "token",
|
Name: "token",
|
||||||
|
|||||||
56
frontend/templates/client_consumption.pages.tmpl
Normal file
56
frontend/templates/client_consumption.pages.tmpl
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
{{ define "client_consumption.pages.tmpl" }}
|
||||||
|
{{ template "head" . }}
|
||||||
|
<div class="columns">
|
||||||
|
{{ template "sidebar" . }}
|
||||||
|
<div class="column is-10">
|
||||||
|
<section class="section">
|
||||||
|
<h1 class="title is-4">📊 Statistiques de consommation</h1>
|
||||||
|
|
||||||
|
{{ if eq .User.Role "ADMIN" }}
|
||||||
|
<div class="field mb-4">
|
||||||
|
<label class="label">👤 Sélectionner un client :</label>
|
||||||
|
<div class="control">
|
||||||
|
<div class="select is-small">
|
||||||
|
<select id="clientSelect" name="client" onchange="location.href='/admin/consumption?client=' + this.value">
|
||||||
|
<option value="">Tous les clients</option>
|
||||||
|
{{ range .Clients }}
|
||||||
|
<option value="{{ .ID }}" {{ if eq $.SelectedClientID .ID }}selected{{ end }}>{{ .Email }}</option>
|
||||||
|
{{ end }}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
<div class="buttons are-small mb-4">
|
||||||
|
<a class="button is-info is-light" href="?period=today">📅 Aujourd'hui</a>
|
||||||
|
<a class="button is-info is-light" href="?period=week">📆 Cette semaine</a>
|
||||||
|
<a class="button is-info is-light" href="?period=month">🗓️ Ce mois</a>
|
||||||
|
<a class="button is-info is-light" href="?period=year">📈 Cette année</a>
|
||||||
|
<a class="button is-info is-light" href="/admin/consumption">🔁 Tout</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<table class="table is-striped is-fullwidth">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Date</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Description</th>
|
||||||
|
<th>Crédits utilisés</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{ range .Consumptions }}
|
||||||
|
<tr>
|
||||||
|
<td>{{ .CreatedAt.Format "02/01/2006 15:04" }}</td>
|
||||||
|
<td>{{ .MessageType }}</td>
|
||||||
|
<td>{{ .Description }}</td>
|
||||||
|
<td>{{ .CreditsUsed }}</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
Loading…
Reference in New Issue
Block a user