import { ISPHttpClientOptions , SPHttpClient , SPHttpClientResponse } from '@microsoft/sp-http' ; import { Environment , EnvironmentType } from '@microsoft/sp-core-library' ; import { PrincipalType , IPropertyFieldGroupOrPerson } from './../propertyFields/peoplePicker/IPropertyFieldPeoplePicker' ; import { ISPPeopleSearchService } from './ISPPeopleSearchService' ; import SPPeoplePickerMockHttpClient from './SPPeopleSearchMockService' ; import { BaseComponentContext } from '@microsoft/sp-component-base' ; /**
 * Service implementation to search people in SharePoint
 */ export default class SPPeopleSearchService implements ISPPeopleSearchService { /**
     * Search people from the SharePoint People database
     */ public searchPeople ( ctx , query , principalType , siteUrl = null ) { if ( Environment . type === EnvironmentType . Local ) { return this . searchPeopleFromMock ( ctx , query ) ; } else { const userRequestUrl = `${ siteUrl ? siteUrl : ctx . pageContext . web . absoluteUrl }/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser` ; const data = { 'queryParams' : { 'AllowEmailAddresses' : true , 'AllowMultipleEntities' : false , 'AllUrlZones' : false , 'MaximumEntitySuggestions' : 20 , 'PrincipalSource' : 15 , 'PrincipalType' : ! ! principalType && principalType . length > 0 ? principalType . reduce ( ( a , b ) => a + b , 0 ) : 1 , 'QueryString' : query } } ; let httpPostOptions = { headers : { 'accept' : 'application/json' , 'content-type' : 'application/json' } , body : JSON . stringify ( data ) } ; return ctx . spHttpClient . post ( userRequestUrl , SPHttpClient . configurations . v1 , httpPostOptions ) . then ( ( searchResponse ) => { return searchResponse . json ( ) . then ( ( usersResponse ) => { let res = [ ] ; let values = JSON . parse ( usersResponse . value ) ; values = values . filter ( v => ! ( v . EntityData && v . EntityData . PrincipalType && v . EntityData . PrincipalType === "UNVALIDATED_EMAIL_ADDRESS" ) ) ; values = values . filter ( v => v . Key !== null ) ; res = values . map ( element => { switch ( element . EntityType ) { case 'User' : let email = element . EntityData . Email !== null ? element . EntityData . Email : element . Description ; const groupOrPerson = { fullName : element . DisplayText , login : element . Description } ; groupOrPerson . id = element . Key ; groupOrPerson . email = email ; groupOrPerson . jobTitle = element . EntityData . Title ; groupOrPerson . initials = this . getFullNameInitials ( groupOrPerson . fullName ) ; groupOrPerson . imageUrl = this . getUserPhotoUrl ( groupOrPerson . email , siteUrl ? siteUrl : ctx . pageContext . web . absoluteUrl ) ; return groupOrPerson ; case 'SecGroup' : const group = { fullName : element . DisplayText , login : element . ProviderName , id : element . Key , description : element . Description , } ; return group ; case 'FormsRole' : const formsRole = { fullName : element . DisplayText , login : element . ProviderName , id : element . Key , description : element . Description } ; return formsRole ; default : const persona = { fullName : element . DisplayText , login : element . EntityData . AccountName , id : element . EntityData . SPGroupID , description : element . Description } ; return persona ; } } ) ; return res ; } ) ; } ) ; } } /**
     * Generates Initials from a full name
     */ private getFullNameInitials ( fullName ) { if ( fullName === null ) { return fullName ; } const words = fullName . split ( ' ' ) ; if ( words . length === 0 ) { return '' ; } else if ( words . length === 1 ) { return words [ 0 ] . charAt ( 0 ) ; } else { return ( words [ 0 ] . charAt ( 0 ) + words [ 1 ] . charAt ( 0 ) ) ; } } /**
     * Gets the user photo url
     */ private getUserPhotoUrl ( userEmail , siteUrl ) { if ( userEmail ) { return `${ siteUrl }/_layouts/15/userphoto.aspx?size=S&accountname=${ encodeURIComponent ( userEmail ) }` ; } return null ; } /**
     * Returns fake people results for the Mock mode
     */ private searchPeopleFromMock ( ctx , query ) { return SPPeoplePickerMockHttpClient . searchPeople ( ctx . pageContext . web . absoluteUrl ) . then ( ( ) => { const results = [ { fullName : 'Katie Jordan' , initials : 'KJ' , jobTitle : 'VIP Marketing' , email : 'katiej@contoso.com' , login : 'katiej@contoso.com' } , { fullName : 'Gareth Fort' , initials : 'GF' , jobTitle : 'Sales Lead' , email : 'garethf@contoso.com' , login : 'garethf@contoso.com' } , { fullName : 'Sara Davis' , initials : 'SD' , jobTitle : 'Assistant' , email : 'sarad@contoso.com' , login : 'sarad@contoso.com' } , { fullName : 'John Doe' , initials : 'JD' , jobTitle : 'Developer' , email : 'johnd@contoso.com' , login : 'johnd@contoso.com' } ] ; return results ; } ) as Promise < Array < IPropertyFieldGroupOrPerson > > ; } }