Skip to content

Commit

Permalink
refactor: adjust usersservice find to new query implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LhonRafaat committed Sep 14, 2024
1 parent afd5e6d commit d62eecf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
26 changes: 5 additions & 21 deletions src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import {
Controller,
Get,
Body,
Patch,
Param,
Delete,
UseGuards,
Req,
Query,
} from '@nestjs/common';
import { Controller, Get, Param, Delete, UseGuards, Req } from '@nestjs/common';
import { UsersService } from './users.service';
import { UpdateUserDto } from './dto/update-user.dto';
import {
ApiBearerAuth,
ApiExtraModels,
ApiOkResponse,
ApiTags,
} from '@nestjs/swagger';
import { TUser } from './user.model';
import { AuthGuard } from '@nestjs/passport';
import {
Action,
IQuery,
IRequest,
TResponse,
getResponseType,
Expand All @@ -41,13 +28,10 @@ export class UsersController {
@Get()
@ApiOkResponse(getResponseType(TUser))
@QueryTypes()
@UseGuards(AccessTokenGuard, AbilitiesGuard)
@checkAbilities({ action: Action.Read, subject: TUser })
async findAll(
@Req() req: IRequest,
@Query() query: IQuery,
): Promise<TResponse<TUser>> {
return this.usersService.findAll(req, query);
// @UseGuards(AccessTokenGuard, AbilitiesGuard)
// @checkAbilities({ action: Action.Read, subject: TUser })
async findAll(@Req() req: IRequest): Promise<TResponse<TUser>> {
return this.usersService.findAll(req);
}

@UseGuards(AccessTokenGuard)
Expand Down
24 changes: 16 additions & 8 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@ import { Model } from 'mongoose';
import { TUser } from './user.model';
import { RegisterPayload } from '../auth/dto/register.payload';
import * as bcrypt from 'bcrypt';
import { IQuery, IRequest, TResponse } from '../../common/helper/common-types';
import { IRequest, TResponse } from '../../common/helper/common-types';

@Injectable()
export class UsersService {
constructor(@InjectModel('User') private readonly userModel: Model<TUser>) {}

async findAll(req: IRequest, query: IQuery): Promise<TResponse<TUser>> {
async findAll(req: IRequest): Promise<TResponse<TUser>> {
console.log(req.queryObj);
const users = this.userModel
.find({
...req.queryObj,
...req.dateQr,
...req.queryObj?.regular,
})
.sort({ [query.sort]: query.sortBy === 'desc' ? -1 : 1 });
.sort({
[req.pagination.sort]: req.pagination.sortBy === 'desc' ? -1 : 1,
});
if (req.queryObj?.references) {
users.populate(
req.queryObj?.references.paths,
req.queryObj?.references.value,
);
}

const total = await users.clone().countDocuments();

users.limit(+query.limit).skip(req.skip);
users.limit(req.pagination.limit).skip(req.pagination.skip);

const response: TResponse<TUser> = {
result: await users.exec(),
count: total,
limit: +query.limit,
page: +query.page,
limit: req.pagination.limit,
page: req.pagination.page,
};

return response;
Expand Down

0 comments on commit d62eecf

Please sign in to comment.