Skip to content

Commit

Permalink
fix: issue with stats count on admin panel (#618)
Browse files Browse the repository at this point in the history
* fix: create a dto for the query

* fix: prettier

---------

Co-authored-by: orig <[email protected]>
  • Loading branch information
origranot and orig authored Dec 18, 2023
1 parent 1040ea6 commit 518def2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
18 changes: 18 additions & 0 deletions apps/backend/src/core/users/dto/count-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IsOptional, IsBoolean, IsDateString, IsString, IsBooleanString, IsDate } from 'class-validator';
import { Transform } from 'class-transformer';

export class CountQueryDto {
@IsOptional()
@IsDate()
@Transform(({ value }) => (value ? new Date(value) : null))
startDate: Date;

@IsOptional()
@IsDate()
@Transform(({ value }) => (value ? new Date(value) : null))
endDate: Date;

@IsOptional()
@IsBooleanString()
verified: boolean;
}
1 change: 1 addition & 0 deletions apps/backend/src/core/users/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './find-all-query.dto';
export * from './count-query.dto';
14 changes: 5 additions & 9 deletions apps/backend/src/core/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { Role, User } from '@reduced.to/prisma';
import { FindAllQueryDto } from './dto';
import { FindAllQueryDto, CountQueryDto } from './dto';
import { UsersService } from './users.service';
import { JwtAuthGuard } from '../../auth/guards/jwt.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
Expand Down Expand Up @@ -30,15 +30,11 @@ export class UsersController {

@Get('count')
@Roles(Role.ADMIN)
async count(@Query('startDate') startDate: Date, @Query('endDate') endDate: Date, @Query('verified') verified: boolean) {
// Create a filter object based on the query parameters
async count(@Query() { startDate, endDate, verified }: CountQueryDto) {
const filter: Record<string, any> = {};

if (!isNaN(startDate.getTime()) && !isNaN(endDate.getTime())) {
filter.createdAt = {
gte: startDate,
lte: endDate,
};
if (startDate && endDate) {
filter.createdAt = { gte: startDate, lte: endDate };
}

if (typeof verified === 'boolean') {
Expand Down

0 comments on commit 518def2

Please sign in to comment.