diff --git a/finnow-api/source/transaction/service.d b/finnow-api/source/transaction/service.d index 34167b3..4aa995c 100644 --- a/finnow-api/source/transaction/service.d +++ b/finnow-api/source/transaction/service.d @@ -2,6 +2,7 @@ module transaction.service; import handy_http_primitives; import std.datetime; +import slf4d; import transaction.api; import transaction.model; @@ -88,7 +89,6 @@ TransactionDetail updateTransaction( TransactionCategoryRepository categoryRepo = ds.getTransactionCategoryRepository(); AccountRepository accountRepo = ds.getAccountRepository(); TransactionRepository transactionRepo = ds.getTransactionRepository(); - AccountJournalEntryRepository jeRepo = ds.getAccountJournalEntryRepository(); TransactionTagRepository tagRepo = ds.getTransactionTagRepository(); AttachmentRepository attachmentRepo = ds.getAttachmentRepository(); @@ -101,48 +101,70 @@ TransactionDetail updateTransaction( // Update the transaction: ds.doTransaction(() { TransactionDetail curr = transactionRepo.update(transactionId, payload); - bool amountOrCurrencyChanged = prev.amount != curr.amount || prev.currency.code != curr.currency.code; - bool updateCreditEntry = amountOrCurrencyChanged || ( - prev.creditedAccount != curr.creditedAccount - ); - bool updateDebitEntry = amountOrCurrencyChanged || ( - prev.debitedAccount != curr.debitedAccount - ); - - // Update journal entries if necessary: - if (updateCreditEntry && !prev.creditedAccount.isNull) { - jeRepo.deleteByAccountIdAndTransactionId(prev.creditedAccount.get.id, transactionId); - } - if (updateCreditEntry && !curr.creditedAccount.isNull) { - jeRepo.insert( - timestamp, - curr.creditedAccount.get.id, - transactionId, - curr.amount, - AccountJournalEntryType.CREDIT, - curr.currency - ); - } - if (updateDebitEntry && !prev.debitedAccount.isNull) { - jeRepo.deleteByAccountIdAndTransactionId(prev.debitedAccount.get.id, transactionId); - } - if (updateDebitEntry && !curr.debitedAccount.isNull) { - jeRepo.insert( - timestamp, - curr.debitedAccount.get.id, - transactionId, - curr.amount, - AccountJournalEntryType.DEBIT, - curr.currency - ); - } - + updateLinkedAccountJournalEntries(prev, curr, payload, ds, timestamp); tagRepo.updateTags(transactionId, payload.tags); updateAttachments(curr.id, timestamp, payload.attachmentIdsToRemove, files, attachmentRepo, transactionRepo); }); return getTransaction(ds, transactionId); } +private void updateLinkedAccountJournalEntries( + in TransactionDetail prev, + in TransactionDetail curr, + in AddTransactionPayload payload, + ProfileDataSource ds, + in SysTime timestamp +) { + AccountJournalEntryRepository jeRepo = ds.getAccountJournalEntryRepository(); + const bool amountOrCurrencyChanged = prev.amount != curr.amount || prev.currency.code != curr.currency.code; + const bool updateCreditEntry = amountOrCurrencyChanged || ( + (prev.creditedAccount.isNull && !payload.creditedAccountId.isNull) || + (!prev.creditedAccount.isNull && payload.creditedAccountId.isNull) || + ( + !prev.creditedAccount.isNull && + !payload.creditedAccountId.isNull && + prev.creditedAccount.get.id != payload.creditedAccountId.get + ) + ); + const bool updateDebitEntry = amountOrCurrencyChanged || ( + (prev.debitedAccount.isNull && !payload.creditedAccountId.isNull) || + (!prev.debitedAccount.isNull && payload.debitedAccountId.isNull) || + ( + !prev.debitedAccount.isNull && + !payload.debitedAccountId.isNull && + prev.debitedAccount.get.id != payload.debitedAccountId.get + ) + ); + + // Update journal entries if necessary: + if (updateCreditEntry && !prev.creditedAccount.isNull) { + jeRepo.deleteByAccountIdAndTransactionId(prev.creditedAccount.get.id, prev.id); + } + if (updateCreditEntry && !payload.creditedAccountId.isNull) { + jeRepo.insert( + timestamp, + payload.creditedAccountId.get, + curr.id, + curr.amount, + AccountJournalEntryType.CREDIT, + curr.currency + ); + } + if (updateDebitEntry && !prev.debitedAccount.isNull) { + jeRepo.deleteByAccountIdAndTransactionId(prev.debitedAccount.get.id, prev.id); + } + if (updateDebitEntry && !payload.debitedAccountId.isNull) { + jeRepo.insert( + timestamp, + payload.debitedAccountId.get, + curr.id, + curr.amount, + AccountJournalEntryType.DEBIT, + curr.currency + ); + } +} + void deleteTransaction(ProfileDataSource ds, ulong transactionId) { TransactionRepository txnRepo = ds.getTransactionRepository(); AttachmentRepository attachmentRepo = ds.getAttachmentRepository();