mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: update services
This commit is contained in:
parent
8cff20c861
commit
d067a0ffa4
@ -21,7 +21,9 @@ import { PluginType } from '@/plugins/types';
|
|||||||
import { SettingService } from '@/setting/services/setting.service';
|
import { SettingService } from '@/setting/services/setting.service';
|
||||||
import { FALLBACK_DEFAULT_NLU_PENALTY_FACTOR } from '@/utils/constants/nlp';
|
import { FALLBACK_DEFAULT_NLU_PENALTY_FACTOR } from '@/utils/constants/nlp';
|
||||||
import { BaseService } from '@/utils/generics/base-service';
|
import { BaseService } from '@/utils/generics/base-service';
|
||||||
|
import { getCriteriaIds } from '@/utils/helpers/criteria';
|
||||||
import { getRandomElement } from '@/utils/helpers/safeRandom';
|
import { getRandomElement } from '@/utils/helpers/safeRandom';
|
||||||
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { BlockDto } from '../dto/block.dto';
|
import { BlockDto } from '../dto/block.dto';
|
||||||
import { EnvelopeFactory } from '../helpers/envelope-factory';
|
import { EnvelopeFactory } from '../helpers/envelope-factory';
|
||||||
@ -778,28 +780,26 @@ export class BlockService extends BaseService<
|
|||||||
/**
|
/**
|
||||||
* Updates the `trigger_labels` and `assign_labels` fields of a block when a label is deleted.
|
* Updates the `trigger_labels` and `assign_labels` fields of a block when a label is deleted.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* This method removes the deleted label from the `trigger_labels` and `assign_labels` fields of all blocks that have the label.
|
* This method removes the deleted label from the `trigger_labels` and `assign_labels` fields of all blocks that have the label.
|
||||||
*
|
*
|
||||||
* @param label The label that is being deleted.
|
* @param label The label that is being deleted.
|
||||||
*/
|
*/
|
||||||
@OnEvent('hook:label:delete')
|
@OnEvent('hook:label:preDelete')
|
||||||
async handleLabelDelete(labels: Label[]) {
|
async handleLabelDelete(_query: unknown, criteria: TFilterQuery<Label>) {
|
||||||
const blocks = await this.find({
|
const ids = getCriteriaIds(criteria);
|
||||||
$or: [
|
await this.getRepository().model.updateMany(
|
||||||
{ trigger_labels: { $in: labels.map((l) => l.id) } },
|
{
|
||||||
{ assign_labels: { $in: labels.map((l) => l.id) } },
|
$or: [
|
||||||
],
|
{ trigger_labels: { $in: ids } },
|
||||||
});
|
{ assign_labels: { $in: ids } },
|
||||||
|
],
|
||||||
for (const block of blocks) {
|
},
|
||||||
const trigger_labels = block.trigger_labels.filter(
|
{
|
||||||
(labelId) => !labels.find((l) => l.id === labelId),
|
$pull: {
|
||||||
);
|
trigger_labels: { $in: ids },
|
||||||
const assign_labels = block.assign_labels.filter(
|
assign_labels: { $in: ids },
|
||||||
(labelId) => !labels.find((l) => l.id === labelId),
|
},
|
||||||
);
|
},
|
||||||
await this.updateOne(block.id, { trigger_labels, assign_labels });
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ import {
|
|||||||
} from '@/attachment/types';
|
} from '@/attachment/types';
|
||||||
import { config } from '@/config';
|
import { config } from '@/config';
|
||||||
import { BaseService } from '@/utils/generics/base-service';
|
import { BaseService } from '@/utils/generics/base-service';
|
||||||
|
import { getCriteriaIds } from '@/utils/helpers/criteria';
|
||||||
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
import {
|
import {
|
||||||
SocketGet,
|
SocketGet,
|
||||||
SocketPost,
|
SocketPost,
|
||||||
@ -260,22 +262,22 @@ export class SubscriberService extends BaseService<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the `labels` field of a subscriber when a label is deleted.
|
* Before deleting a `Label`, this method updates the `labels` field of a subscriber.
|
||||||
*
|
*
|
||||||
* This method removes the deleted label from the `labels` field of all subscribers that have the label.
|
* @param _query - The Mongoose query object used for deletion.
|
||||||
|
* @param criteria - The filter criteria for finding the labels to be deleted.
|
||||||
*
|
*
|
||||||
* @param label The label that is being deleted.
|
* @returns {Promise<void>} A promise that resolves once the event is emitted.
|
||||||
*/
|
*/
|
||||||
@OnEvent('hook:label:delete')
|
@OnEvent('hook:label:preDelete')
|
||||||
async handleLabelDelete(labels: Label[]) {
|
async handleLabelDelete(
|
||||||
const subscribers = await this.find({
|
_query: unknown,
|
||||||
labels: { $in: labels.map((l) => l.id) },
|
criteria: TFilterQuery<Label>,
|
||||||
});
|
): Promise<void> {
|
||||||
for (const subscriber of subscribers) {
|
const ids = getCriteriaIds(criteria);
|
||||||
const updatedLabels = subscriber.labels.filter(
|
await this.getRepository().model.updateMany(
|
||||||
(label) => !labels.find((l) => l.id === label),
|
{ labels: { $in: ids } },
|
||||||
);
|
{ $pull: { labels: { $in: ids } } },
|
||||||
await this.updateOne(subscriber.id, { labels: updatedLabels });
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,13 @@ import { OnEvent } from '@nestjs/event-emitter';
|
|||||||
import { HelperService } from '@/helper/helper.service';
|
import { HelperService } from '@/helper/helper.service';
|
||||||
import { HelperType, NLU } from '@/helper/types';
|
import { HelperType, NLU } from '@/helper/types';
|
||||||
import { LoggerService } from '@/logger/logger.service';
|
import { LoggerService } from '@/logger/logger.service';
|
||||||
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { NlpEntity, NlpEntityDocument } from '../schemas/nlp-entity.schema';
|
import { NlpEntity, NlpEntityDocument } from '../schemas/nlp-entity.schema';
|
||||||
import { NlpValue, NlpValueDocument } from '../schemas/nlp-value.schema';
|
import { NlpValue, NlpValueDocument } from '../schemas/nlp-value.schema';
|
||||||
|
|
||||||
import { NlpEntityService } from './nlp-entity.service';
|
import { NlpEntityService } from './nlp-entity.service';
|
||||||
|
import { NlpSampleEntityService } from './nlp-sample-entity.service';
|
||||||
import { NlpSampleService } from './nlp-sample.service';
|
import { NlpSampleService } from './nlp-sample.service';
|
||||||
import { NlpValueService } from './nlp-value.service';
|
import { NlpValueService } from './nlp-value.service';
|
||||||
|
|
||||||
@ -28,6 +30,7 @@ export class NlpService {
|
|||||||
protected readonly nlpEntityService: NlpEntityService,
|
protected readonly nlpEntityService: NlpEntityService,
|
||||||
protected readonly nlpValueService: NlpValueService,
|
protected readonly nlpValueService: NlpValueService,
|
||||||
protected readonly helperService: HelperService,
|
protected readonly helperService: HelperService,
|
||||||
|
protected readonly nlpSampleEntityService: NlpSampleEntityService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,20 +109,34 @@ export class NlpService {
|
|||||||
*
|
*
|
||||||
* @param entity - The NLP entity to be deleted.
|
* @param entity - The NLP entity to be deleted.
|
||||||
*/
|
*/
|
||||||
@OnEvent('hook:nlpEntity:delete')
|
@OnEvent('hook:nlpEntity:preDelete')
|
||||||
async handleEntityDelete(entity: NlpEntity) {
|
async handleEntityDelete(_query: unknown, criteria: TFilterQuery<NlpValue>) {
|
||||||
// Synchonize new entity with NLP provider
|
if (criteria._id) {
|
||||||
try {
|
await this.nlpValueService.deleteMany({ entity: criteria._id });
|
||||||
if (entity.foreign_id) {
|
await this.nlpSampleEntityService.deleteMany({ entity: criteria._id });
|
||||||
const helper = await this.helperService.getDefaultNluHelper();
|
|
||||||
await helper.deleteEntity(entity.foreign_id);
|
const entities = await this.nlpEntityService.find({
|
||||||
this.logger.debug('Deleted entity successfully synced!', entity);
|
...(typeof criteria === 'string' ? { _id: criteria } : criteria),
|
||||||
} else {
|
builtin: false,
|
||||||
this.logger.error(`Entity ${entity} is missing foreign_id`);
|
});
|
||||||
throw new NotFoundException(`Entity ${entity} is missing foreign_id`);
|
const helper = await this.helperService.getDefaultHelper(HelperType.NLU);
|
||||||
|
|
||||||
|
for (const entity of entities) {
|
||||||
|
// Synchonize new entity with NLP provider
|
||||||
|
try {
|
||||||
|
if (entity.foreign_id) {
|
||||||
|
await helper.deleteEntity(entity.foreign_id);
|
||||||
|
this.logger.debug('Deleted entity successfully synced!', entity);
|
||||||
|
} else {
|
||||||
|
this.logger.error(`Entity ${entity} is missing foreign_id`);
|
||||||
|
throw new NotFoundException(
|
||||||
|
`Entity ${entity} is missing foreign_id`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error('Unable to sync deleted entity', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
this.logger.error('Unable to sync deleted entity', err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,20 +185,37 @@ export class NlpService {
|
|||||||
*
|
*
|
||||||
* @param value - The NLP value to be deleted.
|
* @param value - The NLP value to be deleted.
|
||||||
*/
|
*/
|
||||||
@OnEvent('hook:nlpValue:delete')
|
@OnEvent('hook:nlpValue:preDelete')
|
||||||
async handleValueDelete(value: NlpValue) {
|
async handleValueDelete(_query: unknown, criteria: TFilterQuery<NlpValue>) {
|
||||||
// Synchonize new value with NLP provider
|
if (criteria._id) {
|
||||||
try {
|
await this.nlpSampleEntityService.deleteMany({
|
||||||
const helper = await this.helperService.getDefaultNluHelper();
|
value: criteria._id,
|
||||||
const populatedValue = await this.nlpValueService.findOneAndPopulate(
|
});
|
||||||
value.id,
|
|
||||||
);
|
const values = await this.nlpValueService.find({
|
||||||
if (populatedValue) {
|
...(typeof criteria === 'string' ? { _id: criteria } : criteria),
|
||||||
await helper.deleteValue(populatedValue);
|
builtin: false,
|
||||||
this.logger.debug('Deleted value successfully synced!', value);
|
});
|
||||||
|
const helper = await this.helperService.getDefaultHelper(HelperType.NLU);
|
||||||
|
|
||||||
|
for (const value of values) {
|
||||||
|
// Synchonize new value with NLP provider
|
||||||
|
try {
|
||||||
|
const populatedValue = await this.nlpValueService.findOneAndPopulate(
|
||||||
|
value.id,
|
||||||
|
);
|
||||||
|
if (populatedValue) {
|
||||||
|
await helper.deleteValue(populatedValue);
|
||||||
|
this.logger.debug('Deleted value successfully synced!', value);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error('Unable to sync deleted value', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} else if (criteria.entity) {
|
||||||
this.logger.error('Unable to sync deleted value', err);
|
// Do nothing : cascading deletes coming from Nlp Sample Entity
|
||||||
|
} else {
|
||||||
|
throw new Error('Attempted to delete a NLP value using unknown criteria');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user