har delete request
This commit is contained in:
@@ -141,6 +141,11 @@ export class ChattService {
|
||||
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) {
|
||||
|
||||
this.chatRepository.assign(chat, dto, {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { JwtModule } from '@nestjs/jwt';
|
||||
import { AdminModule } from '../admin/admin.module';
|
||||
import { NotificationsModule } from '../notification/notifications.module';
|
||||
import { RequestListeners } from './listeners/request.listeners';
|
||||
import { ChatModule } from '../chat/chat.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -24,6 +25,7 @@ import { RequestListeners } from './listeners/request.listeners';
|
||||
AuthModule,
|
||||
AdminModule,
|
||||
NotificationsModule,
|
||||
ChatModule,
|
||||
],
|
||||
controllers: [RequestController],
|
||||
providers: [RequestService, RequestRepository, RequestListeners],
|
||||
|
||||
@@ -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 } },
|
||||
);
|
||||
return this.em.transactional(async (em) => {
|
||||
const request = await em.findOne(Request, { id, user: { id: userId } });
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
request.deletedAt = new Date();
|
||||
await this.em.flush();
|
||||
return request;
|
||||
|
||||
await this.hardDeleteRequest(id, em);
|
||||
return { message: 'Request deleted successfully' };
|
||||
});
|
||||
}
|
||||
|
||||
async removeAsAdmin(id: string) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id },
|
||||
);
|
||||
return this.em.transactional(async (em) => {
|
||||
const request = await em.findOne(Request, { id });
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
request.deletedAt = new Date();
|
||||
await this.em.flush();
|
||||
return request;
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user