From aa25ce6b03682d414c36c39cb8433d4f9c3d2535 Mon Sep 17 00:00:00 2001 From: hexastack Date: Tue, 8 Oct 2024 08:12:38 +0100 Subject: [PATCH] feat: add bulk delete functionality --- .../context-var.controller.spec.ts | 40 ++++++++++++++- .../controllers/context-var.controller.ts | 28 ++++++++++ .../src/components/context-vars/index.tsx | 51 +++++++++++++++++-- 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/api/src/chat/controllers/context-var.controller.spec.ts b/api/src/chat/controllers/context-var.controller.spec.ts index 7adfdafd..b7101fd9 100644 --- a/api/src/chat/controllers/context-var.controller.spec.ts +++ b/api/src/chat/controllers/context-var.controller.spec.ts @@ -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', diff --git a/api/src/chat/controllers/context-var.controller.ts b/api/src/chat/controllers/context-var.controller.ts index 962f043e..204c7498 100644 --- a/api/src/chat/controllers/context-var.controller.ts +++ b/api/src/chat/controllers/context-var.controller.ts @@ -7,6 +7,7 @@ */ import { + BadRequestException, Body, Controller, Delete, @@ -144,4 +145,31 @@ export class ContextVarController extends BaseController { } 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 { + 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; + } } diff --git a/frontend/src/components/context-vars/index.tsx b/frontend/src/components/context-vars/index.tsx index 3cb5e1c5..6368b97b 100644 --- a/frontend/src/components/context-vars/index.tsx +++ b/frontend/src/components/context-vars/index.tsx @@ -8,9 +8,10 @@ import { faAsterisk } from "@fortawesome/free-solid-svg-icons"; import AddIcon from "@mui/icons-material/Add"; +import DeleteIcon from "@mui/icons-material/Delete"; import { Button, Grid, Paper, Switch } from "@mui/material"; -import { GridColDef } from "@mui/x-data-grid"; -import React from "react"; +import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid"; +import React, { useState } from "react"; import { DeleteDialog } from "@/app-components/dialogs/DeleteDialog"; import { FilterTextfield } from "@/app-components/inputs/FilterTextfield"; @@ -21,6 +22,7 @@ import { import { renderHeader } from "@/app-components/tables/columns/renderHeader"; import { DataGrid } from "@/app-components/tables/DataGrid"; import { useDelete } from "@/hooks/crud/useDelete"; +import { useDeleteMany } from "@/hooks/crud/useDeleteMany"; import { useFind } from "@/hooks/crud/useFind"; import { useUpdate } from "@/hooks/crud/useUpdate"; import { getDisplayDialogs, useDialog } from "@/hooks/useDialog"; @@ -66,9 +68,24 @@ export const ContextVars = () => { }, onSuccess() { deleteDialogCtl.closeDialog(); + setSelectedContextVars([]); toast.success(t("message.item_delete_success")); }, }); + const { mutateAsync: deleteContextVars } = useDeleteMany( + EntityType.CONTEXT_VAR, + { + onError: (error) => { + toast.error(error.message || t("message.internal_server_error")); + }, + onSuccess: () => { + deleteDialogCtl.closeDialog(); + setSelectedContextVars([]); + toast.success(t("message.item_delete_success")); + }, + }, + ); + const [selectedContextVars, setSelectedContextVars] = useState([]); const actionColumns = useActionColumns( EntityType.CONTEXT_VAR, [ @@ -143,6 +160,9 @@ export const ContextVars = () => { }, actionColumns, ]; + const handleSelectionChange = (selection: GridRowSelectionModel) => { + setSelectedContextVars(selection as string[]); + }; return ( @@ -152,7 +172,13 @@ export const ContextVars = () => { { - if (deleteDialogCtl?.data) deleteContextVar(deleteDialogCtl.data); + if (selectedContextVars.length > 0) { + deleteContextVars(selectedContextVars); + setSelectedContextVars([]); + deleteDialogCtl.closeDialog(); + } else if (deleteDialogCtl?.data) { + deleteContextVar(deleteDialogCtl.data); + } }} /> @@ -179,12 +205,29 @@ export const ContextVars = () => { ) : null} + {selectedContextVars.length > 0 && ( + + + + )} - +