alby-hub/db/queries/get_budget_usage.go
Anshuman 1d3f9ecd41
feat: suffix balance/amount fields with explicit unit (Sat or Msat) (#2153)
* feat: add both Sat and Msat companion fields for all ambiguous balance/amount properties

* feat: add sat and msat companion fields to backend API responses

* chore: align frontend types and other missing fields

* chore: keep old formula for calculating total fee sat

* chore: remove unnecessary balance assignments in cashu and phoenix

* chore: add deprecated comment to non unit fields

* chore: rename callers and variable to specify units

* chore: remove msat fields for channel size and liquidity fields

* chore: drop msat fields from onchain channel size and liquidity responses

* chore: further changes

* chore: remove amount msat field onchain tx

* chore: remove msats from onchain balance response

* chore: remove msat fields for swaps

* chore: remove msat fields for punishment reserves

* chore: simplify rebalancing fee calculation

* chore: remove unnecessary fields

* chore: mark deprecated fields in frontend types

---------

Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
2026-04-17 13:53:28 +05:30

70 lines
2.2 KiB
Go

package queries
import (
"time"
"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"gorm.io/gorm"
)
func GetBudgetUsageMsat(tx *gorm.DB, appPermission *db.AppPermission) (uint64, error) {
var result struct {
Sum uint64
}
err := tx.
Table("transactions").
Select("SUM(amount_msat + fee_msat + fee_reserve_msat) as sum").
Where("app_id = ? AND type = ? AND (state = ? OR state = ?) AND created_at > ?", appPermission.AppId, constants.TRANSACTION_TYPE_OUTGOING, constants.TRANSACTION_STATE_SETTLED, constants.TRANSACTION_STATE_PENDING, getStartOfBudget(appPermission.BudgetRenewal)).Scan(&result).Error
if err != nil {
return 0, err
}
return result.Sum, nil
}
func getStartOfBudget(budget_type string) time.Time {
now := time.Now()
switch budget_type {
case constants.BUDGET_RENEWAL_DAILY:
// TODO: Use the location of the user, instead of the server
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
case constants.BUDGET_RENEWAL_WEEKLY:
weekday := now.Weekday()
var startOfWeek time.Time
if weekday == 0 {
startOfWeek = now.AddDate(0, 0, -6)
} else {
startOfWeek = now.AddDate(0, 0, -int(weekday)+1)
}
return time.Date(startOfWeek.Year(), startOfWeek.Month(), startOfWeek.Day(), 0, 0, 0, 0, startOfWeek.Location())
case constants.BUDGET_RENEWAL_MONTHLY:
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
case constants.BUDGET_RENEWAL_YEARLY:
return time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, now.Location())
default: //"never"
return time.Time{}
}
}
func GetBudgetRenewsAt(budgetRenewal string) *uint64 {
budgetStart := getStartOfBudget(budgetRenewal)
switch budgetRenewal {
case constants.BUDGET_RENEWAL_DAILY:
renewal := uint64(budgetStart.AddDate(0, 0, 1).Unix())
return &renewal
case constants.BUDGET_RENEWAL_WEEKLY:
renewal := uint64(budgetStart.AddDate(0, 0, 7).Unix())
return &renewal
case constants.BUDGET_RENEWAL_MONTHLY:
renewal := uint64(budgetStart.AddDate(0, 1, 0).Unix())
return &renewal
case constants.BUDGET_RENEWAL_YEARLY:
renewal := uint64(budgetStart.AddDate(1, 0, 0).Unix())
return &renewal
default: //"never"
return nil
}
}