mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: add bulk delete
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
@@ -40,6 +40,7 @@ describe('NlpValueController', () => {
|
||||
let nlpEntityService: NlpEntityService;
|
||||
let jhonNlpValue: NlpValue;
|
||||
let positiveValue: NlpValue;
|
||||
let negativeValue: NlpValue;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
@@ -67,6 +68,7 @@ describe('NlpValueController', () => {
|
||||
nlpEntityService = module.get<NlpEntityService>(NlpEntityService);
|
||||
jhonNlpValue = await nlpValueService.findOne({ value: 'jhon' });
|
||||
positiveValue = await nlpValueService.findOne({ value: 'positive' });
|
||||
negativeValue = await nlpValueService.findOne({ value: 'negative' });
|
||||
});
|
||||
afterAll(async () => {
|
||||
await closeInMongodConnection();
|
||||
@@ -228,4 +230,34 @@ describe('NlpValueController', () => {
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
describe('deleteMany', () => {
|
||||
it('should delete multiple nlp values', async () => {
|
||||
const valuesToDelete = [positiveValue.id, negativeValue.id];
|
||||
|
||||
const result = await nlpValueController.deleteMany(valuesToDelete);
|
||||
|
||||
expect(result.deletedCount).toEqual(valuesToDelete.length);
|
||||
const remainingValues = await nlpValueService.find({
|
||||
_id: { $in: valuesToDelete },
|
||||
});
|
||||
expect(remainingValues.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should throw BadRequestException when no IDs are provided', async () => {
|
||||
await expect(nlpValueController.deleteMany([])).rejects.toThrow(
|
||||
BadRequestException,
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw NotFoundException when provided IDs do not exist', async () => {
|
||||
const nonExistentIds = [
|
||||
'614c1b2f58f4f04c876d6b8d',
|
||||
'614c1b2f58f4f04c876d6b8e',
|
||||
];
|
||||
|
||||
await expect(
|
||||
nlpValueController.deleteMany(nonExistentIds),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
Query,
|
||||
NotFoundException,
|
||||
UseInterceptors,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
|
||||
import { TFilterQuery } from 'mongoose';
|
||||
@@ -25,6 +26,7 @@ import { TFilterQuery } from 'mongoose';
|
||||
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
|
||||
import { LoggerService } from '@/logger/logger.service';
|
||||
import { BaseController } from '@/utils/generics/base-controller';
|
||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||
import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe';
|
||||
import { PopulatePipe } from '@/utils/pipes/populate.pipe';
|
||||
@@ -193,4 +195,29 @@ export class NlpValueController extends BaseController<
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes multiple NLP values by their IDs.
|
||||
* @param ids - IDs of NLP values to be deleted.
|
||||
* @returns A Promise that resolves to the deletion result.
|
||||
*/
|
||||
@CsrfCheck(true)
|
||||
@Delete('')
|
||||
@HttpCode(204)
|
||||
async deleteMany(@Body('ids') ids: string[]): Promise<DeleteResult> {
|
||||
if (!ids || ids.length === 0) {
|
||||
throw new BadRequestException('No IDs provided for deletion.');
|
||||
}
|
||||
const deleteResult = await this.nlpValueService.deleteMany({
|
||||
_id: { $in: ids },
|
||||
});
|
||||
|
||||
if (deleteResult.deletedCount === 0) {
|
||||
this.logger.warn(`Unable to delete NLP values with provided IDs: ${ids}`);
|
||||
throw new NotFoundException('NLP values with provided IDs not found');
|
||||
}
|
||||
|
||||
this.logger.log(`Successfully deleted NLP values with IDs: ${ids}`);
|
||||
return deleteResult;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user