This commit is contained in:
2026-02-20 11:13:06 +03:30
parent 75a4154f90
commit 5d4154d135
4 changed files with 108 additions and 29 deletions
@@ -7,6 +7,7 @@ import { FindOrdersDto } from '../dto/find-orders.dto';
import { import {
CreateOrderAsAdminDto, CreateOrderAsAdminDto,
} from '../dto/create-order.dto'; } from '../dto/create-order.dto';
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
import { AdminId } from 'src/common/decorators/admin-id.decorator'; import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator'; import { Permissions } from 'src/common/decorators/permissions.decorator';
@@ -46,20 +47,20 @@ export class OrderController {
} }
// @Get('admin/orders/:id') @Get('admin/orders/:id')
// @UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
// @ApiOperation({ summary: 'order detail' }) @ApiOperation({ summary: 'order detail' })
// orderDetailAsAdmin(@Param('id') orderId: string) { orderDetailAsAdmin(@Param('id') orderId: string) {
// return this.orderService.findOrderOrFail(orderId); return this.orderService.findOrderOrFail(orderId);
// } }
// @Patch('admin/orders/:id') @Patch('admin/orders/:id')
// @UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
// @Permissions(PermissionEnum.UPDATE_ORDER) @Permissions(PermissionEnum.UPDATE_ORDER)
// @ApiOperation({ summary: 'Update Order ' }) @ApiOperation({ summary: 'Update Order ' })
// updateOrder(@Param('id') id: string, @Body() dto: UpdateOrderAsAdminDto) { updateOrder(@Param('id') id: string, @Body() dto: UpdateOrderAsAdminDto) {
// return this.orderService.updateOrderAsAdmin(id, dto); return this.orderService.updateOrderAsAdmin(id, dto);
// } }
// @Delete('admin/orders/:orderId') // @Delete('admin/orders/:orderId')
// @UseGuards(AdminAuthGuard) // @UseGuards(AdminAuthGuard)
+7 -2
View File
@@ -8,10 +8,15 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateOrderAsAdminDto { export class CreateOrderAsAdminDto {
@ApiProperty({ example: '' }) @ApiPropertyOptional({ example: '' })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
userId: string; userId?: string;
@ApiPropertyOptional({ example: '' })
@IsString()
@IsNotEmpty()
invoiceItemId?: string;
@ApiProperty() @ApiProperty()
@IsString() @IsString()
+11 -10
View File
@@ -1,14 +1,15 @@
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'; import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { CreateOrderAsAdminDto, CreateOrderItemDtoAsAdmin } from './create-order.dto'; import { CreateOrderAsAdminDto, CreateOrderItemDtoAsAdmin } from './create-order.dto';
import { IsNumber } from 'class-validator'; import { IsOptional, IsString } from 'class-validator';
export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) {
@ApiProperty()
items: UpdateOrCreateOrderItem[]
}
export class UpdateOrCreateOrderItem extends CreateOrderItemDtoAsAdmin { export class UpdateOrCreateOrderItem extends CreateOrderItemDtoAsAdmin {
@ApiProperty() @ApiProperty()
@IsNumber() @IsString()
id: string id: string;
}
export class UpdateOrderAsAdminDto extends PartialType(CreateOrderAsAdminDto) {
@ApiPropertyOptional({ type: [UpdateOrCreateOrderItem] })
@IsOptional()
items?: UpdateOrCreateOrderItem[];
} }
+76 -4
View File
@@ -24,6 +24,7 @@ import { PermissionEnum } from 'src/common/enums/permission.enum';
import { User } from 'src/modules/user/entities/user.entity'; import { User } from 'src/modules/user/entities/user.entity';
import { OrderType } from '../entities/order-type.entity'; import { OrderType } from '../entities/order-type.entity';
import { Product } from 'src/modules/product/entities/product.entity'; import { Product } from 'src/modules/product/entities/product.entity';
import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
@Injectable() @Injectable()
export class OrderService { export class OrderService {
@@ -45,11 +46,34 @@ export class OrderService {
) { } ) { }
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto): Promise<Order> { async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto): Promise<Order> {
const user = await this.userService.findById(dto.userId); let invoiceItem: null | InvoiceItem = null
if (!user) { let user: User | null = null
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
if (dto.invoiceItemId) {
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId },{populate:['invoice','invoice.user']});
if (!invoiceItem) {
throw new NotFoundException(`Invoice item with ID ${dto.invoiceItemId} not found.`);
}
user=invoiceItem.invoice.user
}
if (!invoiceItem) {
if (!dto.userId) {
throw new NotFoundException(`UserId is mandatory`);
}
const user = await this.userService.findById(dto.userId);
if (!user) {
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
}
} }
if(!user){
throw new NotFoundException(`User not found`);
}
const creator = await this.adminService.findOrFail(adminId); const creator = await this.adminService.findOrFail(adminId);
const type = await this.em.findOne(OrderType, { id: dto.typeId }); const type = await this.em.findOne(OrderType, { id: dto.typeId });
@@ -66,16 +90,64 @@ export class OrderService {
} }
const order = this.em.create(Order, { const order = this.em.create(Order, {
user: user as User, user,
creator, creator,
type, type,
product: product ?? null, product: product ?? null,
title: dto.title, title: dto.title,
status: OrderStatusEnum.CREATED, status: OrderStatusEnum.CREATED,
estimatedDays: dto.estimatedDays, estimatedDays: dto.estimatedDays,
invoiceItem
}); });
await this.em.persistAndFlush(order); await this.em.persistAndFlush(order);
return order; return order;
} }
async findOrderOrFail(id: string): Promise<Order> {
const order = await this.em.findOne(Order, { id }, { populate: ['user', 'type', 'product', 'creator'] });
if (!order) {
throw new NotFoundException(`Order with ID ${id} not found.`);
}
return order;
}
// user should not be change able
async updateOrderAsAdmin(id: string, dto: UpdateOrderAsAdminDto): Promise<Order> {
const order = await this.findOrderOrFail(id);
if (dto.userId !== undefined) {
const user = await this.userService.findById(dto.userId);
if (!user) {
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
}
order.user = user as User;
}
if (dto.typeId !== undefined) {
const type = await this.em.findOne(OrderType, { id: dto.typeId });
if (!type) {
throw new NotFoundException(`Order type with ID ${dto.typeId} not found.`);
}
order.type = type;
}
if (dto.estimatedDays !== undefined) {
order.estimatedDays = dto.estimatedDays;
}
if (dto.title !== undefined) {
order.title = dto.title;
}
if (dto.productId !== undefined) {
if (dto.productId == null || dto.productId === '') {
order.product = null;
} else {
const product = await this.em.findOne(Product, { id: dto.productId });
if (!product) {
throw new NotFoundException(`Product with ID ${dto.productId} not found.`);
}
order.product = product;
}
}
await this.em.flush();
return order;
}
} }