Skip to content

Commit 0ed24b6

Browse files
authored
Merge branch 'main' into docs
2 parents d64bf6c + d4b1a43 commit 0ed24b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7140
-5417
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
44
VERSION := $(shell git describe --tags ${TAG})
55
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui compose-up-dev
66
.DEFAULT_GOAL := build
7-
PROTON_COMMIT := "5e5f98bafba2a218b35a9130d75bd68980151d00"
7+
PROTON_COMMIT := "251281aa0c311904eb263eb9bfa7e3b9c41f99b0"
88

99
ui:
1010
@echo " > generating ui build"

billing/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ type AccountConfig struct {
3030
DefaultPlan string `yaml:"default_plan" mapstructure:"default_plan"`
3131
DefaultOffline bool `yaml:"default_offline" mapstructure:"default_offline"`
3232
OnboardCreditsWithOrg int64 `yaml:"onboard_credits_with_org" mapstructure:"onboard_credits_with_org"`
33+
34+
// CreditOverdraftProduct helps identify the product pricing per unit amount for the overdraft
35+
// credits being invoiced
36+
CreditOverdraftProduct string `yaml:"credit_overdraft_product" mapstructure:"credit_overdraft_product"`
37+
38+
// CreditOverdraftInvoiceDay is the day of the range(month) when the overdraft credits are invoiced
39+
CreditOverdraftInvoiceDay int `yaml:"credit_overdraft_invoice_day" mapstructure:"credit_overdraft_invoice_day" default:"1"`
40+
41+
// CreditOverdraftInvoiceRangeShift is the shift in the invoice range for the overdraft credits
42+
// if positive, the invoice range will be shifted to the future else it will be shifted to the past
43+
CreditOverdraftInvoiceRangeShift int `yaml:"credit_overdraft_invoice_range_shift" mapstructure:"credit_overdraft_invoice_range_shift" default:"0"`
3344
}
3445

3546
type PlanChangeConfig struct {

billing/credit/credit.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package credit
22

33
import (
44
"errors"
5+
"strings"
56
"time"
67

78
"github.com/google/uuid"
@@ -20,10 +21,11 @@ var (
2021
// TxNamespaceUUID is the namespace for generating transaction UUIDs deterministically
2122
TxNamespaceUUID = uuid.MustParse("967416d0-716e-4308-b58f-2468ac14f20a")
2223

23-
SourceSystemBuyEvent = "system.buy"
24-
SourceSystemAwardedEvent = "system.awarded"
25-
SourceSystemOnboardEvent = "system.starter"
26-
SourceSystemRevertEvent = "system.revert"
24+
SourceSystemBuyEvent = "system.buy"
25+
SourceSystemAwardedEvent = "system.awarded"
26+
SourceSystemOnboardEvent = "system.starter"
27+
SourceSystemRevertEvent = "system.revert"
28+
SourceSystemOverdraftEvent = "system.overdraft"
2729
)
2830

2931
type TransactionType string
@@ -71,3 +73,7 @@ type Filter struct {
7173
StartRange time.Time
7274
EndRange time.Time
7375
}
76+
77+
func TxUUID(tags ...string) string {
78+
return uuid.NewSHA1(TxNamespaceUUID, []byte(strings.Join(tags, ":"))).String()
79+
}

billing/credit/mocks/transaction_repository.go

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

billing/credit/service.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package credit
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/pkg/errors"
89

@@ -14,6 +15,7 @@ type TransactionRepository interface {
1415
GetBalance(ctx context.Context, id string) (int64, error)
1516
List(ctx context.Context, flt Filter) ([]Transaction, error)
1617
GetByID(ctx context.Context, id string) (Transaction, error)
18+
GetBalanceForRange(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error)
1719
}
1820

1921
type Service struct {
@@ -115,6 +117,12 @@ func (s Service) GetBalance(ctx context.Context, accountID string) (int64, error
115117
return s.transactionRepository.GetBalance(ctx, accountID)
116118
}
117119

120+
// GetBalanceForRange returns the balance for the given accountID within the given time range
121+
// start time is inclusive, end time is exclusive
122+
func (s Service) GetBalanceForRange(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error) {
123+
return s.transactionRepository.GetBalanceForRange(ctx, accountID, start, end)
124+
}
125+
118126
func (s Service) GetByID(ctx context.Context, id string) (Transaction, error) {
119127
return s.transactionRepository.GetByID(ctx, id)
120128
}

billing/customer/customer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ type Filter struct {
8282
OrgID string
8383
ProviderID string
8484
State State
85+
86+
Online *bool
87+
AllowedOverdraft *bool
8588
}
8689

8790
type PaymentMethod struct {

billing/invoice/invoice.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,42 @@ var (
1414
ErrInvalidDetail = fmt.Errorf("invalid invoice detail")
1515
)
1616

17+
const (
18+
// ItemIDMetadataKey is used to uniquely identify the item in the invoice
19+
// this is useful when reconciling the invoice items for payments and
20+
// avoid creating duplicate credits
21+
ItemIDMetadataKey = "item_id"
22+
// ItemTypeMetadataKey is used to identify the item type in the invoice
23+
// this is useful when reconciling the invoice items for payments and
24+
// avoid creating duplicate invoices
25+
ItemTypeMetadataKey = "item_type"
26+
27+
// ReconciledMetadataKey marks the invoice as reconciled and avoid processing it again
28+
// as an optimization
29+
ReconciledMetadataKey = "reconciled"
30+
31+
// GenerateForCreditLockKey is used to lock the invoice generation within current application
32+
GenerateForCreditLockKey = "generate_for_credit"
33+
)
34+
35+
type State string
36+
37+
func (s State) String() string {
38+
return string(s)
39+
}
40+
41+
const (
42+
DraftState State = "draft"
43+
OpenState State = "open"
44+
PaidState State = "paid"
45+
)
46+
1747
type Invoice struct {
18-
ID string
19-
CustomerID string
20-
ProviderID string
21-
State string
48+
ID string
49+
CustomerID string
50+
ProviderID string
51+
// State could be one of draft, open, paid, uncollectible, void
52+
State State
2253
Currency string
2354
Amount int64
2455
HostedURL string
@@ -28,12 +59,44 @@ type Invoice struct {
2859
PeriodStartAt time.Time
2960
PeriodEndAt time.Time
3061

62+
Items []Item
3163
Metadata metadata.Metadata
3264
}
3365

66+
type ItemType string
67+
68+
func (t ItemType) String() string {
69+
return string(t)
70+
}
71+
72+
const (
73+
// CreditItemType is used to charge for the credits used in the system
74+
// as overdraft
75+
CreditItemType ItemType = "credit"
76+
)
77+
78+
type Item struct {
79+
ID string `json:"id"`
80+
ProviderID string `json:"provider_id"`
81+
// Name is the item name
82+
Name string `json:"name"`
83+
// Type is the item type
84+
Type ItemType `json:"type"`
85+
// UnitAmount is per unit cost
86+
UnitAmount int64 `json:"unit_amount"`
87+
// Quantity is the number of units
88+
Quantity int64 `json:"quantity"`
89+
90+
// TimeRangeStart is the start time of the item since it's effective
91+
TimeRangeStart *time.Time `json:"range_start"`
92+
// TimeRangeEnd is the end time of the item since it's effective
93+
TimeRangeEnd *time.Time `json:"range_end"`
94+
}
95+
3496
type Filter struct {
3597
CustomerID string
3698
NonZeroOnly bool
99+
State State
37100

38101
Pagination *pagination.Pagination
39102
}

0 commit comments

Comments
 (0)