request
This commit is contained in:
@@ -3,8 +3,11 @@ export enum PermissionEnum {
|
|||||||
MANAGE_PRODUCTS = 'manage_products',
|
MANAGE_PRODUCTS = 'manage_products',
|
||||||
MANAGE_CATEGORIES = 'manage_categories',
|
MANAGE_CATEGORIES = 'manage_categories',
|
||||||
|
|
||||||
// Order Management
|
// Request Management
|
||||||
VIEW_All_REQUEST_ORDERS = 'view_all_request_order',
|
VIEW_REQUESTS = 'view_requests',
|
||||||
|
DELETE_REQUEST = 'delete_request',
|
||||||
|
UPDATE_REQUEST = 'update_request',
|
||||||
|
|
||||||
VIEW_All_INVOICED_ORDERS = 'view_all_invoiced_orders',
|
VIEW_All_INVOICED_ORDERS = 'view_all_invoiced_orders',
|
||||||
VIEW_MY_ASSIGNED_INVOICED_REQUESTS = 'view_my_assigned_invoiced_requests',
|
VIEW_MY_ASSIGNED_INVOICED_REQUESTS = 'view_my_assigned_invoiced_requests',
|
||||||
|
|
||||||
@@ -12,7 +15,7 @@ export enum PermissionEnum {
|
|||||||
CREATE_ORDER = 'create_order',
|
CREATE_ORDER = 'create_order',
|
||||||
UPDATE_ORDER = 'update_order',
|
UPDATE_ORDER = 'update_order',
|
||||||
ASSIGN_DESIGNER = 'assign_designer',
|
ASSIGN_DESIGNER = 'assign_designer',
|
||||||
CREATE_ORDER_PRINT='create_order_print',
|
CREATE_ORDER_PRINT = 'create_order_print',
|
||||||
CHANGE_ORDER_STATUS = 'change_order_status',
|
CHANGE_ORDER_STATUS = 'change_order_status',
|
||||||
REMOVE_ORDER = 'remove_order',
|
REMOVE_ORDER = 'remove_order',
|
||||||
|
|
||||||
@@ -55,8 +58,10 @@ export const PermissionTitles: Record<PermissionEnum, string> = {
|
|||||||
[PermissionEnum.MANAGE_SETTINGS]: 'مدیریت تنظیمات',
|
[PermissionEnum.MANAGE_SETTINGS]: 'مدیریت تنظیمات',
|
||||||
[PermissionEnum.MANAGE_FORM_BUILDER]: "مدیریت فرم بیلدر",
|
[PermissionEnum.MANAGE_FORM_BUILDER]: "مدیریت فرم بیلدر",
|
||||||
[PermissionEnum.MANAGE_PRINT]: 'مدیریت فرم پرینت',
|
[PermissionEnum.MANAGE_PRINT]: 'مدیریت فرم پرینت',
|
||||||
[PermissionEnum.VIEW_All_REQUEST_ORDERS]: "مشاهده همه درخواست سفارش ها",
|
[PermissionEnum.VIEW_REQUESTS]: "مشاهده همه درخواست سفارش ها",
|
||||||
[PermissionEnum.VIEW_All_INVOICED_ORDERS]: "مشاهده همه سفارشات پیش فاکتور شده",
|
[PermissionEnum.VIEW_All_INVOICED_ORDERS]: "مشاهده همه سفارشات پیش فاکتور شده",
|
||||||
[PermissionEnum.VIEW_MY_ASSIGNED_INVOICED_REQUESTS]: "مشاهده همه پیش فاکتور های من",
|
[PermissionEnum.VIEW_MY_ASSIGNED_INVOICED_REQUESTS]: "مشاهده همه پیش فاکتور های من",
|
||||||
[PermissionEnum.CREATE_ORDER_PRINT]: "ایجاد سفارش چاپ"
|
[PermissionEnum.CREATE_ORDER_PRINT]: "ایجاد سفارش چاپ",
|
||||||
|
[PermissionEnum.DELETE_REQUEST]: "",
|
||||||
|
[PermissionEnum.UPDATE_REQUEST]: ""
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ export class OrderController {
|
|||||||
|
|
||||||
@Get('admin/orders/request')
|
@Get('admin/orders/request')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@Permissions(PermissionEnum.VIEW_All_REQUEST_ORDERS)
|
@Permissions(PermissionEnum.VIEW_REQUESTS)
|
||||||
@ApiOperation({ summary: 'Get all order requests with pagination and filters' })
|
@ApiOperation({ summary: 'Get all order requests with pagination and filters' })
|
||||||
findOrderRequestAsAdmin(@Query() dto: FindOrdersDto) {
|
findOrderRequestAsAdmin(@Query() dto: FindOrdersDto) {
|
||||||
return this.orderService.findOrderRequestsAsAdmin(dto);
|
return this.orderService.findOrderRequestsAsAdmin(dto);
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import { UpdateRequestDto } from './dto/update-request.dto';
|
|||||||
import { AuthGuard } from '../auth/guards/auth.guard';
|
import { AuthGuard } from '../auth/guards/auth.guard';
|
||||||
import { UserId } from 'src/common/decorators';
|
import { UserId } from 'src/common/decorators';
|
||||||
import { FindRequestsDto } from './dto/find-requests.dto';
|
import { FindRequestsDto } from './dto/find-requests.dto';
|
||||||
|
import { AdminAuthGuard } from '../auth/guards/adminAuth.guard';
|
||||||
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||||
|
import { PermissionEnum } from 'src/common/enums/permission.enum';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class RequestController {
|
export class RequestController {
|
||||||
@@ -30,8 +33,8 @@ export class RequestController {
|
|||||||
|
|
||||||
@Patch('public/request/:id')
|
@Patch('public/request/:id')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
update(@Param('id') id: string, @Body() updateRequestDto: UpdateRequestDto, @UserId() userId: string) {
|
update(@Param('id') id: string, @Body() dto: UpdateRequestDto, @UserId() userId: string) {
|
||||||
return this.requestService.update(id, updateRequestDto, userId);
|
return this.requestService.update(id, dto, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('public/request/:id')
|
@Delete('public/request/:id')
|
||||||
@@ -39,4 +42,33 @@ export class RequestController {
|
|||||||
remove(@Param('id') id: string, @UserId() userId: string) {
|
remove(@Param('id') id: string, @UserId() userId: string) {
|
||||||
return this.requestService.remove(id, userId);
|
return this.requestService.remove(id, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------ADmin Routes -------------------
|
||||||
|
@Get('admin/request')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@Permissions(PermissionEnum.VIEW_REQUESTS)
|
||||||
|
findAllAsAdmin(@Query() dto: FindRequestsDto) {
|
||||||
|
return this.requestService.findAllAsAdmin(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('admin/request/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@Permissions(PermissionEnum.VIEW_REQUESTS)
|
||||||
|
findOneAsAdmin(@Param('id') id: string) {
|
||||||
|
return this.requestService.findOneAsAdmin(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch('public/request/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@Permissions(PermissionEnum.UPDATE_REQUEST)
|
||||||
|
updateRequestAsAdmin(@Param('id') id: string, @Body() dto: UpdateRequestDto) {
|
||||||
|
return this.requestService.updateAsAdmin(id, dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('public/request/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@Permissions(PermissionEnum.DELETE_REQUEST)
|
||||||
|
removeRequestAsAdmin(@Param('id') id: string) {
|
||||||
|
return this.requestService.removeAsAdmin(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export class RequestService {
|
|||||||
private readonly requestRepository: RequestRepository,
|
private readonly requestRepository: RequestRepository,
|
||||||
private readonly userService: UserService,
|
private readonly userService: UserService,
|
||||||
private readonly productService: ProductService,
|
private readonly productService: ProductService,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
async create(createRequestDto: CreateRequestDto, userId: string) {
|
async create(createRequestDto: CreateRequestDto, userId: string) {
|
||||||
const { items } = createRequestDto;
|
const { items } = createRequestDto;
|
||||||
@@ -53,6 +53,10 @@ export class RequestService {
|
|||||||
return this.requestRepository.findAllPaginated({ userId, ...dto });
|
return this.requestRepository.findAllPaginated({ userId, ...dto });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findAllAsAdmin(dto: FindRequestsDto) {
|
||||||
|
return this.requestRepository.findAllPaginated(dto);
|
||||||
|
}
|
||||||
|
|
||||||
async findOne(id: string, userId: string) {
|
async findOne(id: string, userId: string) {
|
||||||
const request = await this.requestRepository.findOne(
|
const request = await this.requestRepository.findOne(
|
||||||
{ id, user: { id: userId } },
|
{ id, user: { id: userId } },
|
||||||
@@ -64,6 +68,17 @@ export class RequestService {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findOneAsAdmin(id: string) {
|
||||||
|
const request = await this.requestRepository.findOne(
|
||||||
|
{ id },
|
||||||
|
{ populate: ['items', 'items.product', 'user'] },
|
||||||
|
);
|
||||||
|
if (!request) {
|
||||||
|
throw new NotFoundException('Request not found');
|
||||||
|
}
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
async update(id: string, updateRequestDto: UpdateRequestDto, userId: string) {
|
async update(id: string, updateRequestDto: UpdateRequestDto, userId: string) {
|
||||||
const request = await this.requestRepository.findOne(
|
const request = await this.requestRepository.findOne(
|
||||||
{ id, user: { id: userId } },
|
{ id, user: { id: userId } },
|
||||||
@@ -73,6 +88,46 @@ export class RequestService {
|
|||||||
throw new NotFoundException('Request not found');
|
throw new NotFoundException('Request not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.status !== RequestStatusEnum.PENDING) {
|
||||||
|
throw new BadRequestException('Only pending requests can be updated');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { items } = updateRequestDto;
|
||||||
|
if (items?.length) {
|
||||||
|
return this.em.transactional(async (em) => {
|
||||||
|
request.items.removeAll();
|
||||||
|
await em.flush();
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
const product = await this.productService.findOneOrFail(item.productId);
|
||||||
|
const requestItem = em.create(RequestItem, {
|
||||||
|
request,
|
||||||
|
product,
|
||||||
|
quantity: item.quantity,
|
||||||
|
attributes: item.attributes,
|
||||||
|
description: item.description,
|
||||||
|
attachments: item.attachments,
|
||||||
|
});
|
||||||
|
request.items.add(requestItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
await em.flush();
|
||||||
|
return request;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateAsAdmin(id: string, updateRequestDto: UpdateRequestDto) {
|
||||||
|
const request = await this.requestRepository.findOne(
|
||||||
|
{ id },
|
||||||
|
{ populate: ['items', 'items.product', 'user'] },
|
||||||
|
);
|
||||||
|
if (!request) {
|
||||||
|
throw new NotFoundException('Request not found');
|
||||||
|
}
|
||||||
|
|
||||||
const { items } = updateRequestDto;
|
const { items } = updateRequestDto;
|
||||||
if (items?.length) {
|
if (items?.length) {
|
||||||
return this.em.transactional(async (em) => {
|
return this.em.transactional(async (em) => {
|
||||||
@@ -114,4 +169,16 @@ export class RequestService {
|
|||||||
await this.em.flush();
|
await this.em.flush();
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async removeAsAdmin(id: string) {
|
||||||
|
const request = await this.requestRepository.findOne(
|
||||||
|
{ id },
|
||||||
|
);
|
||||||
|
if (!request) {
|
||||||
|
throw new NotFoundException('Request not found');
|
||||||
|
}
|
||||||
|
request.deletedAt = new Date();
|
||||||
|
await this.em.flush();
|
||||||
|
return request;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user