/**
 * -------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the MIT License.
 * See License in the project root for license information.
 * -------------------------------------------------------------------------------------------
 */ import { CacheService , IGraph , prepScopes } from '@microsoft/mgt-element' ; import { CachePhoto , getIsPhotosCacheEnabled , getPhotoForResource , getPhotoFromCache , getPhotoInvalidationTime , storePhotoInCache } from './graph.photos' ; import { CacheUser , getIsUsersCacheEnabled , getUserInvalidationTime } from './graph.user' ; import { IDynamicPerson } from './types' ; import { schemas } from './cacheStores' ; param { string } userId returns { ( Promise < IDynamicPerson > ) } memberof export async function getUserWithPhoto ( graph , userId ? , requestedProps ? ) { let photo = null ; let user = null ; let cachedPhoto ; let cachedUser ; const resource = userId ? `users/${ userId }` : 'me' ; let fullResource = resource + ( requestedProps ? `?$select=${ requestedProps . toString ( ) }` : '' ) ; const scopes = userId ? [ 'user.readbasic.all' ] : [ 'user.read' ] ; if ( getIsUsersCacheEnabled ( ) ) { const cache = CacheService . getCache < CacheUser > ( schemas . users , schemas . users . stores . users ) ; cachedUser = await cache . getValue ( userId || 'me' ) ; if ( cachedUser && getUserInvalidationTime ( ) > Date . now ( ) - cachedUser . timeCached ) { user = cachedUser . user ? JSON . parse ( cachedUser . user ) : null ; if ( requestedProps ) { const uniqueProps = requestedProps . filter ( prop => ! Object . keys ( user ) . includes ( prop ) ) ; if ( uniqueProps . length >= 1 ) { user = null ; cachedUser = null ; } } } else { cachedUser = null ; } } if ( getIsPhotosCacheEnabled ( ) ) { cachedPhoto = await getPhotoFromCache ( userId || 'me' , schemas . photos . stores . users ) ; if ( cachedPhoto && getPhotoInvalidationTime ( ) > Date . now ( ) - cachedPhoto . timeCached ) { photo = cachedPhoto . photo ; } else if ( cachedPhoto ) { try { const response = await graph . api ( `${ resource }/photo` ) . get ( ) ; if ( response && response [ '@odata.mediaEtag' ] && response [ '@odata.mediaEtag' ] === cachedPhoto . eTag ) { storePhotoInCache ( userId || 'me' , schemas . photos . stores . users , cachedPhoto ) ; photo = cachedPhoto . photo ; } else { cachedPhoto = null ; } } catch ( e ) { if ( e . code === 'ErrorItemNotFound' ) { storePhotoInCache ( userId || 'me' , schemas . photos . stores . users , { eTag : null , photo : null } ) ; } } } } if ( ! cachedPhoto && ! cachedUser ) { let eTag ; const batch = graph . createBatch ( ) ; if ( userId ) { batch . get ( 'user' , `/users/${ userId }${ requestedProps ? '?$select=' + requestedProps . toString ( ) : '' }` , [ 'user.readbasic.all' ] ) ; batch . get ( 'photo' , `users/${ userId }/photo/$value` , [ 'user.readbasic.all' ] ) ; } else { batch . get ( 'user' , 'me' , [ 'user.read' ] ) ; batch . get ( 'photo' , 'me/photo/$value' , [ 'user.read' ] ) ; } const response = await batch . executeAll ( ) ; const photoResponse = response . get ( 'photo' ) ; if ( photoResponse ) { eTag = photoResponse . headers [ 'ETag' ] ; photo = photoResponse . content ; } const userResponse = response . get ( 'user' ) ; if ( userResponse ) { user = userResponse . content ; } if ( getIsUsersCacheEnabled ( ) ) { const cache = CacheService . getCache < CacheUser > ( schemas . users , schemas . users . stores . users ) ; cache . putValue ( userId || 'me' , { user : JSON . stringify ( user ) } ) ; } if ( getIsPhotosCacheEnabled ( ) ) { storePhotoInCache ( userId || 'me' , schemas . photos . stores . users , { eTag , photo : photo } ) ; } } else if ( ! cachedPhoto ) { const response = await getPhotoForResource ( graph , resource , scopes ) ; if ( response ) { if ( getIsPhotosCacheEnabled ( ) ) { storePhotoInCache ( userId || 'me' , schemas . photos . stores . users , { eTag : response . eTag , photo : response . photo } ) ; } photo = response . photo ; } } else if ( ! cachedUser ) { const response = await graph . api ( fullResource ) . middlewareOptions ( prepScopes ( ... scopes ) ) . get ( ) ; if ( response ) { if ( getIsUsersCacheEnabled ( ) ) { const cache = CacheService . getCache < CacheUser > ( schemas . users , schemas . users . stores . users ) ; cache . putValue ( userId || 'me' , { user : JSON . stringify ( response ) } ) ; } user = response ; } } if ( user ) { user . personImage = photo ; } return user ; }