diff --git a/handlers/workspaces.go b/handlers/workspaces.go index c335c1167..260dea68a 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -720,6 +720,26 @@ func GetInvoicesCount(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(invoiceCount) } +func GetAllUserInvoicesCount(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + + if pubKeyFromAuth == "" { + fmt.Println("[workspaces] no pubkey from auth") + w.WriteHeader(http.StatusUnauthorized) + return + } + + allCount := int64(0) + workspaces := GetAllUserWorkspaces(pubKeyFromAuth) + for _, space := range workspaces { + invoiceCount := db.DB.GetWorkspaceInvoicesCount(space.Uuid) + allCount += invoiceCount + } + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(allCount) +} + func (oh *workspaceHandler) DeleteWorkspace(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) diff --git a/routes/workspaces.go b/routes/workspaces.go index f6385b777..0ef5f511c 100644 --- a/routes/workspaces.go +++ b/routes/workspaces.go @@ -38,6 +38,7 @@ func WorkspaceRoutes() chi.Router { r.Get("/poll/invoices/{uuid}", workspaceHandlers.PollBudgetInvoices) r.Get("/poll/user/invoices", workspaceHandlers.PollUserWorkspacesBudget) r.Get("/invoices/count/{uuid}", handlers.GetInvoicesCount) + r.Get("/user/invoices/count", handlers.GetAllUserInvoicesCount) r.Delete("/delete/{uuid}", workspaceHandlers.DeleteWorkspace) r.Post("/mission", workspaceHandlers.UpdateWorkspace)