From 258462cb3cba79082c524f21cc41cf9b55ff1cca Mon Sep 17 00:00:00 2001 From: Domfa Date: Fri, 28 Feb 2025 20:30:52 +0100 Subject: [PATCH] feat(waitlist): add pagination to getAllWaitlist --- .../waitlist/tests/waitlist.service.spec.ts | 64 ++++++++++++++++++- src/modules/waitlist/waitlist.service.ts | 24 ++++++- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/modules/waitlist/tests/waitlist.service.spec.ts b/src/modules/waitlist/tests/waitlist.service.spec.ts index f0a2647d5..2deeb0547 100644 --- a/src/modules/waitlist/tests/waitlist.service.spec.ts +++ b/src/modules/waitlist/tests/waitlist.service.spec.ts @@ -15,6 +15,7 @@ describe('WaitlistService', () => { const mockWaitlistRepository = { find: jest.fn(), + findAndCount: jest.fn(), save: jest.fn(), create: jest.fn(), findOne: jest.fn(), @@ -48,10 +49,67 @@ describe('WaitlistService', () => { }); describe('getAllWaitlist', () => { - it('should return all waitlist', async () => { - await waitlistService.getAllWaitlist(); + it('should return paginated waitlist results with default values when no parameters are provided', async () => { + // Arrange: setup default values (page = 1, limit = 10) + const mockWaitlistEntries = [ + { id: '1', full_name: 'Alice', email: 'alice@example.com' }, + { id: '2', full_name: 'Bob', email: 'bob@example.com' }, + ]; + const totalCount = 2; + mockWaitlistRepository.findAndCount = jest.fn().mockResolvedValue([mockWaitlistEntries, totalCount]); - expect(waitlistRepository.find).toHaveBeenCalled(); + // Act: call service without pagination parameters + const result = await waitlistService.getAllWaitlist(); + + // Assert: defaults (skip: 0, take: 10) should be used + expect(mockWaitlistRepository.findAndCount).toHaveBeenCalledWith({ + skip: 0, + take: 10, + }); + expect(result).toEqual({ + status: 'Success', + status_code: 200, + data: { + current_page: 1, + total_pages: 1, // since totalCount is 2 and default limit is 10 + total_waitlist_count: totalCount, + waitlist: mockWaitlistEntries, + }, + message: 'Waitlist found successfully', + }); + }); + + it('should return paginated waitlist results with provided page and limit', async () => { + // Arrange: setup custom pagination values + const mockWaitlistEntries = [ + { id: '3', full_name: 'Charlie', email: 'charlie@example.com' }, + { id: '4', full_name: 'David', email: 'david@example.com' }, + ]; + const totalCount = 20; + mockWaitlistRepository.findAndCount = jest.fn().mockResolvedValue([mockWaitlistEntries, totalCount]); + + const page = 2; + const limit = 5; + + // Act: call service with page and limit parameters + const result = await waitlistService.getAllWaitlist(page, limit); + + // Assert: ensure proper skip and take values + expect(mockWaitlistRepository.findAndCount).toHaveBeenCalledWith({ + skip: (page - 1) * limit, // (2-1)*5 = 5 + take: limit, // 5 + }); + expect(result).toEqual({ + status: 'Success', + status_code: 200, + data: { + current_page: page, // e.g., 2 + total_pages: Math.ceil(totalCount / limit), // e.g., 4 if totalCount is 20 and limit is 5 + total_waitlist_count: totalCount, + waitlist: mockWaitlistEntries, + }, + message: 'Waitlist found successfully', + }); }); }); diff --git a/src/modules/waitlist/waitlist.service.ts b/src/modules/waitlist/waitlist.service.ts index 49f5ba191..656d1c397 100644 --- a/src/modules/waitlist/waitlist.service.ts +++ b/src/modules/waitlist/waitlist.service.ts @@ -7,6 +7,7 @@ import { CreateWaitlistDto } from './dto/create-waitlist.dto'; import { WaitlistResponseDto } from './dto/create-waitlist-response.dto'; import * as SYS_MSG from '@shared/constants/SystemMessages'; import { CustomHttpException } from '@shared/helpers/custom-http-filter'; +import { STATUS_CODES } from 'http'; @Injectable() export default class WaitlistService { @@ -34,8 +35,25 @@ export default class WaitlistService { return { message: 'You are all signed up!' }; } - async getAllWaitlist() { - const waitlist = await this.waitlistRepository.find(); - return { message: 'Waitlist found successfully', data: { waitlist } }; + async getAllWaitlist(page: number = 1, limit: number = 10) { + if (page < 1) page = 1; + if (limit < 1) limit = 10; + + const [waitlist, total] = await this.waitlistRepository.findAndCount({ + skip: (page - 1) * limit, + take: limit, + }); + + return { + status: 'Success', + status_code: 200, + message: 'Waitlist found successfully', + data: { + waitlist, + total_waitlist_count: total, + current_page: page, + total_pages: Math.ceil(total / limit), + }, + }; } }