Skip to content

accounts: Add CreditAccount & DebitAccount sql queries #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions accounts/store_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
//nolint:lll
type SQLQueries interface {
AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
CreditAccount(ctx context.Context, arg sqlc.CreditAccountParams) (int64, error)
DebitAccount(ctx context.Context, arg sqlc.DebitAccountParams) (int64, error)
DeleteAccount(ctx context.Context, id int64) error
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
GetAccount(ctx context.Context, id int64) (sqlc.Account, error)
Expand Down Expand Up @@ -394,17 +396,10 @@ func (s *SQLStore) CreditAccount(ctx context.Context, alias AccountID,
return err
}

acct, err := db.GetAccount(ctx, id)
if err != nil {
return err
}

newBalance := acct.CurrentBalanceMsat + int64(amount)

_, err = db.UpdateAccountBalance(
ctx, sqlc.UpdateAccountBalanceParams{
ID: id,
CurrentBalanceMsat: newBalance,
_, err = db.CreditAccount(
ctx, sqlc.CreditAccountParams{
ID: id,
Amount: int64(amount),
},
)
if err != nil {
Expand All @@ -429,26 +424,17 @@ func (s *SQLStore) DebitAccount(ctx context.Context, alias AccountID,
return err
}

acct, err := db.GetAccount(ctx, id)
if err != nil {
return err
}

if acct.CurrentBalanceMsat-int64(amount) < 0 {
id, err = db.DebitAccount(
ctx, sqlc.DebitAccountParams{
ID: id,
Amount: int64(amount),
},
)
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("cannot debit %v from the account "+
"balance, as the resulting balance would be "+
"below 0", int64(amount/1000))
}

newBalance := acct.CurrentBalanceMsat - int64(amount)

_, err = db.UpdateAccountBalance(
ctx, sqlc.UpdateAccountBalanceParams{
ID: id,
CurrentBalanceMsat: newBalance,
},
)
if err != nil {
} else if err != nil {
return err
}

Expand Down
39 changes: 39 additions & 0 deletions db/sqlc/accounts.sql.go

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

2 changes: 2 additions & 0 deletions db/sqlc/querier.go

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

13 changes: 13 additions & 0 deletions db/sqlc/queries/accounts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ SET current_balance_msat = $1
WHERE id = $2
RETURNING id;

-- name: CreditAccount :one
UPDATE accounts
SET current_balance_msat = current_balance_msat + sqlc.arg(amount)
WHERE id = $1
RETURNING id;

-- name: DebitAccount :one
UPDATE accounts
SET current_balance_msat = current_balance_msat - sqlc.arg(amount)
WHERE id = $1
AND current_balance_msat >= sqlc.arg(amount)
RETURNING id;

-- name: UpdateAccountExpiry :one
UPDATE accounts
SET expiration = $1
Expand Down
Loading