diff --git a/.vscode/settings.json b/.vscode/settings.json index 33d4973e1..eb138e9b7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,6 +33,6 @@ "source.fixAll": "explicit" }, "[typescript]": { - "editor.defaultFormatter": "dbaeumer.vscode-eslint" + "editor.defaultFormatter": "vscode.typescript-language-features" } } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 7b270c554..3226c220d 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -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; @@ -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 = { @@ -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, @@ -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({ diff --git a/src/modules/organisations/organisations.controller.ts b/src/modules/organisations/organisations.controller.ts index 739e6648a..c00414816 100644 --- a/src/modules/organisations/organisations.controller.ts +++ b/src/modules/organisations/organisations.controller.ts @@ -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) diff --git a/src/modules/organisations/organisations.service.ts b/src/modules/organisations/organisations.service.ts index e29d7f5c8..d0d6e425e 100644 --- a/src/modules/organisations/organisations.service.ts +++ b/src/modules/organisations/organisations.service.ts @@ -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 || '',