Skip to content

Commit

Permalink
[usage] Use credit cents when computing listed usage balance
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Sep 21, 2022
1 parent 39c5542 commit df7f44c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func (s *UsageService) ListUsage(ctx context.Context, in *v1.ListUsageRequest) (

return &v1.ListUsageResponse{
UsageEntries: usageData,
CreditBalanceAtStart: float64(usageSummary.CreditCentsBalanceAtStart) / 100,
CreditBalanceAtEnd: float64(usageSummary.CreditCentsBalanceAtEnd) / 100,
CreditBalanceAtStart: usageSummary.CreditCentsBalanceAtStart.ToCredits(),
CreditBalanceAtEnd: usageSummary.CreditCentsBalanceAtEnd.ToCredits(),
Pagination: &pagination,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions components/usage/pkg/db/cost_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ func (c *CostCenterManager) ComputeInvoiceUsageRecord(ctx context.Context, attri
if err != nil {
return nil, err
}
if summary.CreditCentsBalanceAtEnd <= int64(0) {
// account has not debt, do nothing
if summary.CreditCentsBalanceAtEnd.ToCredits() <= 0 {
// account has no debt, do nothing
return nil, nil
}
return &Usage{
ID: uuid.New(),
AttributionID: attributionID,
Description: "Credits",
CreditCents: CreditCents(summary.CreditCentsBalanceAtEnd * -1),
CreditCents: summary.CreditCentsBalanceAtEnd * -1,
EffectiveTime: NewVarcharTime(now),
Kind: InvoiceUsageKind,
Draft: false,
Expand Down
8 changes: 4 additions & 4 deletions components/usage/pkg/db/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func FindUsage(ctx context.Context, conn *gorm.DB, params *FindUsageParams) ([]U

type UsageSummary struct {
NumRecordsInRange int
CreditCentsBalanceAtStart int64
CreditCentsBalanceAtEnd int64
CreditCentsBalanceAtStart CreditCents
CreditCentsBalanceAtEnd CreditCents
}

func GetUsageSummary(ctx context.Context, conn *gorm.DB, attributionId AttributionID, from, to time.Time, excludeDrafts bool) (*UsageSummary, error) {
Expand Down Expand Up @@ -197,8 +197,8 @@ func GetUsageSummary(ctx context.Context, conn *gorm.DB, attributionId Attributi
}
return &UsageSummary{
NumRecordsInRange: int(numRecordsInRange.Int32),
CreditCentsBalanceAtStart: creditCentsBalanceAtStart.Int64,
CreditCentsBalanceAtEnd: creditCentsBalanceAtStart.Int64 + creditCentsBalanceInPeriod.Int64,
CreditCentsBalanceAtStart: CreditCents(creditCentsBalanceAtStart.Int64),
CreditCentsBalanceAtEnd: CreditCents(creditCentsBalanceAtStart.Int64 + creditCentsBalanceInPeriod.Int64),
}, nil
}

Expand Down

0 comments on commit df7f44c

Please sign in to comment.