Skip to content

Commit 357edc1

Browse files
authored
fix: rule processing (#89)
1 parent cd3339d commit 357edc1

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

pkg/transactions/base_amount_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ func TestBaseAmountService(t *testing.T) {
7373
Type: gomoneypbv1.AccountType_ACCOUNT_TYPE_EXPENSE,
7474
Currency: "RAND",
7575
},
76+
{
77+
Name: "Default Income",
78+
Extra: map[string]string{},
79+
Type: gomoneypbv1.AccountType_ACCOUNT_TYPE_INCOME,
80+
Currency: baseCurrency,
81+
},
7682
}
7783
assert.NoError(t, gormDB.Create(&acc).Error)
7884

@@ -237,6 +243,17 @@ func TestBaseAmountService(t *testing.T) {
237243
// source base
238244
// dest different
239245
},
246+
{
247+
TransactionType: gomoneypbv1.TransactionType_TRANSACTION_TYPE_INCOME,
248+
SourceCurrency: baseCurrency,
249+
SourceAmount: decimal.NewNullDecimal(decimal.NewFromInt(1000)),
250+
SourceAccountID: acc[6].ID,
251+
252+
DestinationCurrency: baseCurrency,
253+
DestinationAccountID: acc[1].ID,
254+
DestinationAmount: decimal.NewNullDecimal(decimal.NewFromInt(1000)),
255+
Extra: make(map[string]string),
256+
},
240257
}
241258

242259
txSrv := validation.NewValidationService(&validation.ServiceConfig{})
@@ -289,6 +306,9 @@ func TestBaseAmountService(t *testing.T) {
289306

290307
assert.EqualValues(t, -55, updatedTxs[9].SourceAmountInBaseCurrency.Decimal.IntPart())
291308
assert.EqualValues(t, 55, updatedTxs[9].DestinationAmountInBaseCurrency.Decimal.IntPart())
309+
310+
assert.EqualValues(t, 1000, updatedTxs[10].DestinationAmountInBaseCurrency.Decimal.IntPart())
311+
assert.EqualValues(t, -1000, updatedTxs[10].SourceAmountInBaseCurrency.Decimal.IntPart())
292312
})
293313

294314
t.Run("success with partial update", func(t *testing.T) {

pkg/transactions/integration_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,75 @@ func TestUpdateTransaction(t *testing.T) {
321321
assert.EqualValues(t, "-20.5", updatedAccounts[1].CurrentBalance.String())
322322
}
323323

324+
func TestBasicIncome(t *testing.T) {
325+
assert.NoError(t, testingutils.FlushAllTables(cfg.Db))
326+
327+
statsSvc := transactions.NewStatService()
328+
mapper := NewMockMapperSvc(gomock.NewController(t))
329+
330+
mapper.EXPECT().MapTransaction(gomock.Any(), gomock.Any()).
331+
Return(&gomoneypbv1.Transaction{})
332+
333+
ruleEngine := NewMockRuleSvc(gomock.NewController(t))
334+
ruleEngine.EXPECT().ProcessTransactions(gomock.Any(), gomock.Any()).
335+
DoAndReturn(func(ctx context.Context, i []*database.Transaction) ([]*database.Transaction, error) {
336+
assert.Len(t, i, 1)
337+
return i, nil
338+
})
339+
340+
baseCurrency := transactions.NewBaseAmountService("USD")
341+
accountSvc := NewMockAccountSvc(gomock.NewController(t))
342+
validationSvc := NewMockValidationSvc(gomock.NewController(t))
343+
doubleEntry := NewMockDoubleEntrySvc(gomock.NewController(t))
344+
345+
srv := transactions.NewService(&transactions.ServiceConfig{
346+
StatsSvc: statsSvc,
347+
MapperSvc: mapper,
348+
BaseAmountService: baseCurrency,
349+
RuleSvc: ruleEngine,
350+
AccountSvc: accountSvc,
351+
ValidationSvc: validationSvc,
352+
DoubleEntry: doubleEntry,
353+
})
354+
355+
accounts := []*database.Account{
356+
{
357+
Name: "Asset 1",
358+
Currency: "USD",
359+
Extra: map[string]string{},
360+
},
361+
{
362+
Name: "Income 1",
363+
Currency: "USD",
364+
Extra: map[string]string{},
365+
},
366+
}
367+
assert.NoError(t, gormDB.Create(&accounts).Error)
368+
369+
accountSvc.EXPECT().GetAllAccounts(gomock.Any()).Return(accounts, nil)
370+
validationSvc.EXPECT().Validate(gomock.Any(), gomock.Any(), gomock.Any()).
371+
Return(nil)
372+
doubleEntry.EXPECT().Record(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
373+
Return(nil)
374+
375+
depositDate2 := time.Date(2025, 6, 7, 0, 0, 0, 0, time.UTC)
376+
_, err := srv.Create(context.TODO(), &transactionsv1.CreateTransactionRequest{
377+
TransactionDate: timestamppb.New(depositDate2),
378+
Transaction: &transactionsv1.CreateTransactionRequest_Income{
379+
Income: &transactionsv1.Income{
380+
DestinationAmount: "100",
381+
DestinationCurrency: "USD",
382+
DestinationAccountId: accounts[0].ID,
383+
384+
SourceAmount: "-100",
385+
SourceCurrency: "USD",
386+
SourceAccountId: accounts[1].ID,
387+
},
388+
},
389+
})
390+
assert.NoError(t, err)
391+
}
392+
324393
func TestBasicCalc(t *testing.T) {
325394
assert.NoError(t, testingutils.FlushAllTables(cfg.Db))
326395

pkg/transactions/service.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@ func (s *Service) CreateBulkInternal(
321321

322322
var originalTxs []*database.Transaction
323323

324-
var toCreate []*database.Transaction
325-
var toUpdate []*database.Transaction
326-
327324
for _, req := range reqs {
328325
if req.OriginalTx != nil { // save list of original transactions for update
329326
originalTxs = append(originalTxs, req.OriginalTx)
@@ -340,12 +337,6 @@ func (s *Service) CreateBulkInternal(
340337

341338
// validate wallet transaction date
342339

343-
if req.OriginalTx == nil {
344-
toCreate = append(toCreate, newTx)
345-
} else {
346-
toUpdate = append(toUpdate, newTx)
347-
}
348-
349340
if req.Req.SkipRules {
350341
transactionWithoutRules = append(transactionWithoutRules, newTx)
351342
} else {
@@ -362,6 +353,17 @@ func (s *Service) CreateBulkInternal(
362353
transactionWithRules = modifiedTxs
363354
}
364355

356+
var toCreate []*database.Transaction
357+
var toUpdate []*database.Transaction
358+
359+
for _, newTx := range append(transactionWithRules, transactionWithoutRules...) {
360+
if newTx.ID == 0 {
361+
toCreate = append(toCreate, newTx)
362+
} else {
363+
toUpdate = append(toUpdate, newTx)
364+
}
365+
}
366+
365367
zerolog.Ctx(ctx).Info().
366368
Int("to_create", len(toCreate)).
367369
Int("to_update", len(toUpdate)).

0 commit comments

Comments
 (0)