Skip to content

Commit 0e3b9ad

Browse files
committed
fix currency config
1 parent faaed11 commit 0e3b9ad

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

pkg/billing/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type RoutineConf struct {
2323
}
2424

2525
type CurrencyConf struct {
26-
Currency pb.Currency `json:"currency"` // Default currency for platform
26+
Currency *pb.Currency `json:"currency"` // Default currency for platform
2727
}
2828

2929
type RoundingConf struct {
@@ -63,7 +63,7 @@ var (
6363
}
6464
currencySetting = &sc.Setting[CurrencyConf]{
6565
Value: CurrencyConf{
66-
Currency: pb.Currency{
66+
Currency: &pb.Currency{
6767
Id: schema.DEFAULT_CURRENCY_ID,
6868
Title: schema.DEFAULT_CURRENCY_NAME,
6969
},
@@ -151,13 +151,14 @@ func MakeCurrencyConf(ctx context.Context, log *zap.Logger) (conf CurrencyConf)
151151
conf = currencySetting.Value
152152
}
153153

154+
log.Debug("Got currency config", zap.Any("conf", conf))
154155
return conf
155156
}
156157

157158
func MakeRoundingConf(ctx context.Context, log *zap.Logger) (conf RoundingConf) {
158159
sc.Setup(log, ctx, &settingsClient)
159160

160-
if err := sc.Fetch(currencyKey, &conf, roundingSetting); err != nil {
161+
if err := sc.Fetch(roundingKey, &conf, roundingSetting); err != nil {
161162
conf = roundingSetting.Value
162163
}
163164

pkg/billing/invoices.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (s *BillingServiceServer) CreateInvoice(ctx context.Context, req *connect.R
316316

317317
// Convert invoice's currency to default currency(according to how creating transaction works)
318318
defCurr := MakeCurrencyConf(ctx, log).Currency
319-
rate, _, err := s.currencies.GetExchangeRate(ctx, t.GetCurrency(), &defCurr)
319+
rate, _, err := s.currencies.GetExchangeRate(ctx, t.GetCurrency(), defCurr)
320320
if err != nil {
321321
log.Error("Failed to get exchange rate", zap.Error(err))
322322
return nil, status.Error(codes.Internal, "Failed to get exchange rate")
@@ -325,7 +325,7 @@ func (s *BillingServiceServer) CreateInvoice(ctx context.Context, req *connect.R
325325
newTr, err := s.CreateTransaction(ctx, connect.NewRequest(&pb.Transaction{
326326
Priority: pb.Priority_NORMAL,
327327
Account: t.GetAccount(),
328-
Currency: &defCurr,
328+
Currency: defCurr,
329329
Total: transactionTotal * rate,
330330
Exec: 0,
331331
}))
@@ -847,7 +847,7 @@ func (s *BillingServiceServer) UpdateInvoice(ctx context.Context, r *connect.Req
847847

848848
// Convert invoice's currency to default currency(according to how creating transaction works)
849849
defCurr := MakeCurrencyConf(ctx, log).Currency
850-
rate, _, err := s.currencies.GetExchangeRate(ctx, t.GetCurrency(), &defCurr)
850+
rate, _, err := s.currencies.GetExchangeRate(ctx, t.GetCurrency(), defCurr)
851851
if err != nil {
852852
log.Error("Failed to get exchange rate", zap.Error(err))
853853
return nil, status.Error(codes.Internal, "Failed to get exchange rate")
@@ -856,7 +856,7 @@ func (s *BillingServiceServer) UpdateInvoice(ctx context.Context, r *connect.Req
856856
newTr, err := s.CreateTransaction(ctx, connect.NewRequest(&pb.Transaction{
857857
Priority: pb.Priority_NORMAL,
858858
Account: t.GetAccount(),
859-
Currency: &defCurr,
859+
Currency: defCurr,
860860
Total: transactionTotal * rate,
861861
Exec: 0,
862862
}))

pkg/billing/routines.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ func (s *BillingServiceServer) InvoiceExpiringInstances(ctx context.Context, log
258258
log.Debug("Instance owner found", zap.String("account", acc.GetUuid()))
259259

260260
if acc.Currency == nil {
261-
acc.Currency = &currencyConf.Currency
261+
acc.Currency = currencyConf.Currency
262262
}
263-
rate, _, err := s.currencies.GetExchangeRate(ctx, &currencyConf.Currency, acc.Currency)
263+
rate, _, err := s.currencies.GetExchangeRate(ctx, currencyConf.Currency, acc.Currency)
264264
if err != nil {
265265
log.Error("Error getting exchange rate", zap.Error(err))
266266
continue
@@ -327,7 +327,7 @@ start:
327327
routineConf := MakeRoutineConf(ctx, log)
328328
roundingConf := MakeRoundingConf(ctx, log)
329329
currencyConf := MakeCurrencyConf(ctx, log)
330-
currencyConf.Currency = pb.Currency{
330+
currencyConf.Currency = &pb.Currency{
331331
Id: 0,
332332
Title: "NCU",
333333
}

pkg/billing/transactions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ func (s *BillingServiceServer) CreateTransaction(ctx context.Context, req *conne
299299
var cur *pb.Currency
300300

301301
if dbAcc.Currency == nil {
302-
cur = &currencyConf.Currency
302+
cur = currencyConf.Currency
303303
} else {
304304
cur = dbAcc.Currency
305305
}
306306

307307
var rate float64 = 1
308308

309309
if cur.GetId() != currencyConf.Currency.GetId() {
310-
rate, _, err = s.currencies.GetExchangeRate(ctx, cur, &currencyConf.Currency)
310+
rate, _, err = s.currencies.GetExchangeRate(ctx, cur, currencyConf.Currency)
311311
if err != nil {
312312
log.Error("Failed to get exchange rate", zap.String("err", err.Error()))
313313
return nil, status.Error(codes.Internal, err.Error())
@@ -515,15 +515,15 @@ func (s *BillingServiceServer) UpdateTransaction(ctx context.Context, r *connect
515515
var cur *pb.Currency
516516

517517
if dbAcc.Currency == nil {
518-
cur = &currencyConf.Currency
518+
cur = currencyConf.Currency
519519
} else {
520520
cur = dbAcc.Currency
521521
}
522522

523523
var rate float64 = 1
524524

525525
if cur.GetId() != currencyConf.Currency.GetId() {
526-
rate, _, err = s.currencies.GetExchangeRate(ctx, cur, &currencyConf.Currency)
526+
rate, _, err = s.currencies.GetExchangeRate(ctx, cur, currencyConf.Currency)
527527
if err != nil {
528528
log.Error("Failed to get exchange rate", zap.String("err", err.Error()))
529529
return nil, status.Error(codes.Internal, err.Error())

0 commit comments

Comments
 (0)