mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #1009 from Hexastack/829-issue---trait-built-in-entity-must-not-be-deleted
fix: block deletion for builtin Nlp Entities
This commit is contained in:
@@ -222,6 +222,7 @@ export class NlpEntityController extends BaseController<
|
||||
if (!ids?.length) {
|
||||
throw new BadRequestException('No IDs provided for deletion.');
|
||||
}
|
||||
|
||||
const deleteResult = await this.nlpEntityService.deleteMany({
|
||||
_id: { $in: ids },
|
||||
});
|
||||
|
||||
@@ -163,7 +163,7 @@ describe('NlpValueController', () => {
|
||||
entity: intentNlpEntity!.id,
|
||||
value: 'updated',
|
||||
expressions: [],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
};
|
||||
const result = await nlpValueController.updateOne(
|
||||
@@ -191,7 +191,6 @@ describe('NlpValueController', () => {
|
||||
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);
|
||||
|
||||
@@ -232,7 +232,7 @@ describe('NlpEntityService', () => {
|
||||
{
|
||||
value: 'jhon',
|
||||
expressions: ['john', 'joohn', 'jhonny'],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
],
|
||||
|
||||
@@ -138,6 +138,7 @@ describe('PermissionRepository', () => {
|
||||
|
||||
expect(permissionModel.deleteOne).toHaveBeenCalledWith({
|
||||
_id: permissionToDelete.id,
|
||||
builtin: { $ne: true },
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
|
||||
@@ -306,6 +306,7 @@ describe('BaseRepository', () => {
|
||||
|
||||
expect(dummyModel.deleteOne).toHaveBeenCalledWith({
|
||||
_id: createdId,
|
||||
builtin: { $ne: true },
|
||||
});
|
||||
expect(result).toEqualPayload({ acknowledged: true, deletedCount: 1 });
|
||||
});
|
||||
@@ -318,12 +319,13 @@ describe('BaseRepository', () => {
|
||||
|
||||
expect(dummyModel.deleteOne).toHaveBeenCalledWith({
|
||||
dummy: 'dummy test 2',
|
||||
builtin: { $ne: true },
|
||||
});
|
||||
expect(result).toEqualPayload({ acknowledged: true, deletedCount: 1 });
|
||||
});
|
||||
|
||||
it('should call lifecycle hooks appropriately when deleting by id', async () => {
|
||||
const criteria = createdId;
|
||||
jest.spyOn(dummyModel, 'deleteOne');
|
||||
|
||||
// Spies for lifecycle hooks
|
||||
const spyBeforeDelete = jest
|
||||
@@ -333,7 +335,12 @@ describe('BaseRepository', () => {
|
||||
.spyOn(dummyRepository, 'postDelete')
|
||||
.mockResolvedValue();
|
||||
|
||||
await dummyRepository.deleteOne(criteria);
|
||||
await dummyRepository.deleteOne(createdId);
|
||||
|
||||
expect(dummyModel.deleteOne).toHaveBeenCalledWith({
|
||||
_id: createdId,
|
||||
builtin: { $ne: true },
|
||||
});
|
||||
|
||||
// Verifying that lifecycle hooks are called with correct parameters
|
||||
expect(spyBeforeDelete).toHaveBeenCalledTimes(1);
|
||||
@@ -341,6 +348,7 @@ describe('BaseRepository', () => {
|
||||
expect.objectContaining({ $useProjection: true }),
|
||||
{
|
||||
_id: new Types.ObjectId(createdId),
|
||||
builtin: { $ne: true },
|
||||
},
|
||||
);
|
||||
expect(spyAfterDelete).toHaveBeenCalledWith(
|
||||
|
||||
@@ -555,13 +555,15 @@ export abstract class BaseRepository<
|
||||
}
|
||||
|
||||
async deleteOne(criteria: string | TFilterQuery<T>): Promise<DeleteResult> {
|
||||
const filter = typeof criteria === 'string' ? { _id: criteria } : criteria;
|
||||
|
||||
return await this.model
|
||||
.deleteOne(typeof criteria === 'string' ? { _id: criteria } : criteria)
|
||||
.deleteOne({ ...filter, builtin: { $ne: true } })
|
||||
.exec();
|
||||
}
|
||||
|
||||
async deleteMany(criteria: TFilterQuery<T>): Promise<DeleteResult> {
|
||||
return await this.model.deleteMany(criteria);
|
||||
return await this.model.deleteMany({ ...criteria, builtin: { $ne: true } });
|
||||
}
|
||||
|
||||
async preCreateValidate(
|
||||
|
||||
10
api/src/utils/test/fixtures/nlpvalue.ts
vendored
10
api/src/utils/test/fixtures/nlpvalue.ts
vendored
@@ -18,35 +18,35 @@ export const nlpValueFixtures: NlpValueCreateDto[] = [
|
||||
entity: '0',
|
||||
value: 'positive',
|
||||
expressions: [],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
{
|
||||
entity: '0',
|
||||
value: 'negative',
|
||||
expressions: [],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
{
|
||||
entity: '1',
|
||||
value: 'jhon',
|
||||
expressions: ['john', 'joohn', 'jhonny'],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
{
|
||||
entity: '0',
|
||||
value: 'greeting',
|
||||
expressions: ['heello', 'Hello', 'hi', 'heyy'],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
{
|
||||
entity: '0',
|
||||
value: 'goodbye',
|
||||
expressions: ['bye', 'bye bye'],
|
||||
builtin: true,
|
||||
builtin: false,
|
||||
doc: '',
|
||||
},
|
||||
{
|
||||
|
||||
@@ -267,6 +267,7 @@ const NlpEntity = () => {
|
||||
<DataGrid
|
||||
columns={nlpEntityColumns}
|
||||
{...nlpEntityGrid}
|
||||
isRowSelectable={({ row }) => !row.builtin}
|
||||
checkboxSelection
|
||||
onRowSelectionModelChange={handleSelectionChange}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user