hexabot/api/src/nlp/repositories/nlp-sample.repository.ts

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-09-10 09:50:11 +00:00
/*
* Copyright © 2024 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 { Injectable } from '@nestjs/common';
2024-10-05 11:15:50 +00:00
import { EventEmitter2 } from '@nestjs/event-emitter';
2024-09-10 09:50:11 +00:00
import { InjectModel } from '@nestjs/mongoose';
2024-10-29 13:36:46 +00:00
import { Document, Model, Query } from 'mongoose';
2024-09-10 09:50:11 +00:00
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
2024-10-29 13:36:46 +00:00
import { TFilterQuery } from '@/utils/types/filter.types';
2024-09-10 09:50:11 +00:00
2024-09-21 11:15:36 +00:00
import {
NLP_SAMPLE_POPULATE,
NlpSample,
NlpSampleFull,
NlpSamplePopulate,
} from '../schemas/nlp-sample.schema';
2024-09-10 09:50:11 +00:00
2024-10-16 17:54:55 +00:00
import { NlpSampleEntityRepository } from './nlp-sample-entity.repository';
2024-09-10 09:50:11 +00:00
@Injectable()
2024-09-21 11:15:36 +00:00
export class NlpSampleRepository extends BaseRepository<
NlpSample,
NlpSamplePopulate,
NlpSampleFull
> {
2024-09-10 09:50:11 +00:00
constructor(
2024-10-05 11:15:50 +00:00
readonly eventEmitter: EventEmitter2,
2024-09-10 09:50:11 +00:00
@InjectModel(NlpSample.name) readonly model: Model<NlpSample>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
2024-10-05 11:15:50 +00:00
super(eventEmitter, model, NlpSample, NLP_SAMPLE_POPULATE, NlpSampleFull);
2024-09-10 09:50:11 +00:00
}
/**
* Deletes NLP sample entities associated with the provided criteria before deleting the sample itself.
*
* @param query - The query object used for deletion.
* @param criteria - Criteria to identify the sample(s) to delete.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<NlpSample, any, any>,
unknown,
NlpSample,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<NlpSample>,
) {
if (criteria._id) {
await this.nlpSampleEntityRepository.deleteMany({
sample: criteria._id,
});
} else {
throw new Error(
'Attempted to delete a NLP sample using unknown criteria',
);
}
}
}