@@ -5,7 +5,7 @@ import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
55import { AlertService } from '../../services/alert.service' ;
66import { ClaContributorService } from 'src/app/core/services/cla-contributor.service' ;
77import { ProjectModel } from 'src/app/core/models/project' ;
8- import { UserModel } from 'src/app/core/models/user' ;
8+ import { UserFromSessionModel , UserFromTokenModel , UserModel } from 'src/app/core/models/user' ;
99import { StorageService } from '../../services/storage.service' ;
1010import { AppSettings } from 'src/app/config/app-settings' ;
1111
@@ -19,10 +19,12 @@ export class ProjectTitleComponent implements OnInit {
1919 @Input ( ) userId : string ;
2020 @Output ( ) errorEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2121 @Output ( ) successEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
22+ @Output ( ) setUserIdEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2223
2324 project = new ProjectModel ( ) ;
25+ userFromToken = new UserFromTokenModel ( ) ;
2426 user = new UserModel ( ) ;
25-
27+ userFromSession = new UserFromSessionModel ( ) ;
2628 constructor (
2729 private alertService : AlertService ,
2830 private storageService : StorageService ,
@@ -39,18 +41,12 @@ export class ProjectTitleComponent implements OnInit {
3941 }
4042
4143 validateGithubFlow ( ) {
42- if ( this . projectId && this . userId ) {
43- const localProjectId = JSON . parse ( this . storageService . getItem ( AppSettings . PROJECT_ID ) ) ;
44- const localUserId = JSON . parse ( this . storageService . getItem ( AppSettings . USER_ID ) ) ;
45- if ( localProjectId !== this . projectId ) {
46- this . getProject ( ) ;
47- } else {
48- this . successEmitter . emit ( 'Project' ) ;
49- this . project . project_name = JSON . parse ( this . storageService . getItem ( AppSettings . PROJECT_NAME ) ) ;
50- }
51-
52- if ( localUserId !== this . userId ) {
44+ if ( this . projectId ) {
45+ this . getProject ( ) ;
46+ if ( this . userId ) {
5347 this . getUser ( ) ;
48+ } else {
49+ this . getUserFromToken ( ) ;
5450 }
5551 } else {
5652 this . errorEmitter . emit ( true ) ;
@@ -79,7 +75,6 @@ export class ProjectTitleComponent implements OnInit {
7975 }
8076 }
8177
82-
8378 getUser ( ) {
8479 if ( this . userId ) {
8580 this . claContributorService . getUser ( this . userId ) . subscribe (
@@ -101,6 +96,51 @@ export class ProjectTitleComponent implements OnInit {
10196 }
10297
10398
99+ getUserFromToken ( ) {
100+ this . claContributorService . getUserFromToken ( ) . subscribe (
101+ ( response ) => {
102+ console . log ( 'getUserFromToken response ==>' , response )
103+ this . userFromToken = response ;
104+ this . storageService . setItem ( AppSettings . USER_ID , this . userFromToken . userID ) ;
105+ this . storageService . setItem ( AppSettings . USER , this . userFromToken ) ;
106+ this . successEmitter . emit ( 'User' ) ;
107+ this . setUserIdEmitter . emit ( this . userFromToken . userID ) ;
108+ } ,
109+ ( ) => {
110+ // If user is not found in token, get user from session
111+ this . getUserFromSession ( ) ;
112+ }
113+ ) ;
114+ }
115+
116+ getUserFromSession ( ) {
117+ this . claContributorService . getUserFromSession ( ) . subscribe (
118+ ( response : any ) => {
119+ console . log ( 'getUserFromSession response ==>' , response )
120+ this . userFromSession = response ;
121+ this . storageService . setItem ( AppSettings . USER_ID , this . userFromSession . user_id ) ;
122+ this . storageService . setItem ( AppSettings . USER , {
123+ dateCreated : this . userFromSession . date_created ,
124+ dateModified : this . userFromSession . date_modified ,
125+ emails : this . userFromSession . user_emails ,
126+ lfEmail : this . userFromSession . lf_email ,
127+ lfUsername : this . userFromSession . lf_username ,
128+ userID : this . userFromSession . user_id ,
129+ username : this . userFromSession . user_name ,
130+ version : this . userFromSession . version
131+ } ) ;
132+ this . successEmitter . emit ( 'User' ) ;
133+ } ,
134+ ( exception ) => {
135+ console . log ( 'getUserFromSession exception ==>' , exception )
136+ this . errorEmitter . emit ( true ) ;
137+ this . alertService . error ( 'An error occurred while retrieving the GitHub user from the session.' ) ;
138+ this . claContributorService . handleError ( exception ) ;
139+ }
140+ ) ;
141+ }
142+
143+
104144
105145
106146}
0 commit comments