2021-08-16 21:40:29 +02:00
|
|
|
import { UserService } from '@ghostfolio/api/app/user/user.service';
|
2021-04-21 20:27:39 +02:00
|
|
|
import { nullifyValuesInObjects } from '@ghostfolio/api/helper/object.helper';
|
|
|
|
import { ImpersonationService } from '@ghostfolio/api/services/impersonation.service';
|
2021-12-07 20:24:15 +01:00
|
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
2021-09-11 09:27:22 +02:00
|
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
2021-04-13 21:53:58 +02:00
|
|
|
import {
|
|
|
|
Body,
|
|
|
|
Controller,
|
|
|
|
Delete,
|
|
|
|
Get,
|
|
|
|
Headers,
|
|
|
|
HttpException,
|
|
|
|
Inject,
|
|
|
|
Param,
|
|
|
|
Post,
|
|
|
|
Put,
|
|
|
|
UseGuards
|
|
|
|
} from '@nestjs/common';
|
|
|
|
import { REQUEST } from '@nestjs/core';
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
import { Order as OrderModel } from '@prisma/client';
|
|
|
|
import { parseISO } from 'date-fns';
|
|
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
|
|
|
|
|
|
|
import { CreateOrderDto } from './create-order.dto';
|
2022-01-23 11:39:30 +01:00
|
|
|
import { Activities } from './interfaces/activities.interface';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { OrderService } from './order.service';
|
|
|
|
import { UpdateOrderDto } from './update-order.dto';
|
|
|
|
|
|
|
|
@Controller('order')
|
|
|
|
export class OrderController {
|
|
|
|
public constructor(
|
|
|
|
private readonly impersonationService: ImpersonationService,
|
|
|
|
private readonly orderService: OrderService,
|
2021-08-16 21:40:29 +02:00
|
|
|
@Inject(REQUEST) private readonly request: RequestWithUser,
|
|
|
|
private readonly userService: UserService
|
2021-04-13 21:53:58 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
@Delete(':id')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async deleteOrder(@Param('id') id: string): Promise<OrderModel> {
|
|
|
|
if (
|
2021-12-07 20:24:15 +01:00
|
|
|
!hasPermission(this.request.user.permissions, permissions.deleteOrder)
|
2021-04-13 21:53:58 +02:00
|
|
|
) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
return this.orderService.deleteOrder({
|
|
|
|
id_userId: {
|
|
|
|
id,
|
|
|
|
userId: this.request.user.id
|
|
|
|
}
|
|
|
|
});
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Get()
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async getAllOrders(
|
|
|
|
@Headers('impersonation-id') impersonationId
|
2022-01-23 11:39:30 +01:00
|
|
|
): Promise<Activities> {
|
2021-07-03 11:32:03 +02:00
|
|
|
const impersonationUserId =
|
|
|
|
await this.impersonationService.validateImpersonationId(
|
|
|
|
impersonationId,
|
|
|
|
this.request.user.id
|
|
|
|
);
|
2022-01-23 11:39:30 +01:00
|
|
|
const userCurrency = this.request.user.Settings.currency;
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2022-01-23 11:39:30 +01:00
|
|
|
let activities = await this.orderService.getOrders({
|
|
|
|
userCurrency,
|
2021-12-28 17:45:04 +01:00
|
|
|
includeDrafts: true,
|
|
|
|
userId: impersonationUserId || this.request.user.id
|
2021-04-13 21:53:58 +02:00
|
|
|
});
|
|
|
|
|
2021-08-16 21:40:29 +02:00
|
|
|
if (
|
|
|
|
impersonationUserId ||
|
|
|
|
this.userService.isRestrictedView(this.request.user)
|
|
|
|
) {
|
2022-01-23 11:39:30 +01:00
|
|
|
activities = nullifyValuesInObjects(activities, [
|
2021-12-28 17:45:04 +01:00
|
|
|
'fee',
|
2022-01-23 11:39:30 +01:00
|
|
|
'feeInBaseCurrency',
|
2021-12-28 17:45:04 +01:00
|
|
|
'quantity',
|
|
|
|
'unitPrice',
|
2022-01-23 11:39:30 +01:00
|
|
|
'value',
|
|
|
|
'valueInBaseCurrency'
|
2021-12-28 17:45:04 +01:00
|
|
|
]);
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 11:39:30 +01:00
|
|
|
return { activities };
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Get(':id')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async getOrderById(@Param('id') id: string): Promise<OrderModel> {
|
|
|
|
return this.orderService.order({
|
|
|
|
id_userId: {
|
|
|
|
id,
|
|
|
|
userId: this.request.user.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async createOrder(@Body() data: CreateOrderDto): Promise<OrderModel> {
|
|
|
|
if (
|
2021-12-07 20:24:15 +01:00
|
|
|
!hasPermission(this.request.user.permissions, permissions.createOrder)
|
2021-04-13 21:53:58 +02:00
|
|
|
) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const date = parseISO(data.date);
|
|
|
|
|
2021-04-25 21:22:35 +02:00
|
|
|
const accountId = data.accountId;
|
|
|
|
delete data.accountId;
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
return this.orderService.createOrder({
|
|
|
|
...data,
|
2022-01-16 15:31:56 +01:00
|
|
|
date,
|
2021-08-07 20:52:55 +02:00
|
|
|
Account: {
|
|
|
|
connect: {
|
|
|
|
id_userId: { id: accountId, userId: this.request.user.id }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
SymbolProfile: {
|
|
|
|
connectOrCreate: {
|
2022-01-16 15:31:56 +01:00
|
|
|
create: {
|
|
|
|
dataSource: data.dataSource,
|
|
|
|
symbol: data.symbol
|
|
|
|
},
|
2021-08-07 20:52:55 +02:00
|
|
|
where: {
|
|
|
|
dataSource_symbol: {
|
2021-06-09 20:35:02 +02:00
|
|
|
dataSource: data.dataSource,
|
|
|
|
symbol: data.symbol
|
|
|
|
}
|
|
|
|
}
|
2021-08-07 20:52:55 +02:00
|
|
|
}
|
2021-05-03 21:19:56 +02:00
|
|
|
},
|
2021-08-07 20:52:55 +02:00
|
|
|
User: { connect: { id: this.request.user.id } }
|
|
|
|
});
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Put(':id')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async update(@Param('id') id: string, @Body() data: UpdateOrderDto) {
|
|
|
|
if (
|
2021-12-07 20:24:15 +01:00
|
|
|
!hasPermission(this.request.user.permissions, permissions.updateOrder)
|
2021-04-13 21:53:58 +02:00
|
|
|
) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const originalOrder = await this.orderService.order({
|
|
|
|
id_userId: {
|
|
|
|
id,
|
|
|
|
userId: this.request.user.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-02 21:18:52 +02:00
|
|
|
if (!originalOrder) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
const date = parseISO(data.date);
|
|
|
|
|
2021-04-25 21:22:35 +02:00
|
|
|
const accountId = data.accountId;
|
|
|
|
delete data.accountId;
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
return this.orderService.updateOrder({
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
date,
|
|
|
|
Account: {
|
|
|
|
connect: {
|
|
|
|
id_userId: { id: accountId, userId: this.request.user.id }
|
2021-05-03 21:19:56 +02:00
|
|
|
}
|
2021-08-07 20:52:55 +02:00
|
|
|
},
|
|
|
|
User: { connect: { id: this.request.user.id } }
|
2021-05-03 21:19:56 +02:00
|
|
|
},
|
2021-08-07 20:52:55 +02:00
|
|
|
where: {
|
|
|
|
id_userId: {
|
|
|
|
id,
|
|
|
|
userId: this.request.user.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|