diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts index 424d375be..05d0c11de 100644 --- a/apps/api/src/app/account/account.controller.ts +++ b/apps/api/src/app/account/account.controller.ts @@ -29,9 +29,8 @@ import { StatusCodes, getReasonPhrase } from 'http-status-codes'; import { AccountService } from './account.service'; import { CreateAccountDto } from './create-account.dto'; -import { UpdateAccountDto } from './update-account.dto'; import { TransferBalanceDto } from './transfer-balance.dto'; -import { resetHours } from '@ghostfolio/common/helper'; +import { UpdateAccountDto } from './update-account.dto'; @Controller('account') export class AccountController { @@ -170,14 +169,18 @@ export class AccountController { ); } - const currentAccountIds = ( - await this.accountService.getAccounts(this.request.user.id) - ).map((account) => account.id); + const accountsOfUser = await this.accountService.getAccounts( + this.request.user.id + ); + + const currentAccountIds = accountsOfUser.map(({ id }) => { + return id; + }); if ( - ![accountIdFrom, accountIdTo].every((id) => - currentAccountIds.includes(id) - ) + ![accountIdFrom, accountIdTo].every((accountId) => { + return currentAccountIds.includes(accountId); + }) ) { throw new HttpException( getReasonPhrase(StatusCodes.NOT_FOUND), @@ -185,22 +188,21 @@ export class AccountController { ); } - const today = resetHours(new Date()); - const userCurrency = this.request.user.Settings.settings.baseCurrency; + const { currency } = accountsOfUser.find(({ id }) => { + return id === accountIdFrom; + }); await this.accountService.updateAccountBalance({ + currency, accountId: accountIdFrom, amount: -balance, - currency: userCurrency, - date: today, userId: this.request.user.id }); await this.accountService.updateAccountBalance({ + currency, accountId: accountIdTo, amount: balance, - currency: userCurrency, - date: today, userId: this.request.user.id }); } diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts index dc049108c..bc6abcc7a 100644 --- a/apps/api/src/app/account/account.service.ts +++ b/apps/api/src/app/account/account.service.ts @@ -109,7 +109,7 @@ export class AccountService { }); } - public async getAccounts(aUserId: string) { + public async getAccounts(aUserId: string): Promise { const accounts = await this.accounts({ include: { Order: true, Platform: true }, orderBy: { name: 'asc' }, @@ -218,13 +218,13 @@ export class AccountService { accountId, amount, currency, - date, + date = new Date(), userId }: { accountId: string; amount: number; currency: string; - date: Date; + date?: Date; userId: string; }) { const { balance, currency: currencyOfAccount } = await this.account({ diff --git a/apps/client/src/app/pages/accounts/accounts-page.component.ts b/apps/client/src/app/pages/accounts/accounts-page.component.ts index 40b72368e..882f9b3f6 100644 --- a/apps/client/src/app/pages/accounts/accounts-page.component.ts +++ b/apps/client/src/app/pages/accounts/accounts-page.component.ts @@ -296,16 +296,18 @@ export class AccountsPageComponent implements OnDestroy, OnInit { data?.account; this.dataService - .postTransferAccountBalance({ + .transferAccountBalance({ accountIdFrom, accountIdTo, balance }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe(() => { - this.router.navigate(['.'], { relativeTo: this.route }); + this.fetchAccounts(); }); } + + this.router.navigate(['.'], { relativeTo: this.route }); }); } } diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts index 9ec847f0a..f5cafb57a 100644 --- a/apps/client/src/app/services/data.service.ts +++ b/apps/client/src/app/services/data.service.ts @@ -480,18 +480,6 @@ export class DataService { return this.http.post(`/api/v1/order`, aOrder); } - public postTransferAccountBalance({ - accountIdFrom, - accountIdTo, - balance - }: TransferBalanceDto) { - return this.http.post('/api/v1/account/transfer-balance', { - accountIdFrom, - accountIdTo, - balance - }); - } - public postUser() { return this.http.post(`/api/v1/user`, {}); } @@ -518,6 +506,18 @@ export class DataService { }); } + public transferAccountBalance({ + accountIdFrom, + accountIdTo, + balance + }: TransferBalanceDto) { + return this.http.post('/api/v1/account/transfer-balance', { + accountIdFrom, + accountIdTo, + balance + }); + } + public updateInfo() { this.http.get('/api/v1/info').subscribe((info) => { const utmSource = <'ios' | 'trusted-web-activity'>(