mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #191 from Hexastack/189-request-add-bulk-delete-functionality-to-nlu-entities-list
feat: [NLP Entity] add bulk delete
This commit is contained in:
commit
afefef2acc
@ -6,7 +6,11 @@
|
|||||||
* 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).
|
* 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 { MethodNotAllowedException, NotFoundException } from '@nestjs/common';
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
MethodNotAllowedException,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { MongooseModule } from '@nestjs/mongoose';
|
import { MongooseModule } from '@nestjs/mongoose';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
@ -258,4 +262,45 @@ describe('NlpEntityController', () => {
|
|||||||
).rejects.toThrow(MethodNotAllowedException);
|
).rejects.toThrow(MethodNotAllowedException);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('deleteMany', () => {
|
||||||
|
it('should delete multiple nlp entities', async () => {
|
||||||
|
const entitiesToDelete = [
|
||||||
|
(
|
||||||
|
await nlpEntityService.findOne({
|
||||||
|
name: 'sentiment',
|
||||||
|
})
|
||||||
|
)?.id,
|
||||||
|
(
|
||||||
|
await nlpEntityService.findOne({
|
||||||
|
name: 'updated',
|
||||||
|
})
|
||||||
|
)?.id,
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await nlpEntityController.deleteMany(entitiesToDelete);
|
||||||
|
|
||||||
|
expect(result.deletedCount).toEqual(entitiesToDelete.length);
|
||||||
|
const remainingEntities = await nlpEntityService.find({
|
||||||
|
_id: { $in: entitiesToDelete },
|
||||||
|
});
|
||||||
|
expect(remainingEntities.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw BadRequestException when no IDs are provided', async () => {
|
||||||
|
await expect(nlpEntityController.deleteMany([])).rejects.toThrow(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw NotFoundException when provided IDs do not exist', async () => {
|
||||||
|
const nonExistentIds = [
|
||||||
|
'614c1b2f58f4f04c876d6b8d',
|
||||||
|
'614c1b2f58f4f04c876d6b8e',
|
||||||
|
];
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
nlpEntityController.deleteMany(nonExistentIds),
|
||||||
|
).rejects.toThrow(NotFoundException);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
BadRequestException,
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
@ -27,6 +28,7 @@ import { TFilterQuery } from 'mongoose';
|
|||||||
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
|
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
|
||||||
import { LoggerService } from '@/logger/logger.service';
|
import { LoggerService } from '@/logger/logger.service';
|
||||||
import { BaseController } from '@/utils/generics/base-controller';
|
import { BaseController } from '@/utils/generics/base-controller';
|
||||||
|
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||||
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||||
import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe';
|
import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe';
|
||||||
import { PopulatePipe } from '@/utils/pipes/populate.pipe';
|
import { PopulatePipe } from '@/utils/pipes/populate.pipe';
|
||||||
@ -211,4 +213,31 @@ export class NlpEntityController extends BaseController<
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes multiple NLP entities by their IDs.
|
||||||
|
* @param ids - IDs of NLP entities 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.nlpEntityService.deleteMany({
|
||||||
|
_id: { $in: ids },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (deleteResult.deletedCount === 0) {
|
||||||
|
this.logger.warn(
|
||||||
|
`Unable to delete NLP entities with provided IDs: ${ids}`,
|
||||||
|
);
|
||||||
|
throw new NotFoundException('NLP entities with provided IDs not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Successfully deleted NLP entities with IDs: ${ids}`);
|
||||||
|
return deleteResult;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import AddIcon from "@mui/icons-material/Add";
|
import AddIcon from "@mui/icons-material/Add";
|
||||||
|
import DeleteIcon from "@mui/icons-material/Delete";
|
||||||
import { Button, Chip, Grid } from "@mui/material";
|
import { Button, Chip, Grid } from "@mui/material";
|
||||||
import { GridColDef } from "@mui/x-data-grid";
|
import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
import { DeleteDialog } from "@/app-components/dialogs";
|
import { DeleteDialog } from "@/app-components/dialogs";
|
||||||
import { FilterTextfield } from "@/app-components/inputs/FilterTextfield";
|
import { FilterTextfield } from "@/app-components/inputs/FilterTextfield";
|
||||||
@ -20,6 +22,7 @@ import {
|
|||||||
import { renderHeader } from "@/app-components/tables/columns/renderHeader";
|
import { renderHeader } from "@/app-components/tables/columns/renderHeader";
|
||||||
import { DataGrid } from "@/app-components/tables/DataGrid";
|
import { DataGrid } from "@/app-components/tables/DataGrid";
|
||||||
import { useDelete } from "@/hooks/crud/useDelete";
|
import { useDelete } from "@/hooks/crud/useDelete";
|
||||||
|
import { useDeleteMany } from "@/hooks/crud/useDeleteMany";
|
||||||
import { useFind } from "@/hooks/crud/useFind";
|
import { useFind } from "@/hooks/crud/useFind";
|
||||||
import { getDisplayDialogs, useDialog } from "@/hooks/useDialog";
|
import { getDisplayDialogs, useDialog } from "@/hooks/useDialog";
|
||||||
import { useHasPermission } from "@/hooks/useHasPermission";
|
import { useHasPermission } from "@/hooks/useHasPermission";
|
||||||
@ -47,6 +50,20 @@ const NlpEntity = () => {
|
|||||||
toast.success(t("message.item_delete_success"));
|
toast.success(t("message.item_delete_success"));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const { mutateAsync: deleteNlpEntities } = useDeleteMany(
|
||||||
|
EntityType.NLP_ENTITY,
|
||||||
|
{
|
||||||
|
onError: (error) => {
|
||||||
|
toast.error(error);
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
deleteEntityDialogCtl.closeDialog();
|
||||||
|
setSelectedNlpEntities([]);
|
||||||
|
toast.success(t("message.item_delete_success"));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const [selectedNlpEntities, setSelectedNlpEntities] = useState<string[]>([]);
|
||||||
const addDialogCtl = useDialog<INlpEntity>(false);
|
const addDialogCtl = useDialog<INlpEntity>(false);
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
@ -154,6 +171,9 @@ const NlpEntity = () => {
|
|||||||
},
|
},
|
||||||
actionEntityColumns,
|
actionEntityColumns,
|
||||||
];
|
];
|
||||||
|
const handleSelectionChange = (selection: GridRowSelectionModel) => {
|
||||||
|
setSelectedNlpEntities(selection as string[]);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
@ -162,18 +182,29 @@ const NlpEntity = () => {
|
|||||||
<DeleteDialog
|
<DeleteDialog
|
||||||
{...deleteEntityDialogCtl}
|
{...deleteEntityDialogCtl}
|
||||||
callback={() => {
|
callback={() => {
|
||||||
if (deleteEntityDialogCtl.data)
|
if (selectedNlpEntities.length > 0) {
|
||||||
|
deleteNlpEntities(selectedNlpEntities);
|
||||||
|
setSelectedNlpEntities([]);
|
||||||
|
deleteEntityDialogCtl.closeDialog();
|
||||||
|
} else if (deleteEntityDialogCtl.data) {
|
||||||
deleteNlpEntity(deleteEntityDialogCtl.data);
|
deleteNlpEntity(deleteEntityDialogCtl.data);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Grid container alignItems="center">
|
<Grid
|
||||||
<Grid item xs={6}>
|
justifyContent="flex-end"
|
||||||
|
gap={1}
|
||||||
|
container
|
||||||
|
alignItems="center"
|
||||||
|
flexShrink={0}
|
||||||
|
>
|
||||||
|
<Grid item>
|
||||||
<FilterTextfield onChange={onSearch} />
|
<FilterTextfield onChange={onSearch} />
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
{hasPermission(EntityType.NLP_ENTITY, PermissionAction.CREATE) ? (
|
{hasPermission(EntityType.NLP_ENTITY, PermissionAction.CREATE) ? (
|
||||||
<Grid item xs={6}>
|
<Grid item>
|
||||||
<Button
|
<Button
|
||||||
startIcon={<AddIcon />}
|
startIcon={<AddIcon />}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@ -184,10 +215,27 @@ const NlpEntity = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
) : null}
|
) : null}
|
||||||
|
{selectedNlpEntities.length > 0 && (
|
||||||
|
<Grid item>
|
||||||
|
<Button
|
||||||
|
startIcon={<DeleteIcon />}
|
||||||
|
variant="contained"
|
||||||
|
color="error"
|
||||||
|
onClick={() => deleteEntityDialogCtl.openDialog(undefined)}
|
||||||
|
>
|
||||||
|
{t("button.delete")}
|
||||||
|
</Button>
|
||||||
|
</Grid>
|
||||||
|
)}
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Grid mt={3}>
|
<Grid mt={3}>
|
||||||
<DataGrid columns={nlpEntityColumns} {...nlpEntityGrid} />
|
<DataGrid
|
||||||
|
columns={nlpEntityColumns}
|
||||||
|
{...nlpEntityGrid}
|
||||||
|
checkboxSelection
|
||||||
|
onRowSelectionModelChange={handleSelectionChange}
|
||||||
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user