From 79952c117f353dd5c2200fb8bc9cccf6c69a0f77 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 21 Jul 2026 12:18:16 +0330 Subject: [PATCH] update order --- negareh.code-workspace | 14 +++++++ src/modules/chat/providers/chat.service.ts | 22 +++++++++++ src/modules/request/dto/create-request.dto.ts | 38 +++++++++---------- .../request/entities/request-item.entity.ts | 9 +---- src/modules/request/request.service.ts | 33 +++++++++++----- 5 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 negareh.code-workspace diff --git a/negareh.code-workspace b/negareh.code-workspace new file mode 100644 index 0000000..2d812ac --- /dev/null +++ b/negareh.code-workspace @@ -0,0 +1,14 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../negareh-admin" + }, + { + "path": "../negareh-console" + } + ], + "settings": {} +} \ No newline at end of file diff --git a/src/modules/chat/providers/chat.service.ts b/src/modules/chat/providers/chat.service.ts index abecbff..4730f99 100755 --- a/src/modules/chat/providers/chat.service.ts +++ b/src/modules/chat/providers/chat.service.ts @@ -115,6 +115,28 @@ export class ChattService { return data } + createChatInTransaction( + em: EntityManager, + refId: string, + dto: Pick, + user: User, + ): Chat | null { + const { content = '', attachments } = dto; + + if (!content.trim() && (!attachments || attachments.length === 0)) { + return null; + } + + const chat = em.create(Chat, { + content, + attachments, + user, + refId, + }); + em.persist(chat); + return chat; + } + async createChat(refId: string, dto: CreateChatDto, user?: User, admin?: Admin) { const { content = '', attachments, parentId } = dto diff --git a/src/modules/request/dto/create-request.dto.ts b/src/modules/request/dto/create-request.dto.ts index b2badeb..b5845e2 100644 --- a/src/modules/request/dto/create-request.dto.ts +++ b/src/modules/request/dto/create-request.dto.ts @@ -40,6 +40,24 @@ export class CreateRequestItemDto { @ValidateNested({ each: true }) @Type(() => RequestItemDto) attributes?: RequestItemDto[]; +} + +export class CreateRequestDto { + @IsArray() + @ApiProperty({ + isArray: true, + type: [CreateRequestItemDto], + example: [ + { + productId: '01ARZ3NDEKTSV4RRFFQ69G5FAV', + attributes: [{ fieldId: 'field_01', value: 'blue' }], + }, + ], + }) + @IsNotEmpty() + @ArrayMinSize(1, { message: 'At least one item is required' }) + @Type(() => CreateRequestItemDto) + items: CreateRequestItemDto[]; @ApiPropertyOptional() @IsOptional() @@ -53,23 +71,3 @@ export class CreateRequestItemDto { @Type(() => AttachmentDto) attachments?: IAttachment[]; } - -export class CreateRequestDto { - @IsArray() - @ApiProperty({ - isArray: true, - type: [CreateRequestItemDto], - example: [ - { - productId: '01ARZ3NDEKTSV4RRFFQ69G5FAV', - attributes: [{ fieldId: 'field_01', value: 'blue' }], - attachments: [{ type: 'image', url: 'https://example.com/photo.jpg' }], - description: 'توضیحات', - }, - ], - }) - @IsNotEmpty() - @ArrayMinSize(1, { message: 'At least one item is required' }) - @Type(() => CreateRequestItemDto) - items: CreateRequestItemDto[]; -} diff --git a/src/modules/request/entities/request-item.entity.ts b/src/modules/request/entities/request-item.entity.ts index 219d5b4..51eff9b 100644 --- a/src/modules/request/entities/request-item.entity.ts +++ b/src/modules/request/entities/request-item.entity.ts @@ -2,7 +2,7 @@ import { Entity, Index, ManyToOne, OptionalProps, Property } from '@mikro-orm/co import { Product } from 'src/modules/product/entities/product.entity'; import { BaseEntity } from 'src/common/entities/base.entity'; import { Request } from './request.entity'; -import { IAttachment, IField } from '../interface/request.interface'; +import { IField } from '../interface/request.interface'; @Entity({ tableName: 'request_items' }) @Index({ properties: ['request'] }) @@ -17,11 +17,4 @@ export class RequestItem extends BaseEntity { @ManyToOne(() => Request) request!: Request; - - @Property({ type: 'text', nullable: true }) - description?: string; - - @Property({ type: 'json', nullable: true }) - attachments?: IAttachment[] // user attachments like voice and photos - } diff --git a/src/modules/request/request.service.ts b/src/modules/request/request.service.ts index d866636..0bf9b07 100644 --- a/src/modules/request/request.service.ts +++ b/src/modules/request/request.service.ts @@ -27,7 +27,7 @@ export class RequestService { ) { } async create(createRequestDto: CreateRequestDto, userId: string) { - const { items } = createRequestDto; + const { items, description, attachments } = createRequestDto; const user = await this.userService.findOrFail(userId); @@ -43,12 +43,17 @@ export class RequestService { request, product, attributes: item.attributes, - description: item.description, - attachments: item.attachments, }); request.items.add(requestItem); } + this.chatService.createChatInTransaction( + em, + request.id, + { content: description ?? '', attachments }, + user, + ); + await em.flush(); return request; }); @@ -134,7 +139,7 @@ export class RequestService { throw new NotFoundException('Request not found'); } - const { items } = updateRequestDto; + const { items, description, attachments } = updateRequestDto; if (items?.length) { return this.em.transactional(async (em) => { request.items.removeAll(); @@ -146,12 +151,17 @@ export class RequestService { request, product, attributes: item.attributes, - description: item.description, - attachments: item.attachments, }); request.items.add(requestItem); } + this.chatService.createChatInTransaction( + em, + request.id, + { content: description ?? '', attachments }, + request.user, + ); + await em.flush(); return request; }); @@ -169,7 +179,7 @@ export class RequestService { throw new NotFoundException('Request not found'); } - const { items } = updateRequestDto; + const { items, description, attachments } = updateRequestDto; if (items?.length) { return this.em.transactional(async (em) => { request.items.removeAll(); @@ -181,12 +191,17 @@ export class RequestService { request, product, attributes: item.attributes, - description: item.description, - attachments: item.attachments, }); request.items.add(requestItem); } + this.chatService.createChatInTransaction( + em, + request.id, + { content: description ?? '', attachments }, + request.user, + ); + await em.flush(); return request; });