Skip to content

Commit

Permalink
Merge pull request #1303 from Domfa/feat/add-pagination-to-getallwait…
Browse files Browse the repository at this point in the history
…list-route

feat : implement pagination to get-all-waitlist
  • Loading branch information
TheCodeGhinux authored Mar 2, 2025
2 parents 16a572b + 9c124fd commit 1cf1446
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
64 changes: 61 additions & 3 deletions src/modules/waitlist/tests/waitlist.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('WaitlistService', () => {

const mockWaitlistRepository = {
find: jest.fn(),
findAndCount: jest.fn(),
save: jest.fn(),
create: jest.fn(),
findOne: jest.fn(),
Expand Down Expand Up @@ -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: '[email protected]' },
{ id: '2', full_name: 'Bob', email: '[email protected]' },
];
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: '[email protected]' },
{ id: '4', full_name: 'David', email: '[email protected]' },
];
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',
});
});
});

Expand Down
24 changes: 21 additions & 3 deletions src/modules/waitlist/waitlist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
},
};
}
}

0 comments on commit 1cf1446

Please sign in to comment.