refactor(api): simplify services logic

This commit is contained in:
yassinedorbozgithub 2024-10-12 12:44:32 +01:00
parent a9070580bd
commit f53589b06d
5 changed files with 2 additions and 115 deletions

View File

@ -7,7 +7,6 @@
*/
import { Injectable, Logger } from '@nestjs/common';
import { FilterQuery } from 'mongoose';
import EventWrapper from '@/channel/lib/EventWrapper';
import { LoggerService } from '@/logger/logger.service';
@ -41,21 +40,6 @@ export class ConversationService extends BaseService<
super(repository);
}
/**
* Finds and retrieves a single conversation entity based on the provided criteria,
* while populating the necessary related data.
*
* @param criteria - The search criteria to find a conversation, which can be a string
* representing a unique identifier or a filter query object that specifies the conditions
* for the search.
*
* @returns A Promise that resolves with the found conversation, populated with any related data,
* or `null` if no conversation matches the criteria.
*/
async findOneAndPopulate(criteria: string | FilterQuery<Conversation>) {
return await this.repository.findOneAndPopulate(criteria);
}
/**
* Marks the conversation as inactive.
*

View File

@ -7,10 +7,8 @@
*/
import { Injectable } from '@nestjs/common';
import { TFilterQuery } from 'mongoose';
import { BaseService } from '@/utils/generics/base-service';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { LabelRepository } from '../repositories/label.repository';
import { Label, LabelFull, LabelPopulate } from '../schemas/label.schema';
@ -20,39 +18,4 @@ export class LabelService extends BaseService<Label, LabelPopulate, LabelFull> {
constructor(readonly repository: LabelRepository) {
super(repository);
}
/**
* Finds all labels and populates related data.
*
* @returns A promise that resolves with an array of labels with populated related data.
*/
async findAllAndPopulate() {
return await this.repository.findAllAndPopulate();
}
/**
* Finds a page of labels based on filters and page query, and populates related data.
*
* @param filters The filters to apply when querying the labels.
* @param pageQuery The pagination and sorting options.
*
* @returns A promise that resolves with a paginated list of labels with populated related data.
*/
async findPageAndPopulate(
filters: TFilterQuery<Label>,
pageQuery: PageQueryDto<Label>,
) {
return await this.repository.findPageAndPopulate(filters, pageQuery);
}
/**
* Finds a label by ID and populates related data.
*
* @param id The ID of the label to find.
*
* @returns A promise that resolves with the found label with populated related data or null if not found.
*/
async findOneAndPopulate(id: string) {
return await this.repository.findOneAndPopulate(id);
}
}

View File

@ -11,11 +11,9 @@ import {
InternalServerErrorException,
Optional,
} from '@nestjs/common';
import { TFilterQuery } from 'mongoose';
import { LoggerService } from '@/logger/logger.service';
import { BaseService } from '@/utils/generics/base-service';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import {
SocketGet,
SocketPost,
@ -75,34 +73,6 @@ export class MessageService extends BaseService<
}
}
/**
* Retrieves a paginated list of messages based on the provided filters
* and page query, and populates the results with additional data.
*
* @param filters - The query filters to apply for message retrieval.
* @param pageQuery - The page query containing pagination information.
*
* @returns A paginated list of populated messages.
*/
async findPageAndPopulate(
filters: TFilterQuery<AnyMessage>,
pageQuery: PageQueryDto<AnyMessage>,
) {
return await this.messageRepository.findPageAndPopulate(filters, pageQuery);
}
/**
* Retrieves a single message by its identifier and populates it with
* additional data.
*
* @param id - The identifier of the message to retrieve.
*
* @returns A populated message object.
*/
async findOneAndPopulate(id: string) {
return await this.messageRepository.findOneAndPopulate(id);
}
/**
* Retrieves the message history for a given subscriber up until a specific
* date, with an optional limit on the number of messages to return.

View File

@ -14,13 +14,11 @@ import {
StreamableFile,
} from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { TFilterQuery } from 'mongoose';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { config } from '@/config';
import { LoggerService } from '@/logger/logger.service';
import { BaseService } from '@/utils/generics/base-service';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import {
SocketGet,
SocketPost,
@ -82,34 +80,6 @@ export class SubscriberService extends BaseService<
}
}
/**
* Retrieves a paginated list of subscribers based on the provided filters
* and populates the result with related data.
*
* @param filters - The criteria used to filter the subscribers.
* @param pageQuery - The pagination and sorting details.
*
* @returns A paginated list of subscribers with populated related data.
*/
async findPageAndPopulate(
filters: TFilterQuery<Subscriber>,
pageQuery: PageQueryDto<Subscriber>,
) {
return await this.repository.findPageAndPopulate(filters, pageQuery);
}
/**
* Finds and returns a single subscriber based on its ID or filters,
* and populates the result with related data.
*
* @param id - The ID of the subscriber or a set of query filters.
*
* @returns The subscriber with populated related data.
*/
async findOneAndPopulate(id: string | TFilterQuery<Subscriber>) {
return await this.repository.findOneAndPopulate(id);
}
/**
* Finds and returns a single subscriber based on a foreign ID.
*

View File

@ -33,8 +33,8 @@ export abstract class BaseService<
return await this.repository.findOne(criteria, options);
}
async findOneAndPopulate(id: string) {
return await this.repository.findOneAndPopulate(id);
async findOneAndPopulate(criteria: string | TFilterQuery<T>) {
return await this.repository.findOneAndPopulate(criteria);
}
async find(filter: TFilterQuery<T>, sort?: QuerySortDto<T>): Promise<T[]> {