fix: handle preDelete criteria

This commit is contained in:
hexastack 2024-10-07 12:26:30 +01:00
parent 803649c4d1
commit 92dbd8c996

View File

@ -37,7 +37,7 @@ export class CategoryRepository extends BaseRepository<Category> {
* @param criteria - The filter criteria for finding blocks to delete. * @param criteria - The filter criteria for finding blocks to delete.
*/ */
async preDelete( async preDelete(
_query: Query< query: Query<
DeleteResult, DeleteResult,
Document<Category, any, any>, Document<Category, any, any>,
unknown, unknown,
@ -46,11 +46,18 @@ export class CategoryRepository extends BaseRepository<Category> {
>, >,
criteria: TFilterQuery<Category>, criteria: TFilterQuery<Category>,
) { ) {
const associatedBlocks = await this.blockService.findOne({ criteria = query.getQuery();
category: criteria._id, const ids = Array.isArray(criteria._id) ? criteria._id : [criteria._id];
});
if (associatedBlocks) { for (const id of ids) {
throw new ForbiddenException(`Category have blocks associated to it`); const associatedBlocks = await this.blockService.findOne({
category: id,
});
if (associatedBlocks) {
throw new ForbiddenException(
`Category ${id} has blocks associated with it`,
);
}
} }
} }
} }