-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_budget implementation (#726)
* Add get_budget implementation * Always granted scope * Don't show always_granted in the UI or save it in the DB. * chore: get budget feedback - replace always granted scope with always granted method list - return nil for budget renews at for "never" budget renewal - add extra tests * fix: add omitempty to renews_at in get_budget response --------- Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
- Loading branch information
Showing
19 changed files
with
446 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getAlby/hub/db/queries" | ||
"github.com/nbd-wtf/go-nostr" | ||
|
||
"github.com/getAlby/hub/db" | ||
"github.com/getAlby/hub/logger" | ||
"github.com/getAlby/hub/nip47/models" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type getBudgetResponse struct { | ||
UsedBudget uint64 `json:"used_budget"` | ||
TotalBudget uint64 `json:"total_budget"` | ||
RenewsAt *uint64 `json:"renews_at,omitempty"` | ||
RenewalPeriod string `json:"renewal_period"` | ||
} | ||
|
||
func (controller *nip47Controller) HandleGetBudgetEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) { | ||
|
||
logger.Logger.WithFields(logrus.Fields{ | ||
"request_event_id": requestEventId, | ||
}).Debug("Getting budget") | ||
|
||
appPermission := db.AppPermission{} | ||
controller.db.Where("app_id = ? AND scope = ?", app.ID, models.PAY_INVOICE_METHOD).First(&appPermission) | ||
|
||
maxAmount := appPermission.MaxAmountSat | ||
if maxAmount == 0 { | ||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: struct{}{}, | ||
}, nostr.Tags{}) | ||
return | ||
} | ||
|
||
usedBudget := queries.GetBudgetUsageSat(controller.db, &appPermission) | ||
responsePayload := &getBudgetResponse{ | ||
TotalBudget: uint64(maxAmount * 1000), | ||
UsedBudget: usedBudget * 1000, | ||
RenewalPeriod: appPermission.BudgetRenewal, | ||
RenewsAt: queries.GetBudgetRenewsAt(appPermission.BudgetRenewal), | ||
} | ||
|
||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: responsePayload, | ||
}, nostr.Tags{}) | ||
} |
Oops, something went wrong.