Skip to content

Commit

Permalink
chore: rename app permissions max amount field
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 12, 2024
1 parent 66a2795 commit 6827da5
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (api *api) UpdateApp(userApp *db.App, updateAppRequest *UpdateAppRequest) e
App: *userApp,
Scope: method,
ExpiresAt: expiresAt,
MaxAmount: int(maxAmount),
MaxAmountSat: int(maxAmount),
BudgetRenewal: budgetRenewal,
}
if err := tx.Create(&perm).Error; err != nil {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (api *api) GetApp(dbApp *db.App) *App {

//renewsIn := ""
budgetUsage := uint64(0)
maxAmount := uint64(paySpecificPermission.MaxAmount)
maxAmount := uint64(paySpecificPermission.MaxAmountSat)
if maxAmount > 0 {
budgetUsage = queries.GetBudgetUsageSat(api.db, &paySpecificPermission)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (api *api) ListApps() ([]App, error) {
apiApp.ExpiresAt = appPermission.ExpiresAt
if appPermission.Scope == constants.PAY_INVOICE_SCOPE {
apiApp.BudgetRenewal = appPermission.BudgetRenewal
apiApp.MaxAmountSat = uint64(appPermission.MaxAmount)
apiApp.MaxAmountSat = uint64(appPermission.MaxAmountSat)
if apiApp.MaxAmountSat > 0 {
apiApp.BudgetUsage = queries.GetBudgetUsageSat(api.db, &appPermission)
}
Expand Down
2 changes: 1 addition & 1 deletion db/db_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (svc *dbService) CreateApp(name string, pubkey string, maxAmountSat uint64,
Scope: scope,
ExpiresAt: expiresAt,
//these fields are only relevant for pay_invoice
MaxAmount: int(maxAmountSat),
MaxAmountSat: int(maxAmountSat),
BudgetRenewal: budgetRenewal,
}
err = tx.Create(&appPermission).Error
Expand Down
2 changes: 2 additions & 0 deletions db/migrations/202407012100_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ ALTER TABLE app_permissions ADD visibility string;
UPDATE app_permissions set balance_type = "full";
UPDATE app_permissions set visibility = "full";
ALTER TABLE app_permissions RENAME COLUMN max_amount TO max_amount_sat;
`).Error; err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type AppPermission struct {
AppId uint `validate:"required"`
App App
Scope string `validate:"required"`
MaxAmount int // TODO: rename to MaxAmountSat
MaxAmountSat int
BudgetRenewal string
ExpiresAt *time.Time
CreatedAt time.Time
Expand Down
2 changes: 1 addition & 1 deletion nip47/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (svc *permissionsService) HasPermission(app *db.App, scope string, amountMs
}

if scope == constants.PAY_INVOICE_SCOPE {
maxAmount := appPermission.MaxAmount
maxAmount := appPermission.MaxAmountSat
if maxAmount != 0 {
budgetUsage := queries.GetBudgetUsageSat(svc.db, &appPermission)

Expand Down
6 changes: 3 additions & 3 deletions nip47/permissions/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHasPermission_Expired(t *testing.T) {
AppId: app.ID,
App: *app,
Scope: PAY_INVOICE_SCOPE,

Check failure on line 41 in nip47/permissions/permissions_test.go

View workflow job for this annotation

GitHub Actions / build (x86_64, linux-amd64, x86_64-unknown-linux-gnu)

undefined: PAY_INVOICE_SCOPE

Check failure on line 41 in nip47/permissions/permissions_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: PAY_INVOICE_SCOPE

Check failure on line 41 in nip47/permissions/permissions_test.go

View workflow job for this annotation

GitHub Actions / build (aarch64, linux-aarch64, aarch64-unknown-linux-gnu)

undefined: PAY_INVOICE_SCOPE
MaxAmount: 100,
MaxAmountSat: 100,
BudgetRenewal: budgetRenewal,
ExpiresAt: &expiresAt,
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestHasPermission_Exceeded(t *testing.T) {
AppId: app.ID,
App: *app,
Scope: models.PAY_INVOICE_METHOD,
MaxAmount: 10,
MaxAmountSat: 10,
BudgetRenewal: budgetRenewal,
ExpiresAt: &expiresAt,
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestHasPermission_OK(t *testing.T) {
AppId: app.ID,
App: *app,
Scope: models.PAY_INVOICE_METHOD,
MaxAmount: 10,
MaxAmountSat: 10,
BudgetRenewal: budgetRenewal,
ExpiresAt: &expiresAt,
}
Expand Down
4 changes: 2 additions & 2 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ func (svc *transactionsService) validateCanPay(tx *gorm.DB, appId *uint, amount
}
}

if appPermission.MaxAmount > 0 {
if appPermission.MaxAmountSat > 0 {
budgetUsageSat := queries.GetBudgetUsageSat(tx, &appPermission)
if int(amountWithFeeReserve/1000) > appPermission.MaxAmount-int(budgetUsageSat) {
if int(amountWithFeeReserve/1000) > appPermission.MaxAmountSat-int(budgetUsageSat) {
return NewQuotaExceededError()
}
}
Expand Down

0 comments on commit 6827da5

Please sign in to comment.