You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.5 KiB
89 lines
2.5 KiB
import { AccountService } from '@ghostfolio/api/app/account/account.service';
|
|
import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator';
|
|
import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard';
|
|
import { permissions } from '@ghostfolio/common/permissions';
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
|
|
|
import {
|
|
Controller,
|
|
Body,
|
|
Post,
|
|
Delete,
|
|
HttpException,
|
|
Inject,
|
|
Param,
|
|
UseGuards
|
|
} from '@nestjs/common';
|
|
import { REQUEST } from '@nestjs/core';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { AccountBalance } from '@prisma/client';
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
|
|
|
import { AccountBalanceService } from './account-balance.service';
|
|
import { CreateAccountBalanceDto } from './create-account-balance.dto';
|
|
|
|
@Controller('account-balance')
|
|
export class AccountBalanceController {
|
|
public constructor(
|
|
private readonly accountBalanceService: AccountBalanceService,
|
|
private readonly accountService: AccountService,
|
|
@Inject(REQUEST) private readonly request: RequestWithUser
|
|
) {}
|
|
|
|
@HasPermission(permissions.createAccountBalance)
|
|
@Post()
|
|
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
|
|
public async createAccountBalance(
|
|
@Body() data: CreateAccountBalanceDto
|
|
): Promise<AccountBalance> {
|
|
const account = await this.accountService.account({
|
|
id_userId: {
|
|
id: data.accountId,
|
|
userId: this.request.user.id
|
|
}
|
|
});
|
|
|
|
if (!account) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return this.accountBalanceService.createAccountBalance({
|
|
Account: {
|
|
connect: {
|
|
id_userId: {
|
|
id: account.id,
|
|
userId: account.userId
|
|
}
|
|
}
|
|
},
|
|
date: data.date,
|
|
value: data.balance
|
|
});
|
|
}
|
|
|
|
@HasPermission(permissions.deleteAccountBalance)
|
|
@Delete(':id')
|
|
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
|
|
public async deleteAccountBalance(
|
|
@Param('id') id: string
|
|
): Promise<AccountBalance> {
|
|
const accountBalance = await this.accountBalanceService.accountBalance({
|
|
id
|
|
});
|
|
|
|
if (!accountBalance || accountBalance.userId !== this.request.user.id) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return this.accountBalanceService.deleteAccountBalance({
|
|
id
|
|
});
|
|
}
|
|
}
|