From 214dd2b3d9c24ec1deddbb7223690a0927215cb6 Mon Sep 17 00:00:00 2001 From: Nishad Date: Tue, 5 Mar 2024 16:23:06 +0530 Subject: [PATCH] worked on the client & user login credential message, worked on the error handling for conflict organization create Signed-off-by: Nishad --- .../repositories/organization.repository.ts | 14 ++++++++ apps/organization/src/organization.service.ts | 36 +++++++++++-------- apps/user/src/user.service.ts | 2 +- libs/common/src/response-messages/index.ts | 3 +- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/apps/organization/repositories/organization.repository.ts b/apps/organization/repositories/organization.repository.ts index 930fe04d7..741d8bd09 100644 --- a/apps/organization/repositories/organization.repository.ts +++ b/apps/organization/repositories/organization.repository.ts @@ -42,6 +42,20 @@ export class OrganizationRepository { } } + + async checkOrganizationSlugExist(orgSlug: string): Promise { + try { + return this.prisma.organisation.findUnique({ + where: { + orgSlug + } + }); + } catch (error) { + this.logger.error(`error in checkOrganizationSlugExist: ${JSON.stringify(error)}`); + throw error; + } + } + /** * * @Body createOrgDtp diff --git a/apps/organization/src/organization.service.ts b/apps/organization/src/organization.service.ts index a82df7f25..0d3154c44 100644 --- a/apps/organization/src/organization.service.ts +++ b/apps/organization/src/organization.service.ts @@ -1,6 +1,6 @@ /* eslint-disable prefer-destructuring */ import { organisation, user } from '@prisma/client'; -import { Injectable, Logger, ConflictException, InternalServerErrorException, HttpException, BadRequestException, ForbiddenException } from '@nestjs/common'; +import { Injectable, Logger, ConflictException, InternalServerErrorException, HttpException, BadRequestException, ForbiddenException, UnauthorizedException } from '@nestjs/common'; import { PrismaService } from '@credebl/prisma-service'; import { CommonService } from '@credebl/common'; import { OrganizationRepository } from '../repositories/organization.repository'; @@ -63,6 +63,13 @@ export class OrganizationService { } const orgSlug = this.createOrgSlug(createOrgDto.name); + + const isOrgSlugExist = await this.organizationRepository.checkOrganizationSlugExist(orgSlug); + + if (isOrgSlugExist) { + throw new ConflictException(ResponseMessages.organisation.error.exists); + } + createOrgDto.orgSlug = orgSlug; createOrgDto.createdBy = userId; createOrgDto.lastChangedBy = userId; @@ -353,31 +360,32 @@ export class OrganizationService { } async clientLoginCredentails(clientCredentials: IClientCredentials): Promise { - - const {clientId, clientSecret} = clientCredentials; - return this.authenticateClientKeycloak(clientId, clientSecret); - } + const {clientId, clientSecret} = clientCredentials; + return this.authenticateClientKeycloak(clientId, clientSecret); +} async authenticateClientKeycloak(clientId: string, clientSecret: string): Promise { - + try { + const payload = new ClientCredentialTokenPayloadDto(); + // eslint-disable-next-line camelcase + payload.client_id = clientId; + // eslint-disable-next-line camelcase + payload.client_secret = clientSecret; + payload.scope = 'email profile'; - const payload = new ClientCredentialTokenPayloadDto(); - // eslint-disable-next-line camelcase - payload.client_id = clientId; - // eslint-disable-next-line camelcase - payload.client_secret = clientSecret; - payload.scope = 'email profile'; - + try { const mgmtTokenResponse = await this.clientRegistrationService.getToken(payload); return mgmtTokenResponse; + } catch (error) { + throw new UnauthorizedException(ResponseMessages.organisation.error.invalidClient); + } } catch (error) { this.logger.error(`Error in authenticateClientKeycloak : ${JSON.stringify(error)}`); throw new RpcException(error.response ? error.response : error); } - } /** diff --git a/apps/user/src/user.service.ts b/apps/user/src/user.service.ts index f9e112b41..cfffa20a4 100644 --- a/apps/user/src/user.service.ts +++ b/apps/user/src/user.service.ts @@ -562,7 +562,7 @@ export class UserService { tokenResponse.isRegisteredToSupabase = false; return tokenResponse; } catch (error) { - throw new UnauthorizedException(error?.message); + throw new UnauthorizedException(ResponseMessages.user.error.invalidCredentials); } } else { diff --git a/libs/common/src/response-messages/index.ts b/libs/common/src/response-messages/index.ts index 436d90506..a0bf8fd81 100644 --- a/libs/common/src/response-messages/index.ts +++ b/libs/common/src/response-messages/index.ts @@ -108,7 +108,8 @@ export const ResponseMessages = { invalidInvitationId:'Invalid format for invitation id', ecosystemIdIsRequired:'ecosystemId is required', roleNotMatch: 'User does not have access', - orgDoesNotMatch: 'Organization does not match' + orgDoesNotMatch: 'Organization does not match', + invalidClient: 'Invalid client credentials' } },