@@ -1,19 +1,24 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString, IsUrl, Length } from "class-validator";
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString, Length } from "class-validator";
|
||||
import { AttachmentType } from "../interfaces/chat";
|
||||
import { TicketMessageEnum } from "../../../common/enums/message.enum";
|
||||
|
||||
|
||||
export class CreateChatDto {
|
||||
@IsNotEmpty({ message: TicketMessageEnum.MESSAGE_REQUIRED })
|
||||
@IsOptional()
|
||||
@IsString({ message: TicketMessageEnum.MESSAGE_STRING })
|
||||
@Length(1, 5000, { message: TicketMessageEnum.MESSAGE_LENGTH })
|
||||
@ApiProperty({ description: "The message content of the ticket" })
|
||||
content: string;
|
||||
@Length(0, 5000, { message: TicketMessageEnum.MESSAGE_LENGTH })
|
||||
@ApiProperty({ description: "The message content of the ticket", required: false })
|
||||
content: string = '';
|
||||
|
||||
@IsOptional()
|
||||
@IsNotEmpty({ message: TicketMessageEnum.ATTACHMENT_REQUIRED })
|
||||
@ApiProperty({ description: "attachment url", example: [{ type: '', url: '' }] })
|
||||
attachments?: AttachmentType[];
|
||||
|
||||
@ApiPropertyOptional({ description: "Parent chat ID for reply" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
parentId?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,18 +116,31 @@ export class ChattService {
|
||||
}
|
||||
|
||||
async createChat(refId: string, dto: CreateChatDto, user?: User, admin?: Admin) {
|
||||
const { content, attachments } = dto
|
||||
const { content = '', attachments, parentId } = dto
|
||||
|
||||
if (!user && !admin) {
|
||||
throw new BadRequestException("One of user or admin is required")
|
||||
}
|
||||
|
||||
if (!content.trim() && (!attachments || attachments.length === 0)) {
|
||||
throw new BadRequestException("Message content or attachment is required")
|
||||
}
|
||||
|
||||
let parent: Chat | undefined
|
||||
if (parentId) {
|
||||
parent = await this.findOneOrFail(parentId)
|
||||
if (parent.refId !== refId) {
|
||||
throw new BadRequestException("Parent message does not belong to this chat")
|
||||
}
|
||||
}
|
||||
|
||||
const ticket = this.chatRepository.create({
|
||||
content,
|
||||
attachments,
|
||||
admin,
|
||||
user,
|
||||
refId
|
||||
refId,
|
||||
parent,
|
||||
})
|
||||
|
||||
await this.chatRepository.em.persistAndFlush(ticket)
|
||||
@@ -137,6 +150,7 @@ export class ChattService {
|
||||
}
|
||||
|
||||
async deleteChat(chat: Chat) {
|
||||
await this.em.nativeUpdate(Chat, { parent: chat.id }, { parent: null })
|
||||
await this.em.removeAndFlush(chat)
|
||||
return true
|
||||
}
|
||||
@@ -165,7 +179,7 @@ export class ChattService {
|
||||
|
||||
async findOneOrFail(ticketId: string) {
|
||||
const ticket = await this.chatRepository.findOne({ id: ticketId },
|
||||
{ populate: ['user'] })
|
||||
{ populate: ['user', 'admin', 'parent'] })
|
||||
if (!ticket) {
|
||||
throw new BadRequestException('ticket not found!')
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ export class ChatRepository extends EntityRepository<Chat> {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
populate: ['admin', 'user']
|
||||
populate: ['admin', 'user', 'parent', 'parent.admin', 'parent.user'],
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user