Skip to content

Commit

Permalink
Merge pull request #205 from Hexastack/204-issue-duplicated-service-m…
Browse files Browse the repository at this point in the history
…ethods

refactor(api): simplify services logic
  • Loading branch information
marrouchi authored Oct 13, 2024
2 parents 8944e5f + f53589b commit 88a1fee
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 115 deletions.
16 changes: 0 additions & 16 deletions api/src/chat/services/conversation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*
Expand Down
37 changes: 0 additions & 37 deletions api/src/chat/services/label.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
}
30 changes: 0 additions & 30 deletions api/src/chat/services/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 0 additions & 30 deletions api/src/chat/services/subscriber.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions api/src/utils/generics/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]> {
Expand Down

0 comments on commit 88a1fee

Please sign in to comment.