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:
Med Marrouchi
2025-05-26 17:58:21 +01:00
committed by GitHub
8 changed files with 24 additions and 12 deletions

View File

@@ -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 },
});

View File

@@ -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);

View File

@@ -232,7 +232,7 @@ describe('NlpEntityService', () => {
{
value: 'jhon',
expressions: ['john', 'joohn', 'jhonny'],
builtin: true,
builtin: false,
doc: '',
},
],

View File

@@ -138,6 +138,7 @@ describe('PermissionRepository', () => {
expect(permissionModel.deleteOne).toHaveBeenCalledWith({
_id: permissionToDelete.id,
builtin: { $ne: true },
});
expect(result).toEqual({

View File

@@ -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(

View File

@@ -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(

View File

@@ -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: '',
},
{

View File

@@ -267,6 +267,7 @@ const NlpEntity = () => {
<DataGrid
columns={nlpEntityColumns}
{...nlpEntityGrid}
isRowSelectable={({ row }) => !row.builtin}
checkboxSelection
onRowSelectionModelChange={handleSelectionChange}
/>