|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import { BatchResponse, IBatch, IGraph } from '@microsoft/mgt-element'; |
| 9 | +import { Profile } from '@microsoft/microsoft-graph-types-beta'; |
| 10 | + |
| 11 | +import { getEmailFromGraphEntity } from '../../graph/graph.people'; |
| 12 | +import { IDynamicPerson } from '../../graph/types'; |
| 13 | +import { MgtPersonCardConfig, MgtPersonCardState } from './mgt-person-card.types'; |
| 14 | + |
| 15 | +// tslint:disable-next-line:completed-docs |
| 16 | +const userProperties = |
| 17 | + 'businessPhones,companyName,department,displayName,givenName,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,id'; |
| 18 | + |
| 19 | +// tslint:disable-next-line:completed-docs |
| 20 | +const batchKeys = { |
| 21 | + directReports: 'directReports', |
| 22 | + files: 'files', |
| 23 | + messages: 'messages', |
| 24 | + people: 'people', |
| 25 | + person: 'person' |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Get data to populate the person card |
| 30 | + * |
| 31 | + * @export |
| 32 | + * @param {IGraph} graph |
| 33 | + * @param {IDynamicPerson} personDetails |
| 34 | + * @param {boolean} isMe |
| 35 | + * @param {MgtPersonCardConfig} config |
| 36 | + * @return {*} {Promise<MgtPersonCardState>} |
| 37 | + */ |
| 38 | +export async function getPersonCardGraphData( |
| 39 | + graph: IGraph, |
| 40 | + personDetails: IDynamicPerson, |
| 41 | + isMe: boolean, |
| 42 | + config: MgtPersonCardConfig |
| 43 | +): Promise<MgtPersonCardState> { |
| 44 | + const userId = personDetails.id; |
| 45 | + const email = getEmailFromGraphEntity(personDetails); |
| 46 | + |
| 47 | + const isContactOrGroup = |
| 48 | + 'classification' in personDetails || |
| 49 | + ('personType' in personDetails && |
| 50 | + (personDetails.personType.subclass === 'PersonalContact' || personDetails.personType.class === 'Group')); |
| 51 | + |
| 52 | + const batch = graph.createBatch(); |
| 53 | + |
| 54 | + if (!isContactOrGroup) { |
| 55 | + if (config.sections.organization) { |
| 56 | + buildOrgStructureRequest(batch, userId); |
| 57 | + |
| 58 | + if (typeof config.sections.organization !== 'boolean' && config.sections.organization.showWorksWith) { |
| 59 | + buildWorksWithRequest(batch, userId); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (config.sections.mailMessages && email) { |
| 65 | + buildMessagesWithUserRequest(batch, email); |
| 66 | + } |
| 67 | + |
| 68 | + if (config.sections.files) { |
| 69 | + buildFilesRequest(batch, isMe ? null : email); |
| 70 | + } |
| 71 | + |
| 72 | + let response: Map<string, BatchResponse>; |
| 73 | + const data: MgtPersonCardState = {}; // TODO |
| 74 | + try { |
| 75 | + response = await batch.executeAll(); |
| 76 | + } catch { |
| 77 | + // nop |
| 78 | + } |
| 79 | + |
| 80 | + if (response) { |
| 81 | + for (const [key, value] of response) { |
| 82 | + data[key] = value.content.value || value.content; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!isContactOrGroup && config.sections.profile) { |
| 87 | + try { |
| 88 | + const profile = await getProfile(graph, userId); |
| 89 | + if (profile) { |
| 90 | + data.profile = profile; |
| 91 | + } |
| 92 | + } catch { |
| 93 | + // nop |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return data; |
| 98 | +} |
| 99 | + |
| 100 | +// tslint:disable-next-line:completed-docs |
| 101 | +function buildOrgStructureRequest(batch: IBatch, userId: string) { |
| 102 | + const expandManagers = `manager($levels=max;$select=${userProperties})`; |
| 103 | + |
| 104 | + batch.get( |
| 105 | + batchKeys.person, |
| 106 | + `users/${userId}?$expand=${expandManagers}&$select=${userProperties}&$count=true`, |
| 107 | + ['user.read.all'], |
| 108 | + { |
| 109 | + ConsistencyLevel: 'eventual' |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + batch.get(batchKeys.directReports, `users/${userId}/directReports?$select=${userProperties}`); |
| 114 | +} |
| 115 | + |
| 116 | +// tslint:disable-next-line:completed-docs |
| 117 | +function buildWorksWithRequest(batch: IBatch, userId: string) { |
| 118 | + batch.get(batchKeys.people, `users/${userId}/people?$filter=personType/class eq 'Person'`, ['People.Read.All']); |
| 119 | +} |
| 120 | + |
| 121 | +// tslint:disable-next-line:completed-docs |
| 122 | +function buildMessagesWithUserRequest(batch: IBatch, emailAddress: string) { |
| 123 | + batch.get(batchKeys.messages, `me/messages?$search="from:${emailAddress}"`, ['Mail.ReadBasic']); |
| 124 | +} |
| 125 | + |
| 126 | +// tslint:disable-next-line:completed-docs |
| 127 | +function buildFilesRequest(batch: IBatch, emailAddress?: string) { |
| 128 | + let request: string; |
| 129 | + |
| 130 | + if (emailAddress) { |
| 131 | + request = `me/insights/shared?$filter=lastshared/sharedby/address eq '${emailAddress}'`; |
| 132 | + } else { |
| 133 | + request = 'me/insights/used'; |
| 134 | + } |
| 135 | + |
| 136 | + batch.get(batchKeys.files, request, ['Sites.Read.All']); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Get the profile for a user |
| 141 | + * |
| 142 | + * @param {IGraph} graph |
| 143 | + * @param {string} userId |
| 144 | + * @return {*} {Promise<Profile>} |
| 145 | + */ |
| 146 | +async function getProfile(graph: IGraph, userId: string): Promise<Profile> { |
| 147 | + const profile = await graph |
| 148 | + .api(`/users/${userId}/profile`) |
| 149 | + .version('beta') |
| 150 | + .get(); |
| 151 | + return profile; |
| 152 | +} |
0 commit comments