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

112 lines
3.6 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';
import { EventEmitter2 } from '@nestjs/event-emitter';
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
import { NlpValueDTOMapActions } from '../dto/nlp-value.dto';
2024-09-10 09:50:11 +00:00
import {
2024-09-21 11:15:36 +00:00
NLP_VALUE_POPULATE,
2024-09-10 09:50:11 +00:00
NlpValue,
2024-10-09 07:25:08 +00:00
NlpValueDocument,
2024-09-10 09:50:11 +00:00
NlpValueFull,
2024-09-21 11:15:36 +00:00
NlpValuePopulate,
2024-09-10 09:50:11 +00:00
} from '../schemas/nlp-value.schema';
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 NlpValueRepository extends BaseRepository<
NlpValue,
NlpValuePopulate,
NlpValueFull,
NlpValueDTOMapActions
2024-09-21 11:15:36 +00:00
> {
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(NlpValue.name) readonly model: Model<NlpValue>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
2024-10-05 11:15:50 +00:00
super(eventEmitter, model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
2024-09-10 09:50:11 +00:00
}
/**
* Emits an event after a new NLP value is created, bypassing built-in values.
*
* @param created - The newly created NLP value document.
*/
2024-10-09 07:25:08 +00:00
async postCreate(created: NlpValueDocument): Promise<void> {
2024-09-10 09:50:11 +00:00
if (!created.builtin) {
// Bypass builtin entities (probably fixtures)
2024-10-07 14:39:41 +00:00
this.eventEmitter.emit('hook:nlpValue:create', created);
2024-09-10 09:50:11 +00:00
}
}
/**
* Emits an event after an NLP value is updated, bypassing built-in values.
*
* @param query - The query that was used to update the NLP value.
* @param updated - The updated NLP value document.
*/
async postUpdate(
_query: Query<
Document<NlpValue, any, any>,
Document<NlpValue, any, any>,
unknown,
NlpValue,
'findOneAndUpdate'
>,
updated: NlpValue,
): Promise<void> {
if (!updated?.builtin) {
// Bypass builtin entities (probably fixtures)
2024-10-07 14:39:41 +00:00
this.eventEmitter.emit('hook:nlpValue:update', updated);
2024-09-10 09:50:11 +00:00
}
}
/**
* Handles deletion of NLP values and associated entities. If the criteria includes an ID,
* emits an event for each deleted entity.
*
* @param _query - The query used to delete the NLP value(s).
* @param criteria - The filter criteria used to identify the NLP value(s) to delete.
*/
async preDelete(
_query: Query<
DeleteResult,
Document<NlpValue, any, any>,
unknown,
NlpValue,
'deleteOne' | 'deleteMany'
>,
criteria: TFilterQuery<NlpValue>,
): Promise<void> {
if (criteria._id) {
await this.nlpSampleEntityRepository.deleteMany({ value: criteria._id });
const entities = await this.find(
typeof criteria === 'string' ? { _id: criteria } : criteria,
);
entities
.filter((e) => !e.builtin)
.map((e) => {
2024-10-07 14:39:41 +00:00
this.eventEmitter.emit('hook:nlpValue:delete', e);
2024-09-10 09:50:11 +00:00
});
} else if (criteria.entity) {
// Do nothing : cascading deletes coming from Nlp Sample Entity
} else {
throw new Error('Attempted to delete a NLP value using unknown criteria');
}
}
}