refactor: populate queries

This commit is contained in:
Mohamed Marrouchi
2024-09-21 12:15:36 +01:00
parent bb52f2a1d5
commit 7bc5270551
67 changed files with 661 additions and 1016 deletions

View File

@@ -8,7 +8,6 @@
*/
import { Injectable } from '@nestjs/common';
import { TFilterQuery } from 'mongoose';
import { Attachment } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
@@ -24,7 +23,7 @@ import { SettingService } from '@/setting/services/setting.service';
import { BaseService } from '@/utils/generics/base-service';
import { BlockRepository } from '../repositories/block.repository';
import { Block, BlockFull } from '../schemas/block.schema';
import { Block, BlockFull, BlockPopulate } from '../schemas/block.schema';
import { WithUrl } from '../schemas/types/attachment';
import { Context } from '../schemas/types/context';
import {
@@ -36,7 +35,7 @@ import { NlpPattern, Pattern, PayloadPattern } from '../schemas/types/pattern';
import { Payload, StdQuickReply } from '../schemas/types/quick-reply';
@Injectable()
export class BlockService extends BaseService<Block> {
export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
constructor(
readonly repository: BlockRepository,
private readonly contentService: ContentService,
@@ -49,28 +48,6 @@ export class BlockService extends BaseService<Block> {
super(repository);
}
/**
* Finds and populates blocks based on the specified filters.
*
* @param filters - Query filters used to specify search criteria for finding blocks.
*
* @returns A promise that resolves to the populated blocks matching the filters.
*/
async findAndPopulate(filters: TFilterQuery<Block>) {
return await this.repository.findAndPopulate(filters);
}
/**
* Finds and populates a block by ID.
*
* @param id - The block ID.
*
* @returns A promise that resolves to the populated block.
*/
async findOneAndPopulate(id: string) {
return await this.repository.findOneAndPopulate(id);
}
/**
* Find a block whose patterns matches the received event
*

View File

@@ -17,12 +17,20 @@ import { BaseService } from '@/utils/generics/base-service';
import { VIEW_MORE_PAYLOAD } from '../helpers/constants';
import { ConversationRepository } from '../repositories/conversation.repository';
import { Block, BlockFull } from '../schemas/block.schema';
import { Conversation, ConversationFull } from '../schemas/conversation.schema';
import {
Conversation,
ConversationFull,
ConversationPopulate,
} from '../schemas/conversation.schema';
import { OutgoingMessageFormat } from '../schemas/types/message';
import { Payload } from '../schemas/types/quick-reply';
@Injectable()
export class ConversationService extends BaseService<Conversation> {
export class ConversationService extends BaseService<
Conversation,
ConversationPopulate,
ConversationFull
> {
constructor(
readonly repository: ConversationRepository,
private readonly logger: LoggerService,

View File

@@ -14,10 +14,10 @@ import { BaseService } from '@/utils/generics/base-service';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { LabelRepository } from '../repositories/label.repository';
import { Label } from '../schemas/label.schema';
import { Label, LabelFull, LabelPopulate } from '../schemas/label.schema';
@Injectable()
export class LabelService extends BaseService<Label> {
export class LabelService extends BaseService<Label, LabelPopulate, LabelFull> {
constructor(readonly repository: LabelRepository) {
super(repository);
}

View File

@@ -29,11 +29,16 @@ import { SocketResponse } from '@/websocket/utils/socket-response';
import { WebsocketGateway } from '@/websocket/websocket.gateway';
import { MessageRepository } from '../repositories/message.repository';
import { MessageFull, MessagePopulate } from '../schemas/message.schema';
import { Subscriber } from '../schemas/subscriber.schema';
import { AnyMessage } from '../schemas/types/message';
@Injectable()
export class MessageService extends BaseService<AnyMessage> {
export class MessageService extends BaseService<
AnyMessage,
MessagePopulate,
MessageFull
> {
private readonly logger: LoggerService;
private readonly gateway: WebsocketGateway;

View File

@@ -35,10 +35,18 @@ import { WebsocketGateway } from '@/websocket/websocket.gateway';
import { SubscriberUpdateDto } from '../dto/subscriber.dto';
import { SubscriberRepository } from '../repositories/subscriber.repository';
import { Subscriber } from '../schemas/subscriber.schema';
import {
Subscriber,
SubscriberFull,
SubscriberPopulate,
} from '../schemas/subscriber.schema';
@Injectable()
export class SubscriberService extends BaseService<Subscriber> {
export class SubscriberService extends BaseService<
Subscriber,
SubscriberPopulate,
SubscriberFull
> {
private readonly gateway: WebsocketGateway;
constructor(