This commit is contained in:
2026-02-20 18:08:43 +03:30
parent b2be6ba772
commit b0751bf42b
7 changed files with 133 additions and 26 deletions
+75 -10
View File
@@ -1,4 +1,4 @@
import { Injectable, BadRequestException, Logger, NotFoundException } from '@nestjs/common';
import { Injectable, BadRequestException, Logger, NotFoundException, ForbiddenException } from '@nestjs/common';
import { EntityManager } from '@mikro-orm/postgresql';
import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
@@ -26,6 +26,8 @@ import { OrderType } from '../entities/order-type.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
import { UpdateStatusAsDesignerDto } from '../dto/update-status-as-designer.dto';
import { UpdateStatusDto } from '../dto/update-status.dto';
@Injectable()
export class OrderService {
@@ -112,8 +114,26 @@ export class OrderService {
})
}
async findOrdersAsAdmin(dto: FindOrdersDto): Promise<PaginatedResult<Order>> {
return this.orderRepository.findAllPaginated(dto)
async findOrdersAsAdmin(adminId: string, dto: FindOrdersDto): Promise<PaginatedResult<Order>> {
const permissions = await this.adminService.hasPermissions(adminId,
[PermissionEnum.VIEW_ORDERS, PermissionEnum.VIEW_ASSIGNED_ORDERS])
if (permissions.includes(PermissionEnum.VIEW_ORDERS)) {
return this.orderRepository.findAllPaginated(dto)
} else if (permissions.includes(PermissionEnum.VIEW_ASSIGNED_ORDERS)) {
return this.orderRepository.findAllPaginated({
designerId: adminId,
...dto
})
} else {
throw new ForbiddenException("You don't have permission to view orders")
}
}
async getOrderAsUser(userId: string, orderId: string): Promise<Order> {
@@ -124,13 +144,6 @@ export class OrderService {
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;
}
async updateOrderAsAdmin(id: string, dto: UpdateOrderAsAdminDto): Promise<Order> {
const order = await this.findOrderOrFail(id);
@@ -177,6 +190,38 @@ export class OrderService {
await this.em.flush();
}
async updateStatusAsDesigner(orderId: string, dto: UpdateStatusAsDesignerDto): Promise<Order> {
const order = await this.findOrderOrFail(orderId);
order.status = dto.status;
await this.em.flush();
return order
}
async updateStatus(orderId: string, adminId: string, dto: UpdateStatusDto): Promise<Order> {
const permissions = await this.adminService.hasPermissions(adminId,
[PermissionEnum.VIEW_ORDERS, PermissionEnum.VIEW_ASSIGNED_ORDERS])
if (permissions.includes(PermissionEnum.VIEW_ORDERS)) {
const order = await this.findOrderOrFail(orderId);
order.status = dto.status;
await this.em.flush();
return order
}
if (permissions.includes(PermissionEnum.VIEW_ASSIGNED_ORDERS)) {
const order = await this.findOrderOrFailAssignedOrder(orderId, adminId);
order.status = dto.status;
await this.em.flush();
return order
}
const order = await this.findOrderOrFail(orderId);
order.status = dto.status;
await this.em.flush();
return order
}
async assignDesigner(orderId: string, designerId: string) {
const order = await this.findOrderOrFail(orderId)
const designer = await this.adminService.findOrFail(designerId)
@@ -185,4 +230,24 @@ export class OrderService {
await this.em.flush()
return order
}
// -----------Hrlper Methods -----------
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;
}
async findOrderOrFailAssignedOrder(id: string, adminId: 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.`);
}
if (order?.designer?.id !== adminId) {
throw new ForbiddenException("You don't have permission to view this order")
}
return order;
}
}