/**
 * -------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the MIT License.
 * See License in the project root for license information.
 * -------------------------------------------------------------------------------------------
 */ import { BatchResponse , IBatch , IGraph } from '@microsoft/mgt-element' ; import { Profile } from '@microsoft/microsoft-graph-types-beta' ; import { getEmailFromGraphEntity } from '../../graph/graph.people' ; import { IDynamicPerson } from '../../graph/types' ; import { MgtPersonCardConfig , MgtPersonCardState } from './mgt-person-card.types' ; const userProperties = 'businessPhones,companyName,department,displayName,givenName,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,id' ; const batchKeys = { directReports : 'directReports' , files : 'files' , messages : 'messages' , people : 'people' , person : 'person' } ; export param { IGraph } graph param { IDynamicPerson } personDetails param { boolean } isMe param { MgtPersonCardConfig } config return { * } export async function getPersonCardGraphData ( graph , personDetails , isMe , config ) { const userId = personDetails . id ; const email = getEmailFromGraphEntity ( personDetails ) ; const isContactOrGroup = 'classification' in personDetails || ( 'personType' in personDetails && ( personDetails . personType . subclass === 'PersonalContact' || personDetails . personType . class === 'Group' ) ) ; const batch = graph . createBatch ( ) ; if ( ! isContactOrGroup ) { if ( config . sections . organization ) { buildOrgStructureRequest ( batch , userId ) ; if ( typeof config . sections . organization !== 'boolean' && config . sections . organization . showWorksWith ) { buildWorksWithRequest ( batch , userId ) ; } } } if ( config . sections . mailMessages && email ) { buildMessagesWithUserRequest ( batch , email ) ; } if ( config . sections . files ) { buildFilesRequest ( batch , isMe ? null : email ) ; } let response ; const data = { } ; try { response = await batch . executeAll ( ) ; } catch { } if ( response ) { for ( const [ key , value ] of response ) { data [ key ] = value . content . value || value . content ; } } if ( ! isContactOrGroup && config . sections . profile ) { try { const profile = await getProfile ( graph , userId ) ; if ( profile ) { data . profile = profile ; } } catch { } } return data ; } function buildOrgStructureRequest ( batch , userId ) { const expandManagers = `manager($levels=max;$select=${ userProperties })` ; batch . get ( batchKeys . person , `users/${ userId }?$expand=${ expandManagers }&$select=${ userProperties }&$count=true` , [ 'user.read.all' ] , { ConsistencyLevel : 'eventual' } ) ; batch . get ( batchKeys . directReports , `users/${ userId }/directReports?$select=${ userProperties }` ) ; } function buildWorksWithRequest ( batch , userId ) { batch . get ( batchKeys . people , `users/${ userId }/people?$filter=personType/class eq 'Person'` , [ 'People.Read.All' ] ) ; } function buildMessagesWithUserRequest ( batch , emailAddress ) { batch . get ( batchKeys . messages , `me/messages?$search="from:${ emailAddress }"` , [ 'Mail.ReadBasic' ] ) ; } function buildFilesRequest ( batch , emailAddress ? ) { let request ; if ( emailAddress ) { request = `me/insights/shared?$filter=lastshared/sharedby/address eq '${ emailAddress }'` ; } else { request = 'me/insights/used' ; } batch . get ( batchKeys . files , request , [ 'Sites.Read.All' ] ) ; } param { IGraph } graph param { string } userId return { * } async function getProfile ( graph , userId ) { const profile = await graph . api ( `/users/${ userId }/profile` ) . version ( 'beta' ) . get ( ) ; return profile ; }