Skip to content

Commit

Permalink
Update account fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hhassan01 committed Aug 30, 2024
1 parent df68555 commit 03b57e0
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/services/accountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export class AccountsService {
}

async createAccount(createAccountDto: CreateAccountDto): Promise<IAccount> {
this.initializeBalancesObject(createAccountDto)
const initialBalances = this.initializeBalancesObject(
createAccountDto.currencies,
)

const newAccount = new this.accountModel(createAccountDto)
const newAccount = new this.accountModel({
...createAccountDto,
balances: initialBalances,
})
await newAccount.save()

const { id, customerId, currencies, balances } = newAccount
Expand Down Expand Up @@ -52,8 +57,6 @@ export class AccountsService {
if (updateAccountDto.currencies) {
this.updateBalancesForNewCurrencies(account, updateAccountDto.currencies)
}

Object.assign(account, updateAccountDto)
const updatedAccount = await account.save()

const { id, customerId, currencies, balances } = updatedAccount
Expand All @@ -68,9 +71,11 @@ export class AccountsService {
return updatedAccount
}

private initializeBalancesObject(createAccountDto: CreateAccountDto) {
private initializeBalancesObject(currencies: CreateAccountDto['currencies']) {
const balances: IAccount['balances'] = new Map()
createAccountDto.currencies.forEach((currency) => (balances[currency] = 0))
currencies.forEach((currency) => balances.set(currency, 0))

return balances
}

private updateBalancesForNewCurrencies(
Expand Down

0 comments on commit 03b57e0

Please sign in to comment.