From fcd48beac7a95caae875fa8df6ad08e9bbdace65 Mon Sep 17 00:00:00 2001 From: midhun-aot Date: Fri, 6 Sep 2024 12:34:40 -0700 Subject: [PATCH] add log statement for user controller --- .../src/app/controllers/user.controller.ts | 125 +++++++++++------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/backend/users/src/app/controllers/user.controller.ts b/backend/users/src/app/controllers/user.controller.ts index 33977330..148c515d 100644 --- a/backend/users/src/app/controllers/user.controller.ts +++ b/backend/users/src/app/controllers/user.controller.ts @@ -1,59 +1,84 @@ -import { Controller, Post, Body, HttpException, HttpStatus } from '@nestjs/common'; -import { Resource, RoleMatchingMode, Roles, Unprotected } from 'nest-keycloak-connect'; +import { + Controller, + Post, + Body, + HttpException, + HttpStatus, +} from '@nestjs/common'; +import { + Resource, + RoleMatchingMode, + Roles, + Unprotected, +} from 'nest-keycloak-connect'; import { KeycloakService } from 'src/app/services/keycloak.service'; import { AddUserToGroupDto } from 'src/app/dto/addUserToGroup'; @Controller('users') @Resource('user-service') export class UserController { - constructor(private readonly keyCloakService: KeycloakService) {} + constructor(private readonly keyCloakService: KeycloakService) {} - /** - * Add user to a group in Keycloak. - * @param addUserToGroupDto - Object containing userId. - * @returns Object indicating success status and message. - */ - @Post('/addGroup') - @Roles({ roles: ['user-admin'], mode: RoleMatchingMode.ANY }) - async addUserToGroup(@Body() addUserToGroupDto: AddUserToGroupDto): Promise { - try - { - const { userId } = addUserToGroupDto; - - // Get access token from Keycloak - const accessToken = await this.keyCloakService.getToken(); - if (!accessToken) - { - throw new HttpException('Failed to get access token', HttpStatus.INTERNAL_SERVER_ERROR); - } - - // Find group ID by name - const groupName = 'formsflow-client'; // Assuming 'formflow-client' is the group name - const groupId = await this.keyCloakService.getGroupIdByName(groupName, accessToken); - if (!groupId) - { - throw new HttpException(`Group '${groupName}' not found`, HttpStatus.NOT_FOUND); - } - - // Add user to group - const result = await this.keyCloakService.addUserToGroup(userId, groupId, accessToken); - if(result.success) - { - return result; - } - } - catch (error) - { - // Handle errors - if (error.response && error.response.data && error.response.data.error) - { - // If Keycloak returns an error message, throw a Bad Request exception with the error message - throw new HttpException(error.response.data.error, HttpStatus.BAD_REQUEST); - } - else { - // If any other error occurs, throw an Internal Server Error exception - throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); - } - } + /** + * Add user to a group in Keycloak. + * @param addUserToGroupDto - Object containing userId. + * @returns Object indicating success status and message. + */ + @Post('/addGroup') + @Roles({ roles: ['user-admin'], mode: RoleMatchingMode.ANY }) + async addUserToGroup( + @Body() addUserToGroupDto: AddUserToGroupDto, + ): Promise { + try { + const { userId } = addUserToGroupDto; + + // Get access token from Keycloak + const accessToken = await this.keyCloakService.getToken(); + if (!accessToken) { + throw new HttpException( + 'Failed to get access token', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + + // Find group ID by name + const groupName = 'formsflow-client'; // Assuming 'formflow-client' is the group name + const groupId = await this.keyCloakService.getGroupIdByName( + groupName, + accessToken, + ); + if (!groupId) { + throw new HttpException( + `Group '${groupName}' not found`, + HttpStatus.NOT_FOUND, + ); + } + + // Add user to group + const result = await this.keyCloakService.addUserToGroup( + userId, + groupId, + accessToken, + ); + if (result.success) { + return result; + } + } catch (error) { + console.log('addUserToGroup error', error); + // Handle errors + if (error.response && error.response.data && error.response.data.error) { + // If Keycloak returns an error message, throw a Bad Request exception with the error message + throw new HttpException( + error.response.data.error, + HttpStatus.BAD_REQUEST, + ); + } else { + // If any other error occurs, throw an Internal Server Error exception + throw new HttpException( + 'Internal server error', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } + } }