UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

222 lines 9.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SPPeopleSearchService = void 0; const collections_base_1 = require("../../helpers/collections.base"); const rest_types_1 = require("../../types/rest.types"); const sharepoint_types_1 = require("../../types/sharepoint.types"); const rest_1 = require("../rest"); const user_1 = require("./user"); /** * Service implementation to search people in SharePoint */ class SPPeopleSearchService { /** * Service constructor */ constructor(context) { this.context = context; this.cachedLocalUsers = {}; this.cachedLocalUsers[this.context.siteUrl] = []; } /** * Generate the user photo link using SharePoint user photo endpoint. * * @param value */ generateUserPhotoLink(value, size = "M") { return `${this.context.siteUrl}/_layouts/15/userphoto.aspx?accountname=${encodeURIComponent(value)}&size=M`; } /** * Generate sum of principal types * * PrincipalType controls the type of entities that are returned in the results. * Choices are All - 15, Distribution List - 2 , Security Groups - 4, SharePoint Groups - 8, User - 1. * These values can be combined (example: 13 is security + SP groups + users) * * @param principalTypes */ getSumOfPrincipalTypes(principalTypes) { return !!principalTypes && principalTypes.length > 0 ? principalTypes.reduce((a, b) => a + b, 0) : 1; } /** * Retrieve the specified group * * @param groupName * @param siteUrl */ async getGroupId(groupName, siteUrl = null) { // if (Environment.type === EnvironmentType.Local) { // return 1; // } else { const groups = await this.searchTenant(siteUrl, groupName, 1, [sharepoint_types_1.PrincipalType.SharePointGroup], false, 0); return (groups && groups.length > 0) ? parseInt(groups[0].id) : null; //} } /** * Search person by its email or login name */ async searchPersonByEmailOrLogin(email, principalTypes, siteUrl = null, groupId = null, ensureUser = false) { // if (Environment.type === EnvironmentType.Local) { // // If the running environment is local, load the data from the mock // const mockUsers = await this.searchPeopleFromMock(email); // return (mockUsers && mockUsers.length > 0) ? mockUsers[0] : null; // } else { const userResults = await this.searchTenant(siteUrl, email, 1, principalTypes, ensureUser, groupId); return (userResults && userResults.length > 0) ? userResults[0] : null; //} } /** * Search All Users from the SharePoint People database */ async searchPeople(query, maximumSuggestions, principalTypes, siteUrl = null, groupId = null, ensureUser = false) { // if (Environment.type === EnvironmentType.Local) { // // If the running environment is local, load the data from the mock // return this.searchPeopleFromMock(query); // } else { return await this.searchTenant(siteUrl, query, maximumSuggestions, principalTypes, ensureUser, groupId); //} } /** * Tenant search */ async searchTenant(siteUrl, query, maximumSuggestions, principalTypes, ensureUser, groupId) { try { // If the running env is SharePoint, loads from the peoplepicker web service const userRequestUrl = `${siteUrl || this.context.siteUrl}/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser`; const searchBody = { queryParams: { AllowEmailAddresses: true, AllowMultipleEntities: false, AllUrlZones: false, MaximumEntitySuggestions: maximumSuggestions, PrincipalSource: 15, PrincipalType: this.getSumOfPrincipalTypes(principalTypes), QueryString: query } }; // Search on the local site when "0" if (siteUrl) { searchBody.queryParams["SharePointGroupID"] = 0; } // Check if users need to be searched in a specific group if (groupId) { searchBody.queryParams["SharePointGroupID"] = groupId; } // Do the call against the People REST API endpoint const userDataResp = await (0, rest_1.GetJson)(userRequestUrl, JSON.stringify(searchBody), { headers: { Accept: rest_types_1.jsonTypes.standard, "content-type": rest_types_1.contentTypes.json } }); if (userDataResp && userDataResp.value && userDataResp.value.length > 0) { let values = userDataResp.value; if (typeof userDataResp.value === "string") { values = JSON.parse(userDataResp.value); } // Filter out "UNVALIDATED_EMAIL_ADDRESS" values = values.filter(v => !(v.EntityData && v.EntityData.PrincipalType && v.EntityData.PrincipalType === "UNVALIDATED_EMAIL_ADDRESS")); // Check if local user IDs need to be retrieved if (ensureUser) { for (const value of values) { // Only ensure the user if it is not a SharePoint group if (!value.EntityData || (value.EntityData && typeof value.EntityData.SPGroupID === "undefined")) { const id = await this.ensureUser(value.Key); value.LoginName = value.Key; value.Key = id; } } } // Filter out NULL keys values = values.filter(v => v.Key !== null); const userResults = values.map(element => { switch (element.EntityType) { case 'User': return { id: element.Key, loginName: element.LoginName ? element.LoginName : element.Key, imageUrl: this.generateUserPhotoLink(element.Description || ""), imageInitials: this.getFullNameInitials(element.DisplayText), text: element.DisplayText, // name secondaryText: element.EntityData.Email || element.Description, // email tertiaryText: "", // status optionalText: "" // anything }; case 'SecGroup': return { id: element.Key, loginName: element.LoginName ? element.LoginName : element.Key, imageInitials: this.getFullNameInitials(element.DisplayText), text: element.DisplayText, secondaryText: element.ProviderName }; case 'FormsRole': return { id: element.Key, loginName: element.LoginName ? element.LoginName : element.Key, imageInitials: this.getFullNameInitials(element.DisplayText), text: element.DisplayText, secondaryText: element.ProviderName }; default: return { id: element.EntityData.SPGroupID, loginName: element.EntityData.AccountName, imageInitials: this.getFullNameInitials(element.DisplayText), text: element.DisplayText, secondaryText: element.EntityData.AccountName }; } }); return userResults; } // Nothing to return return []; } catch (e) { console.error("PeopleSearchService::searchTenant: error occured while fetching the users."); return []; } } /** * Retrieves the local user ID * * @param userId */ async ensureUser(userId) { const siteUrl = this.context.siteUrl; if (this.cachedLocalUsers && this.cachedLocalUsers[siteUrl]) { const users = this.cachedLocalUsers[siteUrl]; const userIdx = (0, collections_base_1.firstIndexOf)(users, u => u.LoginName === userId); if (userIdx !== -1) { return users[userIdx].Id; } } const user = await (0, user_1.EnsureUser)(siteUrl, userId); if (user && user.Id) { this.cachedLocalUsers[siteUrl].push(user); return user.Id; } return null; } /** * Generates Initials from a full name */ 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)); } } } exports.SPPeopleSearchService = SPPeopleSearchService; //# sourceMappingURL=user-search.js.map