Skip to content

Commit

Permalink
Merge pull request #200 from Hexastack/199-issue-duplicated-populate-…
Browse files Browse the repository at this point in the history
…methods

fix(api): remove duplicated populate methods
  • Loading branch information
marrouchi authored Oct 11, 2024
2 parents 3745d14 + 8442b85 commit 268663c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 85 deletions.
18 changes: 1 addition & 17 deletions api/src/chat/repositories/conversation.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { FilterQuery, Model } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository } from '@/utils/generics/base-repository';

Expand Down Expand Up @@ -49,20 +49,4 @@ export class ConversationRepository extends BaseRepository<
async end(convo: Conversation | ConversationFull) {
return await this.updateOne(convo.id, { active: false });
}

/**
* Finds a single conversation by a given criteria and populates the related fields: `sender`, `current`, and `next`.
*
* @param criteria The search criteria, either a string or a filter query.
*
* @returns A promise resolving to the populated conversation full object.
*/
async findOneAndPopulate(criteria: string | FilterQuery<Conversation>) {
const query = this.findOneQuery(criteria).populate([
'sender',
'current',
'next',
]);
return await this.executeOne(query, ConversationFull);
}
}
36 changes: 1 addition & 35 deletions api/src/user/repositories/permission.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model, TFilterQuery } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository } from '@/utils/generics/base-repository';

Expand All @@ -32,38 +32,4 @@ export class PermissionRepository extends BaseRepository<
) {
super(eventEmitter, model, Permission, PERMISSION_POPULATE, PermissionFull);
}

/**
* Finds all permissions and populates the `model` and `role` relationships.
*
* @returns A list of populated permission entities.
*/
async findAllAndPopulate() {
const query = this.findAllQuery().populate(['model', 'role']);
return await this.execute(query, PermissionFull);
}

/**
* Finds permissions based on the specified filter and populates the `model` and `role` relationships.
*
* @param filter - The filter query for finding permissions.
*
* @returns A list of populated permission entities matching the filter.
*/
async findAndPopulate(filter: TFilterQuery<Permission>) {
const query = this.findQuery(filter).populate(['model', 'role']);
return await this.execute(query, PermissionFull);
}

/**
* Finds a single permission by its ID and populates the `model` and `role` relationships.
*
* @param id - The ID of the permission to find.
*
* @returns The populated permission entity.
*/
async findOneAndPopulate(id: string) {
const query = this.findOneQuery(id).populate(['model', 'role']);
return await this.executeOne(query, PermissionFull);
}
}
34 changes: 1 addition & 33 deletions api/src/user/repositories/role.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model, TFilterQuery } from 'mongoose';
import { Model } from 'mongoose';

import { BaseRepository } from '@/utils/generics/base-repository';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';

import { Permission } from '../schemas/permission.schema';
import {
Expand Down Expand Up @@ -51,35 +50,4 @@ export class RoleRepository extends BaseRepository<
}
return result;
}

/**
* Finds and paginates Role entities based on filter criteria, and populates related fields.
*
* @param filter Filter criteria for querying Role entities.
* @param pageQuery Pagination details.
*
* @returns Paginated result of Role entities populated with related permissions and users.
*/
async findPageAndPopulate(
filter: TFilterQuery<Role>,
pageQuery: PageQueryDto<Role>,
) {
const query = this.findPageQuery(filter, pageQuery).populate([
'permissions',
'users',
]);
return await this.execute(query, RoleFull);
}

/**
* Finds a single Role entity by its ID, and populates related fields.
*
* @param id The ID of the Role to find.
*
* @returns The found Role entity populated with related permissions and users.
*/
async findOneAndPopulate(id: string) {
const query = this.findOneQuery(id).populate(['permissions', 'users']);
return await this.executeOne(query, RoleFull);
}
}

0 comments on commit 268663c

Please sign in to comment.