-
Notifications
You must be signed in to change notification settings - Fork 32
/
service.go
132 lines (115 loc) · 3.41 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package credit
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/raystack/frontier/internal/bootstrap/schema"
)
type TransactionRepository interface {
CreateEntry(ctx context.Context, debit, credit Transaction) ([]Transaction, error)
GetBalance(ctx context.Context, id string) (int64, error)
List(ctx context.Context, flt Filter) ([]Transaction, error)
GetByID(ctx context.Context, id string) (Transaction, error)
}
type Service struct {
transactionRepository TransactionRepository
}
func NewService(repository TransactionRepository) *Service {
return &Service{
transactionRepository: repository,
}
}
func (s Service) Add(ctx context.Context, cred Credit) error {
if cred.ID == "" {
return fmt.Errorf("credit id is empty, it is required to create a transaction")
}
if cred.Amount < 0 {
return fmt.Errorf("credit amount is negative")
}
// check if already credited
t, err := s.transactionRepository.GetByID(ctx, cred.ID)
if err != nil && !errors.Is(err, ErrNotFound) {
return err
}
if err == nil && t.ID != "" {
return ErrAlreadyApplied
}
txSource := "system"
if cred.Source != "" {
txSource = cred.Source
}
_, err = s.transactionRepository.CreateEntry(ctx, Transaction{
CustomerID: schema.PlatformOrgID.String(),
Type: DebitType,
Amount: cred.Amount,
Description: cred.Description,
Source: txSource,
UserID: cred.UserID,
Metadata: cred.Metadata,
}, Transaction{
ID: cred.ID,
Type: CreditType,
CustomerID: cred.CustomerID,
Amount: cred.Amount,
Description: cred.Description,
Source: txSource,
UserID: cred.UserID,
Metadata: cred.Metadata,
})
if err != nil {
return fmt.Errorf("transactionRepository.CreateEntry: %w", err)
}
return nil
}
func (s Service) Deduct(ctx context.Context, cred Credit) error {
if cred.ID == "" {
return fmt.Errorf("credit id is empty, it is required to create a transaction")
}
if cred.Amount < 0 {
return fmt.Errorf("credit amount is negative")
}
// check balance, if enough, sub credits
currentBalance, err := s.GetBalance(ctx, cred.CustomerID)
if err != nil {
return fmt.Errorf("failed to apply transaction: %w", err)
}
// TODO(kushsharma): this is prone to timing attacks and better we do this
// in a transaction
if currentBalance < cred.Amount {
return ErrInsufficientCredits
}
txSource := "system"
if cred.Source != "" {
txSource = cred.Source
}
if _, err := s.transactionRepository.CreateEntry(ctx, Transaction{
ID: cred.ID,
CustomerID: cred.CustomerID,
Type: DebitType,
Amount: cred.Amount,
Description: cred.Description,
Source: txSource,
UserID: cred.UserID,
Metadata: cred.Metadata,
}, Transaction{
Type: CreditType,
CustomerID: schema.PlatformOrgID.String(),
Amount: cred.Amount,
Description: cred.Description,
Source: txSource,
UserID: cred.UserID,
Metadata: cred.Metadata,
}); err != nil {
return fmt.Errorf("failed to deduct credits: %w", err)
}
return nil
}
func (s Service) List(ctx context.Context, flt Filter) ([]Transaction, error) {
return s.transactionRepository.List(ctx, flt)
}
func (s Service) GetBalance(ctx context.Context, accountID string) (int64, error) {
return s.transactionRepository.GetBalance(ctx, accountID)
}
func (s Service) GetByID(ctx context.Context, id string) (Transaction, error) {
return s.transactionRepository.GetByID(ctx, id)
}