Skip to content
Merged
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
59 changes: 59 additions & 0 deletions billing/credit/mocks/transaction_repository.go

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

7 changes: 7 additions & 0 deletions billing/credit/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type TransactionRepository interface {
List(ctx context.Context, flt Filter) ([]Transaction, error)
GetByID(ctx context.Context, id string) (Transaction, error)
GetBalanceForRange(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error)
GetBalanceForRangeWithoutOverdraft(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error)
}

type CustomerRepository interface {
Expand Down Expand Up @@ -164,6 +165,12 @@ func (s Service) GetBalanceForRange(ctx context.Context, accountID string, start
return s.transactionRepository.GetBalanceForRange(ctx, accountID, start, end)
}

// GetBalanceForRangeWithoutOverdraft returns the balance for the given accountID within the given time range
// excluding credit transactions sourced from overdraft reconciliation
func (s Service) GetBalanceForRangeWithoutOverdraft(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error) {
return s.transactionRepository.GetBalanceForRangeWithoutOverdraft(ctx, accountID, start, end)
}

func (s Service) GetByID(ctx context.Context, id string) (Transaction, error) {
return s.transactionRepository.GetByID(ctx, id)
}
Expand Down
3 changes: 2 additions & 1 deletion billing/invoice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type CustomerService interface {
type CreditService interface {
Add(ctx context.Context, cred credit.Credit) error
GetBalanceForRange(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error)
GetBalanceForRangeWithoutOverdraft(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error)
}

type ProductService interface {
Expand Down Expand Up @@ -522,7 +523,7 @@ func (s *Service) GenerateForCredits(ctx context.Context) error {
continue
}

balance, err := s.creditService.GetBalanceForRange(ctx, c.ID, startRange, endRange)
balance, err := s.creditService.GetBalanceForRangeWithoutOverdraft(ctx, c.ID, startRange, endRange)
if err != nil {
errs = append(errs, fmt.Errorf("failed to get balance for customer %s: %w", c.ID, err))
continue
Expand Down
61 changes: 61 additions & 0 deletions internal/store/postgres/billing_transactions_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,64 @@ func (r BillingTransactionRepository) GetBalanceForRange(ctx context.Context, ac
}
return amount, nil
}

func (r BillingTransactionRepository) getCreditBalanceExcludingSource(ctx context.Context, tx *sqlx.Tx, accountID string,
start *time.Time, end *time.Time, excludeSource string) (*int64, error) {
stmt := dialect.Select(goqu.SUM("amount")).From(TABLE_BILLING_TRANSACTIONS).Where(goqu.Ex{
"account_id": accountID,
"type": credit.CreditType,
}).Where(goqu.C("source").Neq(excludeSource))
if start != nil {
stmt = stmt.Where(goqu.Ex{
"created_at": goqu.Op{"gte": *start},
})
}
if end != nil {
stmt = stmt.Where(goqu.Ex{
"created_at": goqu.Op{"lt": *end},
})
}
query, params, err := stmt.ToSQL()
if err != nil {
return nil, fmt.Errorf("%w: %s", parseErr, err)
}

var creditBalance *int64
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_TRANSACTIONS, "GetCreditBalanceExcludingSource", func(ctx context.Context) error {
return tx.QueryRowxContext(ctx, query, params...).Scan(&creditBalance)
}); err != nil {
return nil, fmt.Errorf("%w: %s", dbErr, err)
}
return creditBalance, nil
}

// GetBalanceForRangeWithoutOverdraft returns the balance excluding credit transactions
// with source 'system.overdraft'. This prevents reconciliation credits from inflating
// the balance when calculating credit overdraft invoices.
func (r BillingTransactionRepository) GetBalanceForRangeWithoutOverdraft(ctx context.Context, accountID string, start time.Time,
end time.Time) (int64, error) {
var amount int64
if err := r.dbc.WithTxn(ctx, sql.TxOptions{
Isolation: sql.LevelSerializable,
}, func(tx *sqlx.Tx) error {
debitBalance, err := r.getDebitBalance(ctx, tx, accountID, &start, &end)
if err != nil {
return fmt.Errorf("failed to get debit balance: %w", err)
}
creditBalance, err := r.getCreditBalanceExcludingSource(ctx, tx, accountID, &start, &end, credit.SourceSystemOverdraftEvent)
if err != nil {
return fmt.Errorf("failed to get credit balance: %w", err)
}
if creditBalance == nil {
creditBalance = new(int64)
}
if debitBalance == nil {
debitBalance = new(int64)
}
amount = *creditBalance - *debitBalance
return nil
}); err != nil {
return 0, fmt.Errorf("failed to get balance: %w", err)
}
return amount, nil
}
Loading