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
+28 -18
View File
@@ -6,11 +6,13 @@ import { UpdateRequestDto } from './dto/update-request.dto';
import { FindRequestsDto } from './dto/find-requests.dto';
import { Request } from './entities/request.entity';
import { RequestItem } from './entities/request-item.entity';
import { Invoice } from '../invoice/entities/invoice.entity';
import { UserService } from '../user/providers/user.service';
import { ProductService } from '../product/providers/product.service';
import { FieldRepository } from '../form-builder/repository/field.repository';
import { RequestRepository } from './repositories/request.repository';
import { RequestCreatedEvent } from './events/request.events';
import { ChattService } from '../chat/providers/chat.service';
@Injectable()
export class RequestService {
@@ -21,6 +23,7 @@ export class RequestService {
private readonly productService: ProductService,
private readonly fieldRepository: FieldRepository,
private readonly eventEmitter: EventEmitter2,
private readonly chatService: ChattService,
) { }
async create(createRequestDto: CreateRequestDto, userId: string) {
@@ -193,26 +196,33 @@ export class RequestService {
}
async remove(id: string, userId: string) {
const request = await this.requestRepository.findOne(
{ id, user: { id: userId } },
);
if (!request) {
throw new NotFoundException('Request not found');
}
request.deletedAt = new Date();
await this.em.flush();
return request;
return this.em.transactional(async (em) => {
const request = await em.findOne(Request, { id, user: { id: userId } });
if (!request) {
throw new NotFoundException('Request not found');
}
await this.hardDeleteRequest(id, em);
return { message: 'Request deleted successfully' };
});
}
async removeAsAdmin(id: string) {
const request = await this.requestRepository.findOne(
{ id },
);
if (!request) {
throw new NotFoundException('Request not found');
}
request.deletedAt = new Date();
await this.em.flush();
return request;
return this.em.transactional(async (em) => {
const request = await em.findOne(Request, { id });
if (!request) {
throw new NotFoundException('Request not found');
}
await this.hardDeleteRequest(id, em);
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 });
}
}