Skip to content

Commit

Permalink
feat(getUserOrganisations): add pagination support
Browse files Browse the repository at this point in the history
- Implemented pagination using  and  parameters
- Ensured that only authorized users can retrieve paginated results
- Improved response structure for better readability
  • Loading branch information
dax-side committed Feb 28, 2025
1 parent fd43262 commit f53ecfa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"source.fixAll": "explicit"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.defaultFormatter": "vscode.typescript-language-features"
}
}
8 changes: 4 additions & 4 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class AuthenticationService {

const newOrganisation = await this.organisationService.create(newOrganisationPayload, user.id);

const userOranisations = await this.organisationService.getAllUserOrganisations(user.id);
const userOranisations = await this.organisationService.getAllUserOrganisations(user.id, 1, 10);
const isSuperAdmin = userOranisations.map(instance => instance.user_role).includes('super-admin');
const token = (await this.otpService.createOtp(user.id)).token;

Expand Down Expand Up @@ -174,7 +174,7 @@ export default class AuthenticationService {
if (!isMatch) {
throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.UNAUTHORIZED);
}
const userOranisations = await this.organisationService.getAllUserOrganisations(user.id);
const userOranisations = await this.organisationService.getAllUserOrganisations(user.id, 1, 10);
const access_token = this.jwtService.sign({ id: user.id, sub: user.id });
const isSuperAdmin = userOranisations.map(instance => instance.user_role).includes('super-admin');
const responsePayload = {
Expand Down Expand Up @@ -328,7 +328,7 @@ export default class AuthenticationService {
return await this.createUserGoogle(userCreationPayload);
}

const userOranisations = await this.organisationService.getAllUserOrganisations(userExists.id);
const userOranisations = await this.organisationService.getAllUserOrganisations(userExists.id, 1, 10);
const isSuperAdmin = userOranisations.map(instance => instance.user_role).includes('super-admin');
const accessToken = this.jwtService.sign({
sub: userExists.id,
Expand Down Expand Up @@ -376,7 +376,7 @@ export default class AuthenticationService {

await this.organisationService.create(newOrganisationPaload, newUser.id);

const userOranisations = await this.organisationService.getAllUserOrganisations(newUser.id);
const userOranisations = await this.organisationService.getAllUserOrganisations(newUser.id, 1, 10);
const isSuperAdmin = userOranisations.map(instance => instance.user_role).includes('super-admin');

const accessToken = this.jwtService.sign({
Expand Down
8 changes: 6 additions & 2 deletions src/modules/organisations/organisations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ export class OrganisationsController {
@ApiResponse({ status: 200, description: 'Organisations retrieved successfully', type: UserOrganizationResponseDto })
@ApiResponse({ status: 400, description: 'Bad request', type: UserOrganizationErrorResponseDto })
@Get('/')
async getUserOrganisations(@Req() req) {
async getUserOrganisations(
@Req() req,
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
@Query('page_size', new DefaultValuePipe(10), ParseIntPipe) page_size: number
) {
const { sub } = req.user;
return this.organisationsService.getUserOrganisations(sub);
return this.organisationsService.getUserOrganisations(sub, page, page_size);
}

@UseGuards(OwnershipGuard)
Expand Down
20 changes: 16 additions & 4 deletions src/modules/organisations/organisations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,37 @@ export class OrganisationsService {
};
}

async getUserOrganisations(userId: string) {
const organisations = await this.getAllUserOrganisations(userId);
async getUserOrganisations(userId: string, page: number, page_size: number) {
const organisations = await this.getAllUserOrganisations(userId, page, page_size);
const total_count = await this.organisationUserRole.count({ where: { userId } });

return {
status_code: HttpStatus.OK,
message: 'Organisations retrieved successfully',
data: organisations,
data: {
organisations,
total_count,
current_page: page,
page_size,
},
};
}

async getAllUserOrganisations(userId: string) {
async getAllUserOrganisations(userId: string, page: number, page_size: number) {
const user = await this.userRepository.findOne({ where: { id: userId } });

if (!user) {
throw new CustomHttpException('Invalid Request', HttpStatus.BAD_REQUEST);
}

const skip = (page - 1) * page_size;

const userOrganisations = (
await this.organisationUserRole.find({
where: { userId },
relations: ['organisation', 'organisation.owner', 'role'],
skip,
take: page_size,
})
).map(instance => ({
organisation_id: instance?.organisation?.id || '',
Expand Down

0 comments on commit f53ecfa

Please sign in to comment.