diff --git a/apps/api/src/modules/notifications/dtos/notification-response.dto.ts b/apps/api/src/modules/notifications/dtos/notification-response.dto.ts new file mode 100644 index 00000000..234deec0 --- /dev/null +++ b/apps/api/src/modules/notifications/dtos/notification-response.dto.ts @@ -0,0 +1,17 @@ +import { ObjectType, Field, Int } from '@nestjs/graphql'; +import { Notification } from '../entities/notification.entity'; + +@ObjectType() +export class NotificationResponse { + @Field(() => [Notification]) + notifications: Notification[]; + + @Field(() => Int) + total: number; + + @Field(() => Int) + offset: number; + + @Field(() => Int) + limit: number; +} diff --git a/apps/api/src/modules/notifications/dtos/query-options.dto.ts b/apps/api/src/modules/notifications/dtos/query-options.dto.ts new file mode 100644 index 00000000..90fa5132 --- /dev/null +++ b/apps/api/src/modules/notifications/dtos/query-options.dto.ts @@ -0,0 +1,17 @@ +import { Field, Int, InputType } from '@nestjs/graphql'; +import { IsOptional, IsInt, Min } from 'class-validator'; + +@InputType() +export class QueryOptionsDto { + @Field(() => Int, { defaultValue: 0 }) + @IsOptional() + @IsInt() + @Min(0) + offset?: number = 0; + + @Field(() => Int, { defaultValue: 10 }) + @IsOptional() + @IsInt() + @Min(1) + limit?: number = 10; +} diff --git a/apps/api/src/modules/notifications/notifications.resolver.ts b/apps/api/src/modules/notifications/notifications.resolver.ts index a5a6ddad..a5041d6b 100644 --- a/apps/api/src/modules/notifications/notifications.resolver.ts +++ b/apps/api/src/modules/notifications/notifications.resolver.ts @@ -1,18 +1,23 @@ -import { Query, Resolver } from '@nestjs/graphql'; +import { Args, Query, Resolver } from '@nestjs/graphql'; import { NotificationsService } from './notifications.service'; import { Notification } from './entities/notification.entity'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { ApiKeyGuard } from 'src/common/guards/api-key/api-key.guard'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { UseGuards } from '@nestjs/common'; +import { NotificationResponse } from './dtos/notification-response.dto'; +import { QueryOptionsDto } from './dtos/query-options.dto'; @Resolver(() => Notification) @UseGuards(ApiKeyGuard) export class NotificationsResolver { constructor(private readonly notificationsService: NotificationsService) {} - @Query(() => [Notification], { name: 'notifications' }) - async findAll(): Promise { - return this.notificationsService.getAllNotifications(); + @Query(() => NotificationResponse, { name: 'notifications' }) + async findAll( + @Args('options', { type: () => QueryOptionsDto, nullable: true, defaultValue: {} }) + options: QueryOptionsDto, + ): Promise { + return this.notificationsService.getAllNotifications(options); } } diff --git a/apps/api/src/modules/notifications/notifications.service.ts b/apps/api/src/modules/notifications/notifications.service.ts index d01bd5d8..0dadb180 100644 --- a/apps/api/src/modules/notifications/notifications.service.ts +++ b/apps/api/src/modules/notifications/notifications.service.ts @@ -7,6 +7,8 @@ import { NotificationQueueProducer } from 'src/jobs/producers/notifications/noti import { Status } from 'src/common/constants/database'; import { CreateNotificationDto } from './dtos/create-notification.dto'; import { ConfigService } from '@nestjs/config'; +import { QueryOptionsDto } from './dtos/query-options.dto'; +import { NotificationResponse } from './dtos/notification-response.dto'; @Injectable() export class NotificationsService { @@ -98,12 +100,16 @@ export class NotificationsService { }); } - getAllNotifications(): Promise { - this.logger.log('Getting all active notifications'); - return this.notificationRepository.find({ + async getAllNotifications(options: QueryOptionsDto): Promise { + this.logger.log('Getting all active notifications with options'); + const [notifications, total] = await this.notificationRepository.findAndCount({ where: { status: Status.ACTIVE, }, + skip: options.offset, + take: options.limit, }); + + return { notifications, total, offset: options.offset, limit: options.limit }; } }