har delete request

This commit is contained in:
2026-07-02 22:18:20 +03:30
parent d69a262d5c
commit f4f05babe8
3 changed files with 35 additions and 18 deletions
@@ -141,6 +141,11 @@ export class ChattService {
return true return true
} }
async deleteChatsByRefId(refId: string, em: EntityManager = this.em) {
await em.nativeUpdate(Chat, { refId, parent: { $ne: null } }, { parent: null });
await em.nativeDelete(Chat, { refId });
}
async updateChat(chat: Chat, dto: UpdateChatDto) { async updateChat(chat: Chat, dto: UpdateChatDto) {
this.chatRepository.assign(chat, dto, { this.chatRepository.assign(chat, dto, {
+2
View File
@@ -13,6 +13,7 @@ import { JwtModule } from '@nestjs/jwt';
import { AdminModule } from '../admin/admin.module'; import { AdminModule } from '../admin/admin.module';
import { NotificationsModule } from '../notification/notifications.module'; import { NotificationsModule } from '../notification/notifications.module';
import { RequestListeners } from './listeners/request.listeners'; import { RequestListeners } from './listeners/request.listeners';
import { ChatModule } from '../chat/chat.module';
@Module({ @Module({
imports: [ imports: [
@@ -24,6 +25,7 @@ import { RequestListeners } from './listeners/request.listeners';
AuthModule, AuthModule,
AdminModule, AdminModule,
NotificationsModule, NotificationsModule,
ChatModule,
], ],
controllers: [RequestController], controllers: [RequestController],
providers: [RequestService, RequestRepository, RequestListeners], providers: [RequestService, RequestRepository, RequestListeners],
+22 -12
View File
@@ -6,11 +6,13 @@ import { UpdateRequestDto } from './dto/update-request.dto';
import { FindRequestsDto } from './dto/find-requests.dto'; import { FindRequestsDto } from './dto/find-requests.dto';
import { Request } from './entities/request.entity'; import { Request } from './entities/request.entity';
import { RequestItem } from './entities/request-item.entity'; import { RequestItem } from './entities/request-item.entity';
import { Invoice } from '../invoice/entities/invoice.entity';
import { UserService } from '../user/providers/user.service'; import { UserService } from '../user/providers/user.service';
import { ProductService } from '../product/providers/product.service'; import { ProductService } from '../product/providers/product.service';
import { FieldRepository } from '../form-builder/repository/field.repository'; import { FieldRepository } from '../form-builder/repository/field.repository';
import { RequestRepository } from './repositories/request.repository'; import { RequestRepository } from './repositories/request.repository';
import { RequestCreatedEvent } from './events/request.events'; import { RequestCreatedEvent } from './events/request.events';
import { ChattService } from '../chat/providers/chat.service';
@Injectable() @Injectable()
export class RequestService { export class RequestService {
@@ -21,6 +23,7 @@ export class RequestService {
private readonly productService: ProductService, private readonly productService: ProductService,
private readonly fieldRepository: FieldRepository, private readonly fieldRepository: FieldRepository,
private readonly eventEmitter: EventEmitter2, private readonly eventEmitter: EventEmitter2,
private readonly chatService: ChattService,
) { } ) { }
async create(createRequestDto: CreateRequestDto, userId: string) { async create(createRequestDto: CreateRequestDto, userId: string) {
@@ -193,26 +196,33 @@ export class RequestService {
} }
async remove(id: string, userId: string) { async remove(id: string, userId: string) {
const request = await this.requestRepository.findOne( return this.em.transactional(async (em) => {
{ id, user: { id: userId } }, const request = await em.findOne(Request, { id, user: { id: userId } });
);
if (!request) { if (!request) {
throw new NotFoundException('Request not found'); throw new NotFoundException('Request not found');
} }
request.deletedAt = new Date();
await this.em.flush(); await this.hardDeleteRequest(id, em);
return request; return { message: 'Request deleted successfully' };
});
} }
async removeAsAdmin(id: string) { async removeAsAdmin(id: string) {
const request = await this.requestRepository.findOne( return this.em.transactional(async (em) => {
{ id }, const request = await em.findOne(Request, { id });
);
if (!request) { if (!request) {
throw new NotFoundException('Request not found'); throw new NotFoundException('Request not found');
} }
request.deletedAt = new Date();
await this.em.flush(); await this.hardDeleteRequest(id, em);
return request; return { message: 'Request deleted successfully' };
});
}
private async hardDeleteRequest(id: string, em: EntityManager) {
await this.chatService.deleteChatsByRefId(id, em);
await em.nativeUpdate(Invoice, { request: { id } }, { request: null });
await em.nativeDelete(RequestItem, { request: { id } });
await em.nativeDelete(Request, { id });
} }
} }