Skip to content

Commit 34eaaec

Browse files
authored
feat(billing): use credit product title as overdraft invoice line item name (#1753)
1 parent 3616f6a commit 34eaaec

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎billing/invoice/service.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const (
3333
// MonthCreditRangeForInvoice only month is supported for now, could be week or year
3434
MonthCreditRangeForInvoice = "month"
3535
CreditOverdraftDescription = "Invoice for the underpayment of credit utilization"
36+
// DefaultCreditOverdraftItemName is the invoice line item description
37+
// used when the overdraft product has no title
38+
DefaultCreditOverdraftItemName = "Credit Overdraft"
3639
)
3740

3841
type Repository interface {
@@ -80,6 +83,7 @@ type Service struct {
8083

8184
stripeAutoTax bool
8285
creditOverdraftProduct string
86+
creditOverdraftItemName string
8387
creditOverdraftUnitAmount int64
8488
creditOverdraftInvoiceCurrency string
8589
creditOverdraftInvoiceDay int
@@ -135,9 +139,11 @@ func (s *Service) Init(ctx context.Context) error {
135139
}
136140
s.creditOverdraftInvoiceCurrency = creditPrice.Currency
137141
s.creditOverdraftUnitAmount = int64(float64(creditPrice.Amount) / float64(creditProduct.Config.CreditAmount))
142+
s.creditOverdraftItemName = creditProduct.Title
138143
s.log.InfoContext(ctx, "credit overdraft product details",
139144
"unit_amount", s.creditOverdraftUnitAmount,
140-
"currency", s.creditOverdraftInvoiceCurrency)
145+
"currency", s.creditOverdraftInvoiceCurrency,
146+
"item_name", s.overdraftItemName())
141147
}
142148
return nil
143149
}
@@ -529,7 +535,7 @@ func (s *Service) GenerateForCredits(ctx context.Context) error {
529535
items := []Item{
530536
{
531537
ID: uuid.New().String(),
532-
Name: "Credit Overdraft",
538+
Name: s.overdraftItemName(),
533539
Type: CreditItemType,
534540
UnitAmount: s.creditOverdraftUnitAmount,
535541
Quantity: abs(balance),
@@ -580,6 +586,15 @@ func computeOverdraftWindow(startRange time.Time, endRange time.Time, lastOverdr
580586
return startRange, alreadyInvoiced
581587
}
582588

589+
// overdraftItemName returns the description used on the credit overdraft
590+
// invoice line item, the overdraft product title when available
591+
func (s *Service) overdraftItemName() string {
592+
if s.creditOverdraftItemName != "" {
593+
return s.creditOverdraftItemName
594+
}
595+
return DefaultCreditOverdraftItemName
596+
}
597+
583598
// getCreditOverdraftEndDate get range start and end, end date will be the current date with time set to 00:00:00
584599
// start date will be the end date minus creditOverdraftRangeOfInvoice
585600
func (s *Service) getCreditOverdraftEndDate(current time.Time) time.Time {

‎billing/invoice/service_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,29 @@ func TestService_getCreditOverdraftRange(t *testing.T) {
150150
})
151151
}
152152
}
153+
154+
func TestService_overdraftItemName(t *testing.T) {
155+
tests := []struct {
156+
name string
157+
itemName string
158+
want string
159+
}{
160+
{
161+
name: "uses the overdraft product title when set",
162+
itemName: "For Satellite Images and Data Analytics Services",
163+
want: "For Satellite Images and Data Analytics Services",
164+
},
165+
{
166+
name: "falls back to default when product title is empty",
167+
want: DefaultCreditOverdraftItemName,
168+
},
169+
}
170+
for _, tt := range tests {
171+
t.Run(tt.name, func(t *testing.T) {
172+
s := &Service{creditOverdraftItemName: tt.itemName}
173+
if got := s.overdraftItemName(); got != tt.want {
174+
t.Errorf("overdraftItemName() = %v, want %v", got, tt.want)
175+
}
176+
})
177+
}
178+
}

0 commit comments

Comments
 (0)