Browse Source

Refactoring

pull/2552/head
Thomas 2 years ago
parent
commit
6fc1aca7da
  1. 40
      apps/api/src/app/account/account.controller.ts
  2. 1
      apps/client/src/app/components/accounts-table/accounts-table.component.html
  3. 13
      apps/client/src/app/pages/accounts/accounts-page.component.ts

40
apps/api/src/app/account/account.controller.ts

@ -190,54 +190,46 @@ export class AccountController {
this.request.user.id this.request.user.id
); );
const currentAccountIds = accountsOfUser.map(({ id }) => { const accountFrom = accountsOfUser.find(({ id }) => {
return id; return id === accountIdFrom;
}); });
const fromAccountBalance = accountsOfUser.find( const accountTo = accountsOfUser.find(({ id }) => {
(account) => account.id === accountIdFrom return id === accountIdTo;
).balance; });
if (fromAccountBalance < balance) { if (!accountFrom || !accountTo) {
throw new HttpException( throw new HttpException(
getReasonPhrase(StatusCodes.BAD_REQUEST), getReasonPhrase(StatusCodes.NOT_FOUND),
StatusCodes.BAD_REQUEST StatusCodes.NOT_FOUND
); );
} }
if (accountIdFrom === accountIdTo) { if (accountFrom.id === accountTo.id) {
throw new HttpException( throw new HttpException(
getReasonPhrase(StatusCodes.BAD_REQUEST), getReasonPhrase(StatusCodes.BAD_REQUEST),
StatusCodes.BAD_REQUEST StatusCodes.BAD_REQUEST
); );
} }
if ( if (accountFrom.balance < balance) {
![accountIdFrom, accountIdTo].every((accountId) => {
return currentAccountIds.includes(accountId);
})
) {
throw new HttpException( throw new HttpException(
getReasonPhrase(StatusCodes.NOT_FOUND), getReasonPhrase(StatusCodes.BAD_REQUEST),
StatusCodes.NOT_FOUND StatusCodes.BAD_REQUEST
); );
} }
const { currency } = accountsOfUser.find(({ id }) => {
return id === accountIdFrom;
});
await this.accountService.updateAccountBalance({ await this.accountService.updateAccountBalance({
currency, accountId: accountFrom.id,
accountId: accountIdFrom,
amount: -balance, amount: -balance,
currency: accountFrom.currency,
userId: this.request.user.id userId: this.request.user.id
}); });
await this.accountService.updateAccountBalance({ await this.accountService.updateAccountBalance({
currency, accountId: accountTo.id,
accountId: accountIdTo,
amount: balance, amount: balance,
currency: accountFrom.currency,
userId: this.request.user.id userId: this.request.user.id
}); });
} }

1
apps/client/src/app/components/accounts-table/accounts-table.component.html

@ -2,6 +2,7 @@
<button <button
class="align-items-center d-flex" class="align-items-center d-flex"
mat-stroked-button mat-stroked-button
[disabled]="dataSource?.data.length < 2"
(click)="onTransferBalance()" (click)="onTransferBalance()"
> >
<ion-icon class="mr-2" name="arrow-redo-outline"></ion-icon> <ion-icon class="mr-2" name="arrow-redo-outline"></ion-icon>

13
apps/client/src/app/pages/accounts/accounts-page.component.ts

@ -13,8 +13,8 @@ import { User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import { Account as AccountModel } from '@prisma/client'; import { Account as AccountModel } from '@prisma/client';
import { DeviceDetectorService } from 'ngx-device-detector'; import { DeviceDetectorService } from 'ngx-device-detector';
import { Subject, Subscription } from 'rxjs'; import { EMPTY, Subject, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators'; import { catchError, takeUntil } from 'rxjs/operators';
import { CreateOrUpdateAccountDialog } from './create-or-update-account-dialog/create-or-update-account-dialog.component'; import { CreateOrUpdateAccountDialog } from './create-or-update-account-dialog/create-or-update-account-dialog.component';
import { TransferBalanceDialog } from './transfer-balance/transfer-balance-dialog.component'; import { TransferBalanceDialog } from './transfer-balance/transfer-balance-dialog.component';
@ -300,7 +300,14 @@ export class AccountsPageComponent implements OnDestroy, OnInit {
accountIdTo, accountIdTo,
balance balance
}) })
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(
catchError(() => {
alert($localize`Oops, transfer cash balance has failed.`);
return EMPTY;
}),
takeUntil(this.unsubscribeSubject)
)
.subscribe(() => { .subscribe(() => {
this.fetchAccounts(); this.fetchAccounts();
}); });

Loading…
Cancel
Save