Browse Source

Refactoring

pull/2455/head
Thomas 2 years ago
parent
commit
4d00699af8
  1. 30
      apps/api/src/app/account/account.controller.ts
  2. 6
      apps/api/src/app/account/account.service.ts
  3. 6
      apps/client/src/app/pages/accounts/accounts-page.component.ts
  4. 24
      apps/client/src/app/services/data.service.ts

30
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
});
}

6
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<Account[]> {
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({

6
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 });
});
}
}

24
apps/client/src/app/services/data.service.ts

@ -480,18 +480,6 @@ export class DataService {
return this.http.post<OrderModel>(`/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<UserItem>(`/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<InfoItem>('/api/v1/info').subscribe((info) => {
const utmSource = <'ios' | 'trusted-web-activity'>(

Loading…
Cancel
Save