Browse Source

Remove redundant balance attribute from account

pull/7546/head
Thomas Kaul 3 weeks ago
parent
commit
43299b865f
  1. 20
      apps/api/src/app/account/account.service.ts
  2. 2
      libs/common/src/lib/dtos/create-account.dto.ts
  3. 7
      libs/common/src/lib/dtos/update-account.dto.ts

20
apps/api/src/app/account/account.service.ts

@ -25,7 +25,7 @@ import {
} from '@prisma/client'; } from '@prisma/client';
import { Big } from 'big.js'; import { Big } from 'big.js';
import { endOfToday, format } from 'date-fns'; import { endOfToday, format } from 'date-fns';
import { groupBy } from 'lodash'; import { groupBy, isNil } from 'lodash';
import { CashDetails } from './interfaces/cash-details.interface'; import { CashDetails } from './interfaces/cash-details.interface';
@ -189,7 +189,7 @@ export class AccountService {
} }
}); });
if (balance !== undefined) { if (!isNil(balance)) {
await this.accountBalanceService.createOrUpdateAccountBalance({ await this.accountBalanceService.createOrUpdateAccountBalance({
balance, balance,
userId, userId,
@ -311,7 +311,7 @@ export class AccountService {
userId, userId,
where where
}: { }: {
balance: number; balance?: number;
data: Prisma.AccountUpdateInput; data: Prisma.AccountUpdateInput;
tagIds?: string[]; tagIds?: string[];
userId: string; userId: string;
@ -336,12 +336,14 @@ export class AccountService {
where where
}); });
await this.accountBalanceService.createOrUpdateAccountBalance({ if (!isNil(balance)) {
balance, await this.accountBalanceService.createOrUpdateAccountBalance({
userId, balance,
accountId: account.id, userId,
date: format(new Date(), DATE_FORMAT) accountId: account.id,
}); date: format(new Date(), DATE_FORMAT)
});
}
this.eventEmitter.emit( this.eventEmitter.emit(
PortfolioChangedEvent.getName(), PortfolioChangedEvent.getName(),

2
libs/common/src/lib/dtos/create-account.dto.ts

@ -14,7 +14,7 @@ import { isString } from 'lodash';
export class CreateAccountDto { export class CreateAccountDto {
/** /**
* The initial balance, stored as the account balance of today. * 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() @IsNumber()
@IsOptional() @IsOptional()

7
libs/common/src/lib/dtos/update-account.dto.ts

@ -12,8 +12,13 @@ import {
import { isString } from 'lodash'; import { isString } from 'lodash';
export class UpdateAccountDto { export class UpdateAccountDto {
/**
* The balance, stored as the account balance of today.
* Optional because the account balances are the source of truth.
*/
@IsNumber() @IsNumber()
balance: number; @IsOptional()
balance?: number;
@IsOptional() @IsOptional()
@IsString() @IsString()

Loading…
Cancel
Save