From 4206ef0c4d1ec6288e4d16fc4b78b654933c8601 Mon Sep 17 00:00:00 2001 From: hhassan01 Date: Sat, 31 Aug 2024 00:12:18 +0500 Subject: [PATCH] Update account fix2 --- src/services/accountsService.ts | 3 +++ src/services/transactionsService.ts | 1 - src/utils/filterUndefined.ts | 11 +++++++++++ tests/handlers/createTransactions.test.ts | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/utils/filterUndefined.ts diff --git a/src/services/accountsService.ts b/src/services/accountsService.ts index 33a32ed..13b3f27 100644 --- a/src/services/accountsService.ts +++ b/src/services/accountsService.ts @@ -6,6 +6,7 @@ import { CreateAccountDto, UpdateAccountDto } from '../dto/account' import { NotFoundException } from '../utils/exceptions' import { SupportedCurrency } from '../constants/currencies' import { AwsEventBridgeService } from './awsEventBridgeService' +import { filterUndefined } from 'src/utils/filterUndefined' export class AccountsService { private accountModel: Model @@ -57,6 +58,8 @@ export class AccountsService { if (updateAccountDto.currencies) { this.updateBalancesForNewCurrencies(account, updateAccountDto.currencies) } + + Object.assign(account, filterUndefined(updateAccountDto)) const updatedAccount = await account.save() const { id, customerId, currencies, balances } = updatedAccount diff --git a/src/services/transactionsService.ts b/src/services/transactionsService.ts index 68b571d..f3a3308 100644 --- a/src/services/transactionsService.ts +++ b/src/services/transactionsService.ts @@ -34,7 +34,6 @@ export class TransactionsService { ) if (createTransactionDto.type === TransactionType.INBOUND) { - console.log('running inbound') account.balances.set( createTransactionDto.currency, (account.balances.get(createTransactionDto.currency) || 0) + diff --git a/src/utils/filterUndefined.ts b/src/utils/filterUndefined.ts new file mode 100644 index 0000000..fd0ce7a --- /dev/null +++ b/src/utils/filterUndefined.ts @@ -0,0 +1,11 @@ +export function filterUndefined(obj: Record): Record { + const result: Record = {} + + for (const key in obj) { + if (obj[key] !== undefined) { + result[key] = obj[key] + } + } + + return result +} diff --git a/tests/handlers/createTransactions.test.ts b/tests/handlers/createTransactions.test.ts index a85ac8b..862bf7e 100644 --- a/tests/handlers/createTransactions.test.ts +++ b/tests/handlers/createTransactions.test.ts @@ -39,7 +39,7 @@ describe('createTransactionHandler', () => { balances: new Map([['USD', 100]]), }) await account.save() - console.log({ account }) + const event = { body: JSON.stringify({ accountId: account._id.toString(),