Skip to content

Commit c6b330b

Browse files
committed
fix:update fetch owner org details query
Signed-off-by: shitrerohit <[email protected]>
1 parent 9751adc commit c6b330b

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

apps/organization/repositories/organization.repository.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,14 @@ export class OrganizationRepository {
760760
throw error;
761761
}
762762
}
763-
async getOrgAndAdminUser(orgId: string): Promise<user_org_roles> {
763+
async getOrgAndOwnerUser(orgId: string): Promise<user_org_roles> {
764764
try {
765765
return this.prisma.user_org_roles.findFirst({
766766
where: {
767-
orgId
768-
// orgRole:{
769-
// name:'admin'
770-
// }
767+
orgId,
768+
orgRole: {
769+
name: 'owner'
770+
}
771771
},
772772
include: {
773773
user: {

apps/organization/src/organization.service.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -696,26 +696,34 @@ export class OrganizationService {
696696
return response;
697697
}
698698

699+
/**
700+
* Method used for generate access token based on client-id and client secret
701+
* @param clientCredentials
702+
* @returns session and access token both
703+
*/
699704
async clientLoginCredentails(clientCredentials: IClientCredentials): Promise<IAccessTokenData> {
700705
const { clientId, clientSecret } = clientCredentials;
706+
// This method used to authenticate the requested user on keycloak
701707
const authenticationResult = await this.authenticateClientKeycloak(clientId, clientSecret);
702708
let addSessionDetails;
703-
// Fetch organization details for getting the user id
704-
const orgRoleDetails = await this.organizationRepository.getOrgAndAdminUser(clientId);
705-
this.logger.debug(`orgRoleDetails::::${JSON.stringify(orgRoleDetails)}`);
706-
// check seesion details
709+
// Fetch owner organization details for getting the user id
710+
const orgRoleDetails = await this.organizationRepository.getOrgAndOwnerUser(clientId);
711+
// Fetch the total number of sessions for the requested user to check and restrict the creation of multiple sessions.
707712
const userSessionDetails = await this.userRepository.fetchUserSessions(orgRoleDetails['user'].id);
708713
if (Number(process.env.SESSIONS_LIMIT) <= userSessionDetails?.length) {
709714
throw new BadRequestException(ResponseMessages.user.error.sessionLimitReached);
710715
}
711-
// Creation sessison and account
716+
// Session payload
712717
const sessionData = {
713718
sessionToken: authenticationResult?.access_token,
714719
userId: orgRoleDetails['user'].id,
715720
expires: authenticationResult?.expires_in,
716721
sessionType: SessionType.ORG_SESSION
717722
};
718-
723+
// Note:
724+
// Fetch account details to check whether the requested user account exists
725+
// If the account exists, update it with the latest details and create a new session
726+
// Otherwise, create a new account and also create the new session
719727
const fetchAccountDetails = await this.userRepository.checkAccountDetails(orgRoleDetails['user'].id);
720728
if (fetchAccountDetails) {
721729
const accountData = {
@@ -729,6 +737,9 @@ export class OrganizationService {
729737
addSessionDetails = await this.userRepository.createSession(finalSessionData);
730738
});
731739
} else {
740+
// Note:
741+
// This else block is mostly used for already registered users on the platform to create their account & session in the database.
742+
// Once all users are migrated or created their accounts and sessions in the DB, this code can be removed.
732743
const accountData = {
733744
sessionToken: authenticationResult?.access_token,
734745
userId: orgRoleDetails['user'].id,
@@ -742,12 +753,11 @@ export class OrganizationService {
742753
addSessionDetails = await this.userRepository.createSession(finalSessionData);
743754
});
744755
}
745-
// Response: add session id as cookies
756+
// Response: add session id
746757
const finalResponse = {
747758
...authenticationResult,
748759
sessionId: addSessionDetails.id
749760
};
750-
// In fetch session API need to handle the conditon for session is comes from cookies or query parameter
751761
return finalResponse;
752762
}
753763

0 commit comments

Comments
 (0)