mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: add bulk delete functionality
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 } from '@nestjs/testing';
|
||||
@@ -143,6 +143,44 @@ describe('ContextVarController', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteMany', () => {
|
||||
const deleteResult = { acknowledged: true, deletedCount: 2 };
|
||||
|
||||
it('should delete contextVars when valid IDs are provided', async () => {
|
||||
jest
|
||||
.spyOn(contextVarService, 'deleteMany')
|
||||
.mockResolvedValue(deleteResult);
|
||||
const result = await contextVarController.deleteMany([
|
||||
contextVarToDelete.id,
|
||||
contextVar.id,
|
||||
]);
|
||||
|
||||
expect(contextVarService.deleteMany).toHaveBeenCalledWith({
|
||||
_id: { $in: [contextVarToDelete.id, contextVar.id] },
|
||||
});
|
||||
expect(result).toEqual(deleteResult);
|
||||
});
|
||||
|
||||
it('should throw BadRequestException when no IDs are provided', async () => {
|
||||
await expect(contextVarController.deleteMany([])).rejects.toThrow(
|
||||
new BadRequestException('No IDs provided for deletion.'),
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw NotFoundException when no contextVars are deleted', async () => {
|
||||
jest.spyOn(contextVarService, 'deleteMany').mockResolvedValue({
|
||||
acknowledged: true,
|
||||
deletedCount: 0,
|
||||
});
|
||||
|
||||
await expect(
|
||||
contextVarController.deleteMany([contextVarToDelete.id, contextVar.id]),
|
||||
).rejects.toThrow(
|
||||
new NotFoundException('Context vars with provided IDs not found'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateOne', () => {
|
||||
const contextVarUpdatedDto: ContextVarUpdateDto = {
|
||||
name: 'updated_context_var_name',
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
@@ -144,4 +145,31 @@ export class ContextVarController extends BaseController<ContextVar> {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes multiple categories by their IDs.
|
||||
* @param ids - IDs of categories 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.contextVarService.deleteMany({
|
||||
_id: { $in: ids },
|
||||
});
|
||||
|
||||
if (deleteResult.deletedCount === 0) {
|
||||
this.logger.warn(
|
||||
`Unable to delete context vars with provided IDs: ${ids}`,
|
||||
);
|
||||
throw new NotFoundException('Context vars with provided IDs not found');
|
||||
}
|
||||
|
||||
this.logger.log(`Successfully deleted context vars with IDs: ${ids}`);
|
||||
return deleteResult;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user