create ticket
This commit is contained in:
@@ -1,28 +0,0 @@
|
|||||||
import { ApiProperty } from "@nestjs/swagger";
|
|
||||||
import { IsBoolean, IsNotEmpty, IsString, IsUUID, Length } from "class-validator";
|
|
||||||
|
|
||||||
import { TicketMessageEnum } from "../../../common/enums/message.enum";
|
|
||||||
|
|
||||||
export class CreateTicketCategoryDto {
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.TITLE_REQUIRED })
|
|
||||||
@IsString({ message: TicketMessageEnum.TITLE_STRING })
|
|
||||||
@Length(3, 150, { message: TicketMessageEnum.TITLE_LENGTH })
|
|
||||||
@ApiProperty({ description: "Title of the ticket category", example: "فنی", minLength: 3, maxLength: 150 })
|
|
||||||
title: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.DESCRIPTION_REQUIRED })
|
|
||||||
@IsString({ message: TicketMessageEnum.DESCRIPTION_STRING })
|
|
||||||
@Length(3, 500, { message: TicketMessageEnum.DESCRIPTION_LENGTH })
|
|
||||||
@ApiProperty({ description: "Description of the ticket category", example: "درخواستهای فنی", minLength: 3, maxLength: 500 })
|
|
||||||
description: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.USER_GROUP_ID_NOT_EMPTY })
|
|
||||||
@IsUUID("4", { message: TicketMessageEnum.USER_GROUP_ID_SHOULD_BE_UUID })
|
|
||||||
@ApiProperty({ description: "User group ID", example: "123e4567-e89b-12d3-a456-426614174000" })
|
|
||||||
userGroupId: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.IS_ACTIVE_REQUIRED })
|
|
||||||
@IsBoolean({ message: TicketMessageEnum.IS_ACTIVE_SHOULD_BE_BOOLEAN })
|
|
||||||
@ApiProperty({ description: "Is active", example: true })
|
|
||||||
isActive: boolean;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { ApiProperty } from "@nestjs/swagger";
|
|
||||||
import { ArrayMinSize, IsNotEmpty, IsOptional, IsString, IsUrl } from "class-validator";
|
|
||||||
|
|
||||||
import { TicketMessageEnum } from "../../../common/enums/message.enum";
|
|
||||||
|
|
||||||
export class CreateTicketMessageDto {
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.CONTENT_REQUIRED })
|
|
||||||
@IsString({ message: TicketMessageEnum.CONTENT_STRING })
|
|
||||||
@ApiProperty({ description: "the content of message", example: "test message" })
|
|
||||||
content: string;
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
|
||||||
@ArrayMinSize(1, { message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
|
||||||
@IsUrl({ protocols: ["http", "https"], require_protocol: true }, { each: true, message: TicketMessageEnum.ATTACHMENT_SHOULD_BE_URL })
|
|
||||||
@ApiProperty({ description: "attachment url", example: ["https://example.com/test.png"] })
|
|
||||||
attachmentUrls?: string[];
|
|
||||||
}
|
|
||||||
@@ -5,47 +5,16 @@ import { TicketMessageEnum, UserMessage } from "../../../common/enums/message.en
|
|||||||
|
|
||||||
|
|
||||||
export class CreateTicketDto {
|
export class CreateTicketDto {
|
||||||
// @IsNotEmpty({ message: TicketMessageEnum.TITLE_REQUIRED })
|
|
||||||
// @IsString({ message: TicketMessageEnum.TITLE_STRING })
|
|
||||||
// @Length(3, 150, { message: TicketMessageEnum.TITLE_LENGTH })
|
|
||||||
// @ApiProperty({ description: "The ticket title", example: "Test title" })
|
|
||||||
// title: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.SUBJECT_REQUIRED })
|
|
||||||
@IsString({ message: TicketMessageEnum.SUBJECT_STRING })
|
|
||||||
@Length(3, 150, { message: TicketMessageEnum.SUBJECT_LENGTH })
|
|
||||||
@ApiProperty({ description: "Subject of the ticket", example: "Subject" })
|
|
||||||
subject: string;
|
|
||||||
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.DANAK_SERVICE_ID_REQUIRED })
|
|
||||||
@IsUUID("4", { message: TicketMessageEnum.DANAK_SERVICE_ID_SHOULD_BE_UUID })
|
|
||||||
@ApiProperty({ description: "Service ID related to the ticket", example: "550e8400-e29b-41d4-a716-446655440000" })
|
|
||||||
danakServiceId?: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.CATEGORY_ID_REQUIRED })
|
|
||||||
@IsUUID("4", { message: TicketMessageEnum.CATEGORY_ID_SHOULD_BE_UUID })
|
|
||||||
@ApiProperty({ description: "Category ID of the ticket", example: "123e4567-e89b-12d3-a456-426614174000" })
|
|
||||||
categoryId: string;
|
|
||||||
|
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.MESSAGE_REQUIRED })
|
@IsNotEmpty({ message: TicketMessageEnum.MESSAGE_REQUIRED })
|
||||||
@IsString({ message: TicketMessageEnum.MESSAGE_STRING })
|
@IsString({ message: TicketMessageEnum.MESSAGE_STRING })
|
||||||
@Length(5, 5000, { message: TicketMessageEnum.MESSAGE_LENGTH })
|
@Length(5, 5000, { message: TicketMessageEnum.MESSAGE_LENGTH })
|
||||||
@ApiProperty({ description: "The message content of the ticket", example: "This is a detailed description of the issue." })
|
@ApiProperty({ description: "The message content of the ticket" })
|
||||||
message: string;
|
content: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsNotEmpty({ message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
@IsNotEmpty({ message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
||||||
@ArrayMinSize(1, { message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
|
||||||
@IsUrl({ protocols: ["http", "https"], require_protocol: true }, { each: true, message: TicketMessageEnum.ATTACHMENT_SHOULD_BE_URL })
|
@IsUrl({ protocols: ["http", "https"], require_protocol: true }, { each: true, message: TicketMessageEnum.ATTACHMENT_SHOULD_BE_URL })
|
||||||
@ApiProperty({ description: "attachment url", example: ["https://example.com/test.png"] })
|
@ApiProperty({ description: "attachment url", example: ["https://example.com/test.png"] })
|
||||||
attachmentUrls?: string[];
|
attachments?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateTicketByAdminDto extends CreateTicketDto {
|
|
||||||
@IsNotEmpty({ message: "UserMessage.USER_ID_REQUIRED" })
|
|
||||||
@IsUUID("4", { message:" UserMessage.USER_ID_SHOULD_BE_A_UUID" })
|
|
||||||
@ApiProperty({ description: "User ID of the ticket", example: "123e4567-e89b-12d3-a456-426614174000" })
|
|
||||||
userId: string;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,28 +1,21 @@
|
|||||||
import {
|
import {
|
||||||
Entity,
|
Entity,
|
||||||
Index,
|
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
OneToMany,
|
|
||||||
Property,
|
Property,
|
||||||
Collection,
|
|
||||||
Cascade,
|
|
||||||
Enum,
|
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
|
OptionalProps,
|
||||||
} from '@mikro-orm/core';
|
} from '@mikro-orm/core';
|
||||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
|
||||||
import { User } from "../../user/entities/user.entity";
|
import { User } from "../../user/entities/user.entity";
|
||||||
import { TicketStatus } from "../enums/ticket-status.enum";
|
|
||||||
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 { Order } from 'src/modules/order/entities/order.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Ticket extends BaseEntity {
|
export class Ticket {
|
||||||
|
[OptionalProps]?: 'createdAt'
|
||||||
|
|
||||||
@PrimaryKey({ type: "bigint", autoincrement: true })
|
@PrimaryKey({ type: "bigint", autoincrement: true })
|
||||||
id: bigint;
|
id: bigint;
|
||||||
|
|
||||||
@Enum(() => TicketStatus)
|
|
||||||
status: TicketStatus;
|
|
||||||
|
|
||||||
@Property({ type: 'text' })
|
@Property({ type: 'text' })
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
@@ -32,15 +25,15 @@ export class Ticket extends BaseEntity {
|
|||||||
@ManyToOne(() => Admin, { nullable: true })
|
@ManyToOne(() => Admin, { nullable: true })
|
||||||
admin?: Admin | null;
|
admin?: Admin | null;
|
||||||
|
|
||||||
@ManyToOne(() => Ticket, { nullable: true })
|
@Property({ type: 'json', nullable: true })
|
||||||
parent?: Ticket | null;
|
attachments?: string[]
|
||||||
|
|
||||||
@OneToMany(() => Ticket, (ticket) => ticket.parent)
|
|
||||||
children = new Collection<Ticket>(this)
|
|
||||||
|
|
||||||
@Property({ type: 'json' })
|
|
||||||
attachments: string[]
|
|
||||||
|
|
||||||
@ManyToOne(() => Order)
|
@ManyToOne(() => Order)
|
||||||
order!: Order
|
order!: Order
|
||||||
|
|
||||||
|
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
|
||||||
|
createdAt: Date = new Date();
|
||||||
|
|
||||||
|
@Property({ nullable: true, columnType: 'timestamptz' })
|
||||||
|
readAt?: Date;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,71 @@
|
|||||||
|
import { TicketRepository } from "../repositories/tickets.repository";
|
||||||
import { InjectQueue } from "@nestjs/bullmq";
|
import { InjectQueue } from "@nestjs/bullmq";
|
||||||
import { BadRequestException, HttpException, Injectable, InternalServerErrorException, Logger, OnModuleInit } from "@nestjs/common";
|
import { BadRequestException, HttpException, Injectable, InternalServerErrorException, Logger, OnModuleInit } from "@nestjs/common";
|
||||||
import { Queue } from "bullmq";
|
import { Queue } from "bullmq";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
// import { DataSource, MoreThanOrEqual, Not, QueryRunner } from "typeorm";
|
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 { UserRepository } from "src/modules/user/repositories/user.repository";
|
||||||
|
import { AdminRepository } from "src/modules/admin/repositories/admin.repository";
|
||||||
|
import { Admin } from "src/modules/admin/entities/admin.entity";
|
||||||
|
|
||||||
// import { TICKET } from "../constant";
|
|
||||||
// import { ReferralService } from "./referral.service";
|
|
||||||
// import { IUserIpAndHeaders } from "../../../common/decorators/user.decorator";
|
|
||||||
// import { ParamDto } from "../../../common/DTO/param.dto";
|
|
||||||
// import { CommonMessage, TicketMessageEnum, UserMessage } from "../../../common/enums/message.enum";
|
|
||||||
// import { UserType } from "../../access-logs/enums/user-type.enum";
|
|
||||||
// import { AccessLogService } from "../../access-logs/providers/access-log.service";
|
|
||||||
// import { DanakService } from "../../danak-services/entities/danak-service.entity";
|
|
||||||
// import { NotificationQueue } from "../../notifications/queue/notification.queue";
|
|
||||||
// import { SupportPlanFeatureKey } from "../../support-plans/enums/support-plan-feature-key.enum";
|
|
||||||
// import { SupportPlansService } from "../../support-plans/providers/support-plans.service";
|
|
||||||
// import { User } from "../../users/entities/user.entity";
|
|
||||||
// import { AdminsService } from "../../users/providers/admins.service";
|
|
||||||
// import { UsersService } from "../../users/providers/users.service";
|
|
||||||
// import { CreateTicketCategoryDto } from "../DTO/create-ticket-category.dto";
|
|
||||||
// import { CreateTicketMessageDto } from "../DTO/create-ticket-message.dto";
|
|
||||||
// import { CreateTicketByAdminDto, CreateTicketDto } from "../DTO/create-ticket.dto";
|
|
||||||
// import { ReferTicketDto } from "../DTO/refer-ticket.dto";
|
|
||||||
// import { SearchTicketCategoryDto } from "../DTO/search-ticket-category.dto";
|
|
||||||
// import { SearchTicketQueryDto } from "../DTO/search-ticket-query.dto";
|
|
||||||
// import { UpdateTicketCategoryDto } from "../DTO/update-ticket-category.dto";
|
|
||||||
// import { TicketCategory } from "../entities/ticket-category.entity";
|
|
||||||
// import { TicketMessageAttachment } from "../entities/ticket-message-attachment";
|
|
||||||
// import { TicketMessage } from "../entities/ticket-message.entity";
|
|
||||||
// import { Ticket } from "../entities/ticket.entity";
|
|
||||||
// import { TicketStatus } from "../enums/ticket-status.enum";
|
|
||||||
// import { TicketCategoryRepository } from "../repositories/tickets-category.repository";
|
|
||||||
// import { TicketMessagesRepository } from "../repositories/tickets-message.repository";
|
|
||||||
// import { TicketsRepository } from "../repositories/tickets.repository";
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TicketService {
|
export class TicketService {
|
||||||
private readonly logger = new Logger(TicketService.name);
|
private readonly logger = new Logger(TicketService.name);
|
||||||
|
|
||||||
// constructor(
|
constructor(
|
||||||
// @InjectQueue(TICKET.QUEUE_NAME) private readonly ticketQueue: Queue,
|
private readonly ticketsRepository: TicketRepository,
|
||||||
// private readonly ticketsRepository: TicketsRepository,
|
private readonly orderRepository: OrderRepository,
|
||||||
// private readonly ticketsCategoryRepository: TicketCategoryRepository,
|
private readonly userRepository: UserRepository,
|
||||||
// private readonly ticketMessagesRepository: TicketMessagesRepository,
|
private readonly adminRepository: AdminRepository,
|
||||||
// private readonly usersService: UsersService,
|
|
||||||
// private readonly adminsService: AdminsService,
|
// private readonly adminsService: AdminsService,
|
||||||
// private readonly notificationQueue: NotificationQueue,
|
) { }
|
||||||
// private readonly referralService: ReferralService,
|
|
||||||
// private readonly dataSource: DataSource,
|
|
||||||
// private readonly supportPlansService: SupportPlansService,
|
|
||||||
// private readonly accessLogService: AccessLogService,
|
|
||||||
// ) {}
|
|
||||||
|
|
||||||
|
async createTicket(orderId: string, dto: CreateTicketDto, userId?: string, adminId?: string) {
|
||||||
|
const { content, attachments } = dto
|
||||||
|
if (!userId && !adminId) {
|
||||||
|
throw new BadRequestException("One of userId ord adminId is required")
|
||||||
|
}
|
||||||
|
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
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
user = await this.userRepository.findOne({ id: userId })
|
||||||
|
if (!user) {
|
||||||
|
throw new BadRequestException("User not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (adminId) {
|
||||||
|
admin = await this.adminRepository.findOne({ id: adminId })
|
||||||
|
if (!admin) {
|
||||||
|
throw new BadRequestException("Admin not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user && !admin) {
|
||||||
|
throw new BadRequestException("One of user or admin is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticket = this.ticketsRepository.create({
|
||||||
|
content,
|
||||||
|
attachments,
|
||||||
|
admin,
|
||||||
|
user,
|
||||||
|
order,
|
||||||
|
})
|
||||||
|
|
||||||
|
await this.ticketsRepository.em.persistAndFlush(ticket)
|
||||||
|
|
||||||
|
return ticket
|
||||||
|
|
||||||
|
}
|
||||||
// async onModuleInit() {
|
// async onModuleInit() {
|
||||||
// await this.scheduleAutoCloseJob();
|
// await this.scheduleAutoCloseJob();
|
||||||
// }
|
// }
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import { TicketService } from "./providers/tickets.service";
|
|||||||
|
|
||||||
|
|
||||||
import { ParamDto } from "../../common/DTO/param.dto";
|
import { ParamDto } from "../../common/DTO/param.dto";
|
||||||
|
import { UserId } from "src/common/decorators";
|
||||||
|
import { AdminId } from "src/common/decorators/admin-id.decorator";
|
||||||
|
|
||||||
@Controller("tickets")
|
@Controller("tickets")
|
||||||
@ApiTags("Tickets")
|
@ApiTags("Tickets")
|
||||||
@@ -62,12 +64,17 @@ export class TicketsController {
|
|||||||
|
|
||||||
//----------------- ticket and ticket messages -------------------
|
//----------------- ticket and ticket messages -------------------
|
||||||
|
|
||||||
// @ApiOperation({ summary: "create ticket ==> user route" })
|
@Post('public/ticket/order/:orderId')
|
||||||
// @Post()
|
@ApiOperation({ summary: "create ticket ==> user route" })
|
||||||
// createTicket(@Body() createDto: CreateTicketDto, @UserDec("id") userId: string) {
|
createTicket(@Param('orderId') orderId: string, @Body() createDto: CreateTicketDto, @UserId() userId: string) {
|
||||||
// return this.ticketsService.createTicket(createDto, userId);
|
return this.ticketsService.createTicket(orderId, createDto, userId);
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
@Post('admin/ticket/order/:orderId')
|
||||||
|
@ApiOperation({ summary: "create ticket ==> admin route" })
|
||||||
|
createTicketAsAdmin(@Param('orderId') orderId: string, @Body() createDto: CreateTicketDto, @AdminId() adminId: string) {
|
||||||
|
return this.ticketsService.createTicket(orderId, createDto, undefined, adminId);
|
||||||
|
}
|
||||||
// @ApiOperation({ summary: "create ticket by admin" })
|
// @ApiOperation({ summary: "create ticket by admin" })
|
||||||
// @AdminRoute()
|
// @AdminRoute()
|
||||||
// @PermissionsDec(PermissionEnum.TICKETS)
|
// @PermissionsDec(PermissionEnum.TICKETS)
|
||||||
|
|||||||
Reference in New Issue
Block a user