-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1262 from G4EVA-dev/feat-implement-pagination-on-…
…findAllInvitations-Endpoint Feat: Implement pagination on find all invitations endpoint
- Loading branch information
Showing
7 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import { InviteDto } from './invite.dto'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class PaginatedInvitationsDto { | ||
@ApiProperty({ type: [InviteDto] }) | ||
invitations: InviteDto[]; | ||
|
||
@ApiProperty({ example: 250, description: 'Total number of invitations' }) | ||
total: number; | ||
} | ||
|
||
export class FindAllInvitationsResponseDto { | ||
@ApiProperty({ example: 'success' }) | ||
status: string; | ||
|
||
@ApiProperty({ example: 200 }) | ||
status_code: number; | ||
|
||
@ApiProperty({ example: 'Successfully fetched invites' }) | ||
@ApiProperty({ example: 'Invitations retrieved successfully' }) | ||
message: string; | ||
|
||
@ApiProperty({ type: [InviteDto] }) | ||
data: InviteDto[]; | ||
@ApiProperty({ type: PaginatedInvitationsDto }) | ||
data: PaginatedInvitationsDto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsOptional, IsInt, Min } from 'class-validator'; | ||
import { Type } from 'class-transformer'; | ||
|
||
export class PaginationQueryDto { | ||
@ApiProperty({ | ||
description: 'Page number (starts at 1)', | ||
required: false, | ||
default: 1, | ||
type: Number, | ||
}) | ||
@IsOptional() | ||
@Type(() => Number) | ||
@IsInt() | ||
@Min(1) | ||
page?: number = 1; | ||
|
||
@ApiProperty({ | ||
description: 'Number of items per page', | ||
required: false, | ||
default: 10, | ||
type: Number, | ||
}) | ||
@IsOptional() | ||
@Type(() => Number) | ||
@IsInt() | ||
@Min(1) | ||
limit?: number = 10; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { InviteService } from '../invite.service'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Invite } from '../entities/invite.entity'; | ||
import { Organisation } from '@modules/organisations/entities/organisations.entity'; | ||
import { User } from '@modules/user/entities/user.entity'; | ||
import { HttpStatus, InternalServerErrorException } from '@nestjs/common'; | ||
import { MailerService } from '@nestjs-modules/mailer'; | ||
import { EmailService } from '@modules/email/email.service'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { OrganisationsService } from '@modules/organisations/organisations.service'; | ||
import { Repository, EntityManager } from 'typeorm'; | ||
|
||
describe('InviteService', () => { | ||
let service: InviteService; | ||
let inviteRepository: jest.Mocked<Repository<Invite>>; | ||
|
||
const mockInviteRepository = { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
findAndCount: jest.fn(), | ||
create: jest.fn(), | ||
save: jest.fn(), | ||
}; | ||
|
||
const mockOrganisationRepository = { | ||
findOne: jest.fn(), | ||
}; | ||
|
||
const mockUserRepository = { | ||
findOne: jest.fn(), | ||
}; | ||
|
||
const mockMailerService = { | ||
sendMail: jest.fn(), | ||
}; | ||
|
||
const mockEmailService = { | ||
getTemplate: jest.fn(), | ||
}; | ||
|
||
const mockConfigService = { | ||
get: jest.fn(), | ||
}; | ||
|
||
const mockOrganisationsService = { | ||
addOrganisationMember: jest.fn(), | ||
}; | ||
|
||
// Add a mock for EntityManager | ||
const mockEntityManager = { | ||
transaction: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
InviteService, | ||
{ provide: getRepositoryToken(Invite), useValue: mockInviteRepository }, | ||
{ provide: getRepositoryToken(Organisation), useValue: mockOrganisationRepository }, | ||
{ provide: getRepositoryToken(User), useValue: mockUserRepository }, | ||
{ provide: MailerService, useValue: mockMailerService }, | ||
{ provide: EmailService, useValue: mockEmailService }, | ||
{ provide: ConfigService, useValue: mockConfigService }, | ||
{ provide: OrganisationsService, useValue: mockOrganisationsService }, | ||
{ provide: EntityManager, useValue: mockEntityManager }, // Add this line | ||
], | ||
}).compile(); | ||
|
||
service = module.get<InviteService>(InviteService); | ||
inviteRepository = module.get(getRepositoryToken(Invite)); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('findAllInvitations', () => { | ||
it('should return paginated invitations with default parameters', async () => { | ||
const mockInvites = [ | ||
{ | ||
id: '1', | ||
token: 'token1', | ||
isAccepted: false, | ||
isGeneric: true, | ||
organisation: { id: 'org1', name: 'Org 1' }, | ||
email: '[email protected]', | ||
}, | ||
]; | ||
|
||
mockInviteRepository.findAndCount.mockResolvedValue([mockInvites, 1]); | ||
|
||
const result = await service.findAllInvitations(); | ||
|
||
expect(mockInviteRepository.findAndCount).toHaveBeenCalledWith({ | ||
skip: 0, | ||
take: 10, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
status: 'success', | ||
status_code: HttpStatus.OK, | ||
message: 'Invitations retrieved successfully', | ||
data: { | ||
invitations: expect.any(Array), | ||
total: 1, | ||
}, | ||
}); | ||
expect(result.data.invitations).toHaveLength(1); | ||
}); | ||
|
||
it('should return paginated invitations with custom parameters', async () => { | ||
const mockInvites = [ | ||
{ | ||
id: '1', | ||
token: 'token1', | ||
isAccepted: false, | ||
isGeneric: true, | ||
organisation: { id: 'org1', name: 'Org 1' }, | ||
email: '[email protected]', | ||
}, | ||
{ | ||
id: '2', | ||
token: 'token2', | ||
isAccepted: true, | ||
isGeneric: false, | ||
organisation: { id: 'org2', name: 'Org 2' }, | ||
email: '[email protected]', | ||
}, | ||
]; | ||
|
||
mockInviteRepository.findAndCount.mockResolvedValue([mockInvites, 10]); | ||
|
||
const result = await service.findAllInvitations(2, 5); | ||
|
||
expect(mockInviteRepository.findAndCount).toHaveBeenCalledWith({ | ||
skip: 5, | ||
take: 5, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
status: 'success', | ||
status_code: HttpStatus.OK, | ||
message: 'Invitations retrieved successfully', | ||
data: { | ||
invitations: expect.any(Array), | ||
total: 10, | ||
}, | ||
}); | ||
expect(result.data.invitations).toHaveLength(2); | ||
}); | ||
|
||
it('should handle errors and throw InternalServerErrorException', async () => { | ||
mockInviteRepository.findAndCount.mockRejectedValue(new Error('Database error')); | ||
|
||
await expect(service.findAllInvitations()).rejects.toThrow(InternalServerErrorException); | ||
}); | ||
}); | ||
}); |