Skip to content

Commit

Permalink
Merge pull request #1607 from stakwork/feat/budget_stats
Browse files Browse the repository at this point in the history
Updated bounty budget endpoint to accept budget for other bounty statuses
  • Loading branch information
elraphty authored Apr 4, 2024
2 parents 628ad66 + 6a4076b commit 201b373
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 10 deletions.
25 changes: 25 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,9 +1769,34 @@ func (db database) GetPaymentHistoryByCreated(created *time.Time, org_uuid strin
func (db database) GetOrganizationBudget(org_uuid string) BountyBudget {
ms := BountyBudget{}
db.db.Where("org_uuid = ?", org_uuid).Find(&ms)

return ms
}

func (db database) GetOrganizationStatusBudget(org_uuid string) StatusBudget {

orgBudget := db.GetOrganizationBudget(org_uuid)

var openBudget uint
db.db.Model(&Bounty{}).Where("assignee = '' ").Select("SUM(price)").Row().Scan(&openBudget)

var assignedBudget uint
db.db.Model(&Bounty{}).Where("assignee != '' ").Select("SUM(price)").Row().Scan(&assignedBudget)

var completedBudget uint
db.db.Model(&Bounty{}).Where("completed = true ").Select("SUM(price)").Row().Scan(&completedBudget)

statusBudget := StatusBudget{
OrgUuid: org_uuid,
CurrentBudget: orgBudget.TotalBudget,
OpenBudget: openBudget,
AssignedBudget: assignedBudget,
CompletedBudget: completedBudget,
}

return statusBudget
}

func (db database) GetOrganizationBudgetHistory(org_uuid string) []BudgetHistoryData {
budgetHistory := []BudgetHistoryData{}

Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Database interface {
UpdateOrganizationBudget(budget BountyBudget) BountyBudget
GetPaymentHistoryByCreated(created *time.Time, org_uuid string) PaymentHistory
GetOrganizationBudget(org_uuid string) BountyBudget
GetOrganizationStatusBudget(org_uuid string) StatusBudget
GetOrganizationBudgetHistory(org_uuid string) []BudgetHistoryData
AddAndUpdateBudget(invoice InvoiceList) PaymentHistory
WithdrawBudget(sender_pubkey string, org_uuid string, amount uint)
Expand Down
9 changes: 9 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ type Bounty struct {
OwnerID string `json:"owner_id"`
Paid bool `json:"paid"`
Show bool `gorm:"default:false" json:"show"`
Completed bool `gorm:"default:false" json:"completed"`
Type string `json:"type"`
Award string `json:"award"`
AssignedHours uint8 `json:"assigned_hours"`
Expand Down Expand Up @@ -496,6 +497,14 @@ type BountyBudget struct {
Updated *time.Time `json:"updated"`
}

type StatusBudget struct {
OrgUuid string `json:"org_uuid"`
CurrentBudget uint `json:"current_budget"`
OpenBudget uint `json:"open_budget"`
AssignedBudget uint `json:"assigned_budget"`
CompletedBudget uint `json:"completed_budget"`
}

type BudgetInvoiceRequest struct {
Amount uint `json:"amount"`
SenderPubKey string `json:"sender_pubkey"`
Expand Down
7 changes: 7 additions & 0 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func UpdatePaymentStatus(w http.ResponseWriter, r *http.Request) {
// if setting paid as true by mark as paid
// set completion date and mark as paid
if bounty.Paid {
bounty.Completed = true
bounty.CompletionDate = &now
bounty.MarkAsPaidDate = &now
if bounty.PaidDate == nil {
Expand Down Expand Up @@ -525,6 +526,7 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request

bounty.Paid = true
bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now
h.db.UpdateBounty(bounty)

Expand Down Expand Up @@ -781,7 +783,12 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
bounty, err := h.db.GetBountyByCreated(uint(invData.Created))

if err == nil {
now := time.Now()

bounty.Paid = true
bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now
}

h.db.UpdateBounty(bounty)
Expand Down
18 changes: 9 additions & 9 deletions handlers/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,16 @@ func TestGetOrganizationBudget(t *testing.T) {

t.Run("Should test that the right organization budget is returned, if the user is the organization admin or has the ViewReport role", func(t *testing.T) {
orgUUID := "valid-uuid"
expectedBudget := db.BountyBudget{
ID: 1,
OrgUuid: orgUUID,
TotalBudget: 1000,
Created: nil,
Updated: nil,
statusBudget := db.StatusBudget{
OrgUuid: orgUUID,
CurrentBudget: 10000,
OpenBudget: 1000,
AssignedBudget: 2000,
CompletedBudget: 3000,
}

oHandler.userHasAccess = mockUserHasAccess
mockDb.On("GetOrganizationBudget", orgUUID).Return(expectedBudget).Once()
mockDb.On("GetOrganizationStatusBudget", orgUUID).Return(statusBudget).Once()

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", orgUUID)
Expand All @@ -455,13 +455,13 @@ func TestGetOrganizationBudget(t *testing.T) {

assert.Equal(t, http.StatusOK, rr.Code)

var responseBudget db.BountyBudget
var responseBudget db.StatusBudget
err = json.Unmarshal(rr.Body.Bytes(), &responseBudget)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, expectedBudget, responseBudget)
assert.Equal(t, statusBudget, responseBudget)
})
}

Expand Down
2 changes: 1 addition & 1 deletion handlers/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func (oh *organizationHandler) GetOrganizationBudget(w http.ResponseWriter, r *h
}

// get the organization budget
organizationBudget := oh.db.GetOrganizationBudget(uuid)
organizationBudget := oh.db.GetOrganizationStatusBudget(uuid)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(organizationBudget)
Expand Down
46 changes: 46 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 201b373

Please sign in to comment.