refactor ticket
This commit is contained in:
@@ -13,6 +13,7 @@ import { AssignDesignerDto } from '../dto/assign-designer.dto';
|
|||||||
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||||
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
|
import { CreatePrintFormDto } from '../dto/create-print-form.dto';
|
||||||
|
|
||||||
@ApiTags('orders')
|
@ApiTags('orders')
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@@ -96,7 +97,7 @@ export class OrderController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('admin/orders')
|
@Get('admin/orders')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
||||||
findAllAsAdmin(@Query() dto: FindOrdersDto) {
|
findAllAsAdmin(@Query() dto: FindOrdersDto) {
|
||||||
return this.orderService.findOrdersAsAdmin(dto);
|
return this.orderService.findOrdersAsAdmin(dto);
|
||||||
@@ -117,11 +118,11 @@ export class OrderController {
|
|||||||
return this.orderService.updateOrderAsAdmin(orderId, dto);
|
return this.orderService.updateOrderAsAdmin(orderId, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('admin/orders/:orderId/print-form')
|
@Patch('admin/orders/:id/item/:itemId/print')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Update Order ' })
|
@ApiOperation({ summary: 'Create Print form for order item' })
|
||||||
updateOrderForPrint(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) {
|
createPrintForm(@Param('id') id: string, @Param('itemId') itemId: string, @Body() dto: CreatePrintFormDto) {
|
||||||
return this.orderService.updateOrderAsAdmin(orderId, dto);
|
return this.orderService.createPrintForm(id, +itemId, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('admin/orders/:orderId')
|
@Delete('admin/orders/:orderId')
|
||||||
@@ -172,4 +173,11 @@ export class OrderController {
|
|||||||
return this.orderService.updateOrderItemAsAdmin(orderId, +itemId, body);
|
return this.orderService.updateOrderItemAsAdmin(orderId, +itemId, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('admin/prints')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
||||||
|
findAllPrints(@Query() dto: FindOrdersDto) {
|
||||||
|
return this.orderService.findOrdersAsAdmin(dto);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,13 +37,13 @@ export class CreateOrderItemAsUserDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CreateOrderItemDtoAsAdmin extends CreateOrderItemAsUserDto {
|
export class CreateOrderItemDtoAsAdmin extends CreateOrderItemAsUserDto {
|
||||||
@ApiPropertyOptional({ example: [] })
|
// @ApiPropertyOptional({ example: [] })
|
||||||
@IsArray()
|
// @IsArray()
|
||||||
printAttributes?: IField[]
|
// printAttributes?: IField[]
|
||||||
|
|
||||||
@ApiPropertyOptional({ example: [] })
|
// @ApiPropertyOptional({ example: [] })
|
||||||
@IsArray()
|
// @IsArray()
|
||||||
printAttachments?: IAttachment[]
|
// printAttachments?: IAttachment[]
|
||||||
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { IsArray, } from 'class-validator';
|
||||||
|
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
|
import { IAttachment, IField } from '../interface/order.interface';
|
||||||
|
|
||||||
|
export class CreatePrintFormDto {
|
||||||
|
@ApiPropertyOptional({ example: [] })
|
||||||
|
@IsArray()
|
||||||
|
printAttributes?: IField[]
|
||||||
|
|
||||||
|
@ApiPropertyOptional({ example: [] })
|
||||||
|
@IsArray()
|
||||||
|
printAttachments?: IAttachment[]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
OptionalProps,
|
OptionalProps,
|
||||||
Formula
|
Formula
|
||||||
} from '@mikro-orm/core';
|
} from '@mikro-orm/core';
|
||||||
import { IField, OrderStatusEnum } from '../interface/order.interface';
|
import { OrderStatusEnum } from '../interface/order.interface';
|
||||||
import { User } from '../../user/entities/user.entity';
|
import { User } from '../../user/entities/user.entity';
|
||||||
import { OrderItem } from './order-item.entity';
|
import { OrderItem } from './order-item.entity';
|
||||||
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
||||||
|
|||||||
@@ -34,6 +34,6 @@ import { OrderItemRepository } from './repositories/order-item.repository';
|
|||||||
controllers: [OrderController],
|
controllers: [OrderController],
|
||||||
providers: [OrderService, OrderRepository, OrderListeners,
|
providers: [OrderService, OrderRepository, OrderListeners,
|
||||||
OrdersCrone, OrderItemRepository],
|
OrdersCrone, OrderItemRepository],
|
||||||
exports: [OrderRepository, OrderService],
|
exports: [OrderRepository, OrderService, OrderItemRepository],
|
||||||
})
|
})
|
||||||
export class OrderModule { }
|
export class OrderModule { }
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import { Product } from 'src/modules/product/entities/product.entity';
|
|||||||
import { AdminService } from 'src/modules/admin/providers/admin.service';
|
import { AdminService } from 'src/modules/admin/providers/admin.service';
|
||||||
import { OrderItem } from '../entities/order-item.entity';
|
import { OrderItem } from '../entities/order-item.entity';
|
||||||
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
||||||
|
import { CreatePrintFormDto } from '../dto/create-print-form.dto';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -161,6 +162,19 @@ export class OrderService {
|
|||||||
return updated
|
return updated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createPrintForm(orderId: string, itemId: number, dto: CreatePrintFormDto) {
|
||||||
|
|
||||||
|
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
|
||||||
|
|
||||||
|
const updated = await this.updateOrderItem(orderItem, dto)
|
||||||
|
|
||||||
|
// await this.calculateOrder(undefined, orderId)
|
||||||
|
|
||||||
|
// await this.em.flush()
|
||||||
|
|
||||||
|
return updated
|
||||||
|
}
|
||||||
|
|
||||||
async removeOrderItemAsAdmin(orderId: string, itemId: number) {
|
async removeOrderItemAsAdmin(orderId: string, itemId: number) {
|
||||||
|
|
||||||
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
|
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
|
||||||
@@ -206,6 +220,11 @@ export class OrderService {
|
|||||||
return orders
|
return orders
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findPrints(dto: FindOrdersDto) {
|
||||||
|
const orders = await this.orderRepository.findAllPaginated({...dto,statuses:[]})
|
||||||
|
return orders
|
||||||
|
}
|
||||||
|
|
||||||
// async calculateOrder(inputOrder?: Order, orderId?: string) {
|
// async calculateOrder(inputOrder?: Order, orderId?: string) {
|
||||||
// let order: undefined | Order = inputOrder
|
// let order: undefined | Order = inputOrder
|
||||||
|
|
||||||
@@ -509,7 +528,7 @@ export class OrderService {
|
|||||||
await this.em.transactional(async (em) => {
|
await this.em.transactional(async (em) => {
|
||||||
await this.orderRepository.nativeDelete(order)
|
await this.orderRepository.nativeDelete(order)
|
||||||
await this.orderItemRepository.nativeDelete({ order: { id: orderId } })
|
await this.orderItemRepository.nativeDelete({ order: { id: orderId } })
|
||||||
await this.ticketRepository.nativeDelete({ order: { id: orderId } })
|
// await this.ticketRepository.nativeDelete({ orderItemorder: { id: orderId } })
|
||||||
await em.flush()
|
await em.flush()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { FilterQuery } from '@mikro-orm/core';
|
|||||||
import { Order } from '../entities/order.entity';
|
import { Order } from '../entities/order.entity';
|
||||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||||
import { OrderStatusEnum } from '../interface/order.interface';
|
import { OrderStatusEnum } from '../interface/order.interface';
|
||||||
import { PaymentMethodEnum, PaymentStatusEnum } from '../../payment/interface/payment';
|
|
||||||
import { FindOrdersDto } from '../dto/find-orders.dto';
|
import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +93,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
|||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||||
populate: ['items','items.product'],
|
populate: ['items', 'items.product', 'user'],
|
||||||
});
|
});
|
||||||
|
|
||||||
// Collect all (orderId, productId) pairs for efficient review lookup
|
// Collect all (orderId, productId) pairs for efficient review lookup
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ import { AuthGuard } from "src/modules/auth/guards/auth.guard";
|
|||||||
export class TicketController {
|
export class TicketController {
|
||||||
constructor(private readonly ticketsService: TicketService) { }
|
constructor(private readonly ticketsService: TicketService) { }
|
||||||
|
|
||||||
@Post('public/ticket/order/:orderId')
|
@Post('public/ticket/order/item/:id')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: "create ticket ==> user route" })
|
@ApiOperation({ summary: "create ticket ==> user route" })
|
||||||
createTicket(@Param('orderId') orderId: string, @Body() createDto: CreateTicketDto, @UserId() userId: string) {
|
createTicket(@Param('id') itemId: string, @Body() createDto: CreateTicketDto, @UserId() userId: string) {
|
||||||
return this.ticketsService.createTicket(orderId, createDto, userId);
|
return this.ticketsService.createTicketAsUser(itemId, createDto, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -35,14 +35,14 @@ export class TicketController {
|
|||||||
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
deleteTicket(@Param('ticketId') ticketId: string, @UserId() userId: string) {
|
deleteTicket(@Param('ticketId') ticketId: string, @UserId() userId: string) {
|
||||||
return this.ticketsService.deleteTicket(ticketId, userId);
|
return this.ticketsService.deleteTicketAsUSer(+ticketId, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('public/ticket/:ticketId')
|
@Patch('public/ticket/:ticketId')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
||||||
updateTicket(@Param('ticketId') ticketId: string, @UserId() userId: string, @Body() body: UpdateTicketDto) {
|
updateTicket(@Param('ticketId') ticketId: string, @UserId() userId: string, @Body() body: UpdateTicketDto) {
|
||||||
return this.ticketsService.updateTicket(ticketId, body, userId,);
|
return this.ticketsService.updateTicketAsUser(+ticketId, userId, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=========================== Admin ============================== */
|
/*=========================== Admin ============================== */
|
||||||
@@ -50,15 +50,15 @@ export class TicketController {
|
|||||||
@Delete('admin/ticket/:ticketId')
|
@Delete('admin/ticket/:ticketId')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
||||||
deleteTicketAsAdmin(@Param('ticketId') ticketId: string) {
|
deleteTicketAsAdmin(@Param('ticketId') ticketId: string, @AdminId() adminId: string) {
|
||||||
return this.ticketsService.deleteTicket(ticketId);
|
return this.ticketsService.deleteTicketAsAdmin(+ticketId, adminId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('admin/ticket/order/:orderId')
|
@Post('admin/ticket/order/item/:id')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiOperation({ summary: "create ticket ==> admin route" })
|
@ApiOperation({ summary: "create ticket ==> admin route" })
|
||||||
createTicketAsAdmin(@Param('orderId') orderId: string, @Body() createDto: CreateTicketDto, @AdminId() adminId: string) {
|
createTicketAsAdmin(@Param('id') orderItemId: string, @Body() createDto: CreateTicketDto, @AdminId() adminId: string) {
|
||||||
return this.ticketsService.createTicket(orderId, createDto, undefined, adminId);
|
return this.ticketsService.createTicketAsAdmin(orderItemId, createDto, adminId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ export class TicketController {
|
|||||||
@Patch('admin/ticket/:ticketId')
|
@Patch('admin/ticket/:ticketId')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
|
||||||
updateTicketAsAdmin(@Param('ticketId') ticketId: string, @Body() body: UpdateTicketDto) {
|
updateTicketAsAdmin(@Param('ticketId') ticketId: string, @AdminId() adminId: string, @Body() body: UpdateTicketDto) {
|
||||||
return this.ticketsService.updateTicket(ticketId, body);
|
return this.ticketsService.updateTicketAsAdmin(+ticketId, adminId, body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import {
|
|||||||
} from '@mikro-orm/core';
|
} from '@mikro-orm/core';
|
||||||
import { User } from "../../user/entities/user.entity";
|
import { User } from "../../user/entities/user.entity";
|
||||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
import { Order } from 'src/modules/order/entities/order.entity';
|
|
||||||
import { AttachmentType } from '../interfaces/ticket';
|
import { AttachmentType } from '../interfaces/ticket';
|
||||||
|
import { OrderItem } from 'src/modules/order/entities/order-item.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Ticket {
|
export class Ticket {
|
||||||
@@ -29,8 +29,8 @@ export class Ticket {
|
|||||||
@Property({ type: 'json', nullable: true })
|
@Property({ type: 'json', nullable: true })
|
||||||
attachments?: AttachmentType[]
|
attachments?: AttachmentType[]
|
||||||
|
|
||||||
@ManyToOne(() => Order)
|
@ManyToOne(() => OrderItem)
|
||||||
order!: Order
|
orderItem!: OrderItem
|
||||||
|
|
||||||
@ManyToOne(() => Ticket, { nullable: true })
|
@ManyToOne(() => Ticket, { nullable: true })
|
||||||
parent?: Ticket
|
parent?: Ticket
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { TicketRepository } from "../repositories/tickets.repository";
|
import { TicketRepository } from "../repositories/tickets.repository";
|
||||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||||
import { CreateTicketDto } from "../DTO/create-ticket.dto";
|
import { CreateTicketDto } from "../DTO/create-ticket.dto";
|
||||||
import { OrderRepository } from "src/modules/order/repositories/order.repository";
|
|
||||||
import { User } from "src/modules/user/entities/user.entity";
|
import { User } from "src/modules/user/entities/user.entity";
|
||||||
import { UserRepository } from "src/modules/user/repositories/user.repository";
|
import { UserRepository } from "src/modules/user/repositories/user.repository";
|
||||||
import { AdminRepository } from "src/modules/admin/repositories/admin.repository";
|
import { AdminRepository } from "src/modules/admin/repositories/admin.repository";
|
||||||
@@ -9,6 +8,11 @@ import { Admin } from "src/modules/admin/entities/admin.entity";
|
|||||||
import { FindTicketQueryDto } from "../DTO/search-ticket-query.dto";
|
import { FindTicketQueryDto } from "../DTO/search-ticket-query.dto";
|
||||||
import { EntityManager } from "@mikro-orm/postgresql";
|
import { EntityManager } from "@mikro-orm/postgresql";
|
||||||
import { UpdateTicketDto } from "../DTO/update-ticket.dto";
|
import { UpdateTicketDto } from "../DTO/update-ticket.dto";
|
||||||
|
import { OrderItemRepository } from "src/modules/order/repositories/order-item.repository";
|
||||||
|
import { Ticket } from "../entities/ticket.entity";
|
||||||
|
import { OrderItem } from "src/modules/order/entities/order-item.entity";
|
||||||
|
import { AdminService } from "src/modules/admin/providers/admin.service";
|
||||||
|
import { UserService } from "src/modules/user/providers/user.service";
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -16,37 +20,127 @@ export class TicketService {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly ticketsRepository: TicketRepository,
|
private readonly ticketsRepository: TicketRepository,
|
||||||
private readonly orderRepository: OrderRepository,
|
private readonly orderItemRepository: OrderItemRepository,
|
||||||
private readonly userRepository: UserRepository,
|
private readonly userRepository: UserRepository,
|
||||||
private readonly adminRepository: AdminRepository,
|
private readonly adminRepository: AdminRepository,
|
||||||
|
private readonly adminService: AdminService,
|
||||||
|
private readonly userService: UserService,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async createTicket(orderId: string, dto: CreateTicketDto, userId?: string, adminId?: string) {
|
async createTicketAsAdmin(itemId: string, dto: CreateTicketDto, adminId: string) {
|
||||||
const { content, attachments } = dto
|
|
||||||
if (!userId && !adminId) {
|
const orderItem = await this.orderItemRepository.findOne({ id: itemId })
|
||||||
throw new BadRequestException("One of userId ord adminId is required")
|
if (!orderItem) {
|
||||||
}
|
throw new BadRequestException("orderItem not found")
|
||||||
const order = await this.orderRepository.findOne({ id: orderId })
|
|
||||||
if (!order) {
|
|
||||||
throw new BadRequestException("Order not found")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let user: null | User = null
|
|
||||||
let admin: null | Admin = null
|
let admin: null | Admin = null
|
||||||
|
|
||||||
|
admin = await this.adminService.findOrFail(adminId)
|
||||||
|
|
||||||
|
const ticket = await this.createTicket(orderItem, dto, undefined, admin)
|
||||||
|
|
||||||
|
return ticket
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async createTicketAsUser(itemId: string, dto: CreateTicketDto, userId: string) {
|
||||||
|
|
||||||
|
const orderItem = await this.orderItemRepository.findOne({ id: itemId })
|
||||||
|
|
||||||
|
if (!orderItem) {
|
||||||
|
throw new BadRequestException("orderItem not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await this.userService.findOrFail(userId)
|
||||||
|
|
||||||
|
const ticket = await this.createTicket(orderItem, dto, user, undefined)
|
||||||
|
|
||||||
|
return ticket
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateTicketAsUser(ticketId: number, userId: string, dto: UpdateTicketDto) {
|
||||||
|
|
||||||
|
const ticket = await this.findOneOrFail(ticketId)
|
||||||
|
|
||||||
|
this.assertUserOwner(ticket, userId)
|
||||||
|
|
||||||
|
await this.updateTicket(ticket, dto)
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateTicketAsAdmin(ticketId: number, adminId: string, dto: UpdateTicketDto) {
|
||||||
|
|
||||||
|
const ticket = await this.findOneOrFail(ticketId)
|
||||||
|
|
||||||
|
this.assertAdminOwner(ticket, adminId)
|
||||||
|
|
||||||
|
await this.updateTicket(ticket, dto)
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteTicketAsUser(ticketId: number, userId: string) {
|
||||||
|
|
||||||
|
const ticket = await this.findOneOrFail(ticketId)
|
||||||
|
|
||||||
|
this.assertUserOwner(ticket, userId)
|
||||||
|
|
||||||
|
await this.deleteTicket(ticket)
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteTicketAsAdmin(ticketId: number, adminId: string) {
|
||||||
|
|
||||||
|
const ticket = await this.findOneOrFail(ticketId)
|
||||||
|
|
||||||
|
this.assertAdminOwner(ticket, adminId)
|
||||||
|
|
||||||
|
await this.deleteTicket(ticket)
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
// //******************************** */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//******************************** */
|
||||||
|
|
||||||
|
async deleteTicketAsUSer(ticketId: number, userId: string) {
|
||||||
|
const ticket = await this.findOneOrFail(ticketId)
|
||||||
|
if (ticket.orderItem.order.user.id !== userId) {
|
||||||
|
throw new BadRequestException("You can not Delete this Ticket")
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.em.removeAndFlush(ticket)
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================ Core Logic ============
|
||||||
|
|
||||||
|
async getTickets(orderItemId: string, queryDto: FindTicketQueryDto, userId?: string) {
|
||||||
|
const orderItem = await this.orderItemRepository.findOne({ id: orderItemId })
|
||||||
|
|
||||||
|
if (!orderItem) {
|
||||||
|
throw new BadRequestException("orderItem not found!")
|
||||||
|
}
|
||||||
if (userId) {
|
if (userId) {
|
||||||
user = await this.userRepository.findOne({ id: userId })
|
if (orderItem.order.user.id !== userId) {
|
||||||
if (!user) {
|
throw new BadRequestException("This ticket doenst belongs to You !")
|
||||||
throw new BadRequestException("User not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (adminId) {
|
|
||||||
admin = await this.adminRepository.findOne({ id: adminId })
|
|
||||||
if (!admin) {
|
|
||||||
throw new BadRequestException("Admin not found")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const data = await this.ticketsRepository.findAllPaginated({ ...queryDto, orderItemId });
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
async createTicket(orderItem: OrderItem, dto: CreateTicketDto, user?: User, admin?: Admin) {
|
||||||
|
const { content, attachments } = dto
|
||||||
|
|
||||||
if (!user && !admin) {
|
if (!user && !admin) {
|
||||||
throw new BadRequestException("One of user or admin is required")
|
throw new BadRequestException("One of user or admin is required")
|
||||||
@@ -57,7 +151,7 @@ export class TicketService {
|
|||||||
attachments,
|
attachments,
|
||||||
admin,
|
admin,
|
||||||
user,
|
user,
|
||||||
order,
|
orderItem,
|
||||||
})
|
})
|
||||||
|
|
||||||
await this.ticketsRepository.em.persistAndFlush(ticket)
|
await this.ticketsRepository.em.persistAndFlush(ticket)
|
||||||
@@ -66,50 +160,13 @@ export class TicketService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// //******************************** */
|
async deleteTicket(ticket: Ticket) {
|
||||||
|
|
||||||
async getTickets(orderId: string, queryDto: FindTicketQueryDto, userId?: string) {
|
|
||||||
|
|
||||||
const order = await this.orderRepository.findOne({ id: orderId })
|
|
||||||
if (!order) {
|
|
||||||
throw new BadRequestException("order not found!")
|
|
||||||
}
|
|
||||||
if (userId) {
|
|
||||||
if (order.user.id !== userId) {
|
|
||||||
throw new BadRequestException("This ticket doenst belongs to You !")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const data = await this.ticketsRepository.findAllPaginated({ ...queryDto, orderId });
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
//******************************** */
|
|
||||||
|
|
||||||
async deleteTicket(ticketId: string, userId?: string) {
|
|
||||||
const ticket = await this.ticketsRepository.findOne({ id: ticketId }, { populate: ['order', 'order.user'] })
|
|
||||||
if (!ticket) {
|
|
||||||
throw new BadRequestException('ticket not found!')
|
|
||||||
}
|
|
||||||
if (userId && ticket.order.user.id !== userId) {
|
|
||||||
throw new BadRequestException("You can not Delete this Ticket")
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.em.removeAndFlush(ticket)
|
await this.em.removeAndFlush(ticket)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// //******************************** */
|
async updateTicket(ticket: Ticket, dto: UpdateTicketDto) {
|
||||||
|
|
||||||
async updateTicket(ticketId: string, dto: UpdateTicketDto, userId?: string) {
|
|
||||||
const ticket = await this.ticketsRepository.findOne({ id: ticketId }, { populate: ['order', 'order.user'] })
|
|
||||||
if (!ticket) {
|
|
||||||
throw new BadRequestException('ticket not found!')
|
|
||||||
}
|
|
||||||
if (userId && ticket.order.user.id !== userId) {
|
|
||||||
throw new BadRequestException("You can not Update this Ticket")
|
|
||||||
}
|
|
||||||
this.ticketsRepository.assign(ticket, dto, {
|
this.ticketsRepository.assign(ticket, dto, {
|
||||||
onlyProperties: true,
|
onlyProperties: true,
|
||||||
})
|
})
|
||||||
@@ -119,5 +176,35 @@ export class TicketService {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================ Helpers =================
|
||||||
|
|
||||||
|
async findOneOrFail(ticketId: number) {
|
||||||
|
const ticket = await this.ticketsRepository.findOne({ id: ticketId },
|
||||||
|
{ populate: ['orderItem', 'orderItem.order', 'user'] })
|
||||||
|
if (!ticket) {
|
||||||
|
throw new BadRequestException('ticket not found!')
|
||||||
|
}
|
||||||
|
return ticket
|
||||||
|
}
|
||||||
|
|
||||||
|
assertUserOwner(ticket: Ticket, userId: string) {
|
||||||
|
if (!ticket.user) {
|
||||||
|
throw new BadRequestException("User not found!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticket.user.id !== userId) {
|
||||||
|
throw new BadRequestException("You are not owner of this ticket!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertAdminOwner(ticket: Ticket, adminId: string) {
|
||||||
|
if (!ticket.admin) {
|
||||||
|
throw new BadRequestException("Admin not found!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticket.admin.id !== adminId) {
|
||||||
|
throw new BadRequestException("You are not owner of this ticket!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { FindTicketQueryDto } from '../DTO/search-ticket-query.dto';
|
|||||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||||
|
|
||||||
class FindOrdersOpts extends FindTicketQueryDto {
|
class FindOrdersOpts extends FindTicketQueryDto {
|
||||||
orderId: string;
|
orderItemId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -20,13 +20,13 @@ export class TicketRepository extends EntityRepository<Ticket> {
|
|||||||
const {
|
const {
|
||||||
page = 1,
|
page = 1,
|
||||||
limit = 10,
|
limit = 10,
|
||||||
orderId
|
orderItemId: orderId
|
||||||
} = opts;
|
} = opts;
|
||||||
|
|
||||||
const offset = (page - 1) * limit;
|
const offset = (page - 1) * limit;
|
||||||
|
|
||||||
const where: FilterQuery<Ticket> = {
|
const where: FilterQuery<Ticket> = {
|
||||||
order: { id: orderId }
|
orderItem: { id: orderId }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user