Skip to content

Commit

Permalink
Merge pull request #1682 from stakwork/feat/poll_user_workspace_invoices
Browse files Browse the repository at this point in the history
Add Functionality To All User Workspaces Invoice
  • Loading branch information
elraphty authored Jun 5, 2024
2 parents e04281f + aaf3702 commit 2395b7f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
123 changes: 96 additions & 27 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,31 +444,8 @@ func GetUserWorkspaces(w http.ResponseWriter, r *http.Request) {
}

user := db.DB.GetPerson(userId)

// get the workspaces created by the user, then get all the workspaces
// the user has been added to, loop through to get the workspace
workspaces := GetCreatedWorkspaces(user.OwnerPubKey)

assignedWorkspaces := db.DB.GetUserAssignedWorkspaces(user.OwnerPubKey)
for _, value := range assignedWorkspaces {
uuid := value.WorkspaceUuid
workspace := db.DB.GetWorkspaceByUuid(uuid)
bountyCount := db.DB.GetWorkspaceBountyCount(uuid)
hasRole := db.UserHasAccess(user.OwnerPubKey, uuid, db.ViewReport)

// don't add deleted workspaces to the list
if !workspace.Deleted {
if hasRole {
budget := db.DB.GetWorkspaceBudget(uuid)
workspace.Budget = budget.TotalBudget
} else {
workspace.Budget = 0
}
workspace.BountyCount = bountyCount

workspaces = append(workspaces, workspace)
}
}
// get the user workspaces
workspaces := GetAllUserWorkspaces(user.OwnerPubKey)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspaces)
Expand All @@ -489,7 +466,6 @@ func (oh *workspaceHandler) GetUserDropdownWorkspaces(w http.ResponseWriter, r *
// get the workspaces created by the user, then get all the workspaces
// the user has been added to, loop through to get the workspace
workspaces := GetCreatedWorkspaces(user.OwnerPubKey)

assignedWorkspaces := db.DB.GetUserAssignedWorkspaces(user.OwnerPubKey)
for _, value := range assignedWorkspaces {
uuid := value.WorkspaceUuid
Expand All @@ -507,7 +483,6 @@ func (oh *workspaceHandler) GetUserDropdownWorkspaces(w http.ResponseWriter, r *
workspace.Budget = 0
}
workspace.BountyCount = bountyCount

workspaces = append(workspaces, workspace)
}
}
Expand Down Expand Up @@ -682,6 +657,53 @@ func (oh *workspaceHandler) PollBudgetInvoices(w http.ResponseWriter, r *http.Re
json.NewEncoder(w).Encode("Polled invoices")
}

func (oh *workspaceHandler) PollUserWorkspacesBudget(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
}

// get the user workspaces
workspaces := GetAllUserWorkspaces(pubKeyFromAuth)
// loop through the worksppaces and get each workspace invoice
for _, space := range workspaces {
// get all workspace invoice
workInvoices := oh.db.GetWorkspaceInvoices(space.Uuid)

for _, inv := range workInvoices {
invoiceRes, invoiceErr := oh.getLightningInvoice(inv.PaymentRequest)

if invoiceErr.Error != "" {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(invoiceErr)
return
}

if invoiceRes.Response.Settled {
if !inv.Status && inv.Type == "BUDGET" {
oh.db.AddAndUpdateBudget(inv)
// Update the invoice status
oh.db.UpdateInvoice(inv.PaymentRequest)
}
} else {
// Cheeck if time has expired
isInvoiceExpired := utils.GetInvoiceExpired(inv.PaymentRequest)
// If the invoice has expired and it is not paid delete from the DB
if isInvoiceExpired {
oh.db.DeleteInvoice(inv.PaymentRequest)
}
}
}
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("Polled user workspace invoices")
}

func GetInvoicesCount(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
Expand All @@ -698,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)
Expand Down Expand Up @@ -945,3 +987,30 @@ func (oh *workspaceHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspaceFeatures)
}

func GetAllUserWorkspaces(pubkey string) []db.Workspace {
// get the workspaces created by the user, then get all the workspaces
// the user has been added to, loop through to get the workspace
workspaces := GetCreatedWorkspaces(pubkey)
assignedWorkspaces := db.DB.GetUserAssignedWorkspaces(pubkey)
for _, value := range assignedWorkspaces {
uuid := value.WorkspaceUuid
workspace := db.DB.GetWorkspaceByUuid(uuid)
bountyCount := db.DB.GetWorkspaceBountyCount(uuid)
hasRole := db.UserHasAccess(pubkey, uuid, db.ViewReport)
// don't add deleted workspaces to the list
if !workspace.Deleted {
if hasRole {
budget := db.DB.GetWorkspaceBudget(uuid)
workspace.Budget = budget.TotalBudget
} else {
workspace.Budget = 0
}
workspace.BountyCount = bountyCount

workspaces = append(workspaces, workspace)
}
}

return workspaces
}
2 changes: 2 additions & 0 deletions routes/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func WorkspaceRoutes() chi.Router {
r.Get("/budget/history/{uuid}", workspaceHandlers.GetWorkspaceBudgetHistory)
r.Get("/payments/{uuid}", handlers.GetPaymentHistory)
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)
Expand Down

0 comments on commit 2395b7f

Please sign in to comment.