/* * Copyright © 2025 Hexastack. All rights reserved. * * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 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 { ForbiddenException, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Document, Model, Query } from 'mongoose'; import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository'; import { TFilterQuery } from '@/utils/types/filter.types'; import { CategoryDto } from '../dto/category.dto'; import { Category } from '../schemas/category.schema'; import { BlockService } from '../services/block.service'; @Injectable() export class CategoryRepository extends BaseRepository< Category, never, never, CategoryDto > { constructor( @InjectModel(Category.name) readonly model: Model, private readonly blockService: BlockService, ) { super(model, Category); } /** * Pre-processing logic before deleting a category. * It avoids delete a category that contains blocks. * * @param query - The delete query. * @param criteria - The filter criteria for finding blocks to delete. */ async preDelete( _query: Query< DeleteResult, Document, unknown, Category, 'deleteOne' | 'deleteMany' >, criteria: TFilterQuery, ) { if (criteria._id) { const block = await this.blockService.findOneAndPopulate({ category: criteria._id, }); if (block) { throw new ForbiddenException( `Category ${block.category?.label} has at least one associated block`, ); } } else { throw new Error('Attempted to delete category using unknown criteria'); } } }