diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts index 4663e387b..3d0bb91bd 100644 --- a/apps/api/src/app/account/account.service.ts +++ b/apps/api/src/app/account/account.service.ts @@ -25,7 +25,7 @@ import { } from '@prisma/client'; import { Big } from 'big.js'; import { endOfToday, format } from 'date-fns'; -import { groupBy } from 'lodash'; +import { groupBy, isNil } from 'lodash'; import { CashDetails } from './interfaces/cash-details.interface'; @@ -189,7 +189,7 @@ export class AccountService { } }); - if (balance !== undefined) { + if (!isNil(balance)) { await this.accountBalanceService.createOrUpdateAccountBalance({ balance, userId, @@ -311,7 +311,7 @@ export class AccountService { userId, where }: { - balance: number; + balance?: number; data: Prisma.AccountUpdateInput; tagIds?: string[]; userId: string; @@ -336,12 +336,14 @@ export class AccountService { where }); - await this.accountBalanceService.createOrUpdateAccountBalance({ - balance, - userId, - accountId: account.id, - date: format(new Date(), DATE_FORMAT) - }); + if (!isNil(balance)) { + await this.accountBalanceService.createOrUpdateAccountBalance({ + balance, + userId, + accountId: account.id, + date: format(new Date(), DATE_FORMAT) + }); + } this.eventEmitter.emit( PortfolioChangedEvent.getName(), diff --git a/libs/common/src/lib/dtos/create-account.dto.ts b/libs/common/src/lib/dtos/create-account.dto.ts index d9e54e305..ccadff5f9 100644 --- a/libs/common/src/lib/dtos/create-account.dto.ts +++ b/libs/common/src/lib/dtos/create-account.dto.ts @@ -14,7 +14,7 @@ import { isString } from 'lodash'; export class CreateAccountDto { /** * The initial balance, stored as the account balance of today. - * Optional because the balance is derived from the account balances. + * Optional because callers may instead supply the full history via `balances`. */ @IsNumber() @IsOptional() diff --git a/libs/common/src/lib/dtos/update-account.dto.ts b/libs/common/src/lib/dtos/update-account.dto.ts index d8bfc7b8d..4e1570aad 100644 --- a/libs/common/src/lib/dtos/update-account.dto.ts +++ b/libs/common/src/lib/dtos/update-account.dto.ts @@ -12,8 +12,13 @@ import { import { isString } from 'lodash'; export class UpdateAccountDto { + /** + * The balance, stored as the account balance of today. + * Optional because the account balances are the source of truth. + */ @IsNumber() - balance: number; + @IsOptional() + balance?: number; @IsOptional() @IsString()