@@ -41,7 +41,8 @@ import {
41
41
IEcosystemConfig ,
42
42
IUserForgotPassword ,
43
43
ISessionDetails ,
44
- ISessions
44
+ ISessions ,
45
+ IUpdateAccountDetails
45
46
} from '../interfaces/user.interface' ;
46
47
import { AcceptRejectInvitationDto } from '../dtos/accept-reject-invitation.dto' ;
47
48
import { UserActivityService } from '@credebl/user-activity' ;
@@ -545,11 +546,50 @@ export class UserService {
545
546
try {
546
547
const data = jwt . decode ( refreshToken ) as jwt . JwtPayload ;
547
548
const userByKeycloakId = await this . userRepository . getUserByKeycloakId ( data ?. sub ) ;
549
+ this . logger . debug ( `User details::;${ JSON . stringify ( userByKeycloakId ) } ` ) ;
548
550
const tokenResponse = await this . clientRegistrationService . getAccessToken (
549
551
refreshToken ,
550
552
userByKeycloakId ?. [ 'clientId' ] ,
551
553
userByKeycloakId ?. [ 'clientSecret' ]
552
554
) ;
555
+ this . logger . debug ( `tokenResponse::::${ JSON . stringify ( tokenResponse ) } ` ) ;
556
+ // Fetch the details from account table based on userid and refresh token
557
+ const userAccountDetails = await this . userRepository . fetchAccountByRefreshToken (
558
+ userByKeycloakId ?. [ 'id' ] ,
559
+ refreshToken
560
+ ) ;
561
+ // Update the account details with latest access token, refresh token and exp date
562
+ if ( ! userAccountDetails ) {
563
+ throw new NotFoundException ( ResponseMessages . user . error . userAccountNotFound ) ;
564
+ }
565
+ const updateAccountDetails : IUpdateAccountDetails = {
566
+ accessToken : tokenResponse . access_token ,
567
+ // refreshToken: tokenResponse.refresh_token,
568
+ expiresAt : tokenResponse . expires_in ,
569
+ accountId : userAccountDetails . id
570
+ } ;
571
+ const updateAccountDetailsResponse = await this . userRepository . updateAccountDetailsById ( updateAccountDetails ) ;
572
+ // Delete the preveious session record and create new one
573
+ if ( ! updateAccountDetailsResponse ) {
574
+ throw new InternalServerErrorException ( ResponseMessages . user . error . errorInUpdateAccountDetails ) ;
575
+ }
576
+ const deletePreviousSession = await this . userRepository . deleteSessionRecordByRefreshToken ( refreshToken ) ;
577
+ if ( ! deletePreviousSession ) {
578
+ throw new InternalServerErrorException ( ResponseMessages . user . error . errorInDeleteSession ) ;
579
+ }
580
+ const sessionData = {
581
+ sessionToken : tokenResponse . access_token ,
582
+ userId : userByKeycloakId ?. [ 'id' ] ,
583
+ expires : tokenResponse . expires_in ,
584
+ // refreshToken: tokenResponse.refresh_token,
585
+ sessionType : SessionType . USER_SESSION ,
586
+ accountId : updateAccountDetailsResponse . id
587
+ } ;
588
+ const addSessionDetails = await this . userRepository . createSession ( sessionData ) ;
589
+ if ( ! addSessionDetails ) {
590
+ throw new InternalServerErrorException ( ResponseMessages . user . error . errorInSessionCreation ) ;
591
+ }
592
+
553
593
return tokenResponse ;
554
594
} catch ( error ) {
555
595
throw new BadRequestException ( ResponseMessages . user . error . invalidRefreshToken ) ;
0 commit comments