@@ -190,6 +190,71 @@ func (a *Affiliation) GetProfile(uuid, projectSlug string) *ProfileResponse {
190190 return & response
191191}
192192
193+ // GetIdentityByUser ...
194+ func (a * Affiliation ) GetIdentityByUser (key string , value string ) (* AffIdentity , error ) {
195+ if key == "" || value == "" {
196+ nilKeyOrValueErr := "repository: GetIdentityByUser: key or value is null"
197+ log .Println (nilKeyOrValueErr )
198+ return nil , fmt .Errorf (nilKeyOrValueErr )
199+ }
200+ token , err := a .auth0Client .ValidateToken (a .Environment )
201+ if err != nil {
202+ log .Println (err )
203+ return nil , err
204+ }
205+
206+ headers := make (map [string ]string , 0 )
207+ headers ["Authorization" ] = fmt .Sprintf ("%s %s" , "Bearer" , token )
208+ endpoint := a .AffBaseURL + "/affiliation/" + "identity/" + key + "/" + value
209+ _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
210+ if err != nil {
211+ log .Println ("Repository: GetIdentityByUser: Could not get the identity: " , err )
212+ return nil , err
213+ }
214+
215+ var ident IdentityData
216+ err = json .Unmarshal (res , & ident )
217+ if err != nil {
218+ return nil , err
219+ }
220+
221+ profileEndpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (a .ProjectSlug ) + "/get_profile/" + * ident .UUID
222+ _ , profileRes , err := a .httpClient .Request (strings .TrimSpace (profileEndpoint ), "GET" , headers , nil , nil )
223+ if err != nil {
224+ log .Println ("Repository: GetIdentityByUser: Could not get the identity: " , err )
225+ return nil , err
226+ }
227+
228+ var profile UniqueIdentityFullProfile
229+ err = json .Unmarshal (profileRes , & profile )
230+ if err != nil {
231+ return nil , err
232+ }
233+ var identity AffIdentity
234+ identity .UUID = ident .UUID
235+ identity .Name = * ident .Name
236+ identity .Username = * ident .Username
237+ identity .Email = * ident .Email
238+ identity .ID = & ident .ID
239+
240+ identity .IsBot = profile .Profile .IsBot
241+ identity .Gender = profile .Profile .Gender
242+ identity .GenderACC = profile .Profile .GenderAcc
243+
244+ if len (profile .Enrollments ) > 1 {
245+ identity .OrgName = & profile .Enrollments [0 ].Organization .Name
246+ for _ , org := range profile .Enrollments {
247+ identity .MultiOrgNames = append (identity .MultiOrgNames , org .Organization .Name )
248+ }
249+ } else if len (profile .Enrollments ) == 1 {
250+ identity .OrgName = & profile .Enrollments [0 ].Organization .Name
251+ identity .MultiOrgNames = append (identity .MultiOrgNames , profile .Enrollments [0 ].Organization .Name )
252+ }
253+
254+ return & identity , nil
255+
256+ }
257+
193258func buildServices (a * Affiliation ) (httpClientProvider * http.ClientProvider , esClientProvider * elastic.ClientProvider , auth0ClientProvider * auth0.ClientProvider , err error ) {
194259 esClientProvider , err = elastic .NewClientProvider (& elastic.Params {
195260 URL : a .ESCacheURL ,
0 commit comments