Skip to content
Draft
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
19 changes: 17 additions & 2 deletions billing/invoice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (
// MonthCreditRangeForInvoice only month is supported for now, could be week or year
MonthCreditRangeForInvoice = "month"
CreditOverdraftDescription = "Invoice for the underpayment of credit utilization"
// DefaultCreditOverdraftItemName is the invoice line item description
// used when the overdraft product has no title
DefaultCreditOverdraftItemName = "Credit Overdraft"
)

type Repository interface {
Expand Down Expand Up @@ -80,6 +83,7 @@ type Service struct {

stripeAutoTax bool
creditOverdraftProduct string
creditOverdraftItemName string
creditOverdraftUnitAmount int64
creditOverdraftInvoiceCurrency string
creditOverdraftInvoiceDay int
Expand Down Expand Up @@ -135,9 +139,11 @@ func (s *Service) Init(ctx context.Context) error {
}
s.creditOverdraftInvoiceCurrency = creditPrice.Currency
s.creditOverdraftUnitAmount = int64(float64(creditPrice.Amount) / float64(creditProduct.Config.CreditAmount))
s.creditOverdraftItemName = creditProduct.Title
s.log.InfoContext(ctx, "credit overdraft product details",
"unit_amount", s.creditOverdraftUnitAmount,
"currency", s.creditOverdraftInvoiceCurrency)
"currency", s.creditOverdraftInvoiceCurrency,
"item_name", s.overdraftItemName())
}
return nil
}
Expand Down Expand Up @@ -529,7 +535,7 @@ func (s *Service) GenerateForCredits(ctx context.Context) error {
items := []Item{
{
ID: uuid.New().String(),
Name: "Credit Overdraft",
Name: s.overdraftItemName(),
Type: CreditItemType,
UnitAmount: s.creditOverdraftUnitAmount,
Quantity: abs(balance),
Expand Down Expand Up @@ -580,6 +586,15 @@ func computeOverdraftWindow(startRange time.Time, endRange time.Time, lastOverdr
return startRange, alreadyInvoiced
}

// overdraftItemName returns the description used on the credit overdraft
// invoice line item, the overdraft product title when available
func (s *Service) overdraftItemName() string {
if s.creditOverdraftItemName != "" {
return s.creditOverdraftItemName
}
return DefaultCreditOverdraftItemName
}

// getCreditOverdraftEndDate get range start and end, end date will be the current date with time set to 00:00:00
// start date will be the end date minus creditOverdraftRangeOfInvoice
func (s *Service) getCreditOverdraftEndDate(current time.Time) time.Time {
Expand Down
26 changes: 26 additions & 0 deletions billing/invoice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,29 @@ func TestService_getCreditOverdraftRange(t *testing.T) {
})
}
}

func TestService_overdraftItemName(t *testing.T) {
tests := []struct {
name string
itemName string
want string
}{
{
name: "uses the overdraft product title when set",
itemName: "For Satellite Images and Data Analytics Services",
want: "For Satellite Images and Data Analytics Services",
},
{
name: "falls back to default when product title is empty",
want: DefaultCreditOverdraftItemName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{creditOverdraftItemName: tt.itemName}
if got := s.overdraftItemName(); got != tt.want {
t.Errorf("overdraftItemName() = %v, want %v", got, tt.want)
}
})
}
}
Loading