UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

202 lines 8.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SkipFields = exports.__fixGetListItemsResults = void 0; const collections_base_1 = require("../../../helpers/collections.base"); const typecheckers_1 = require("../../../helpers/typecheckers"); const rest_types_1 = require("../../../types/rest.types"); const sharepoint_types_1 = require("../../../types/sharepoint.types"); const sharepoint_utils_types_1 = require("../../../types/sharepoint.utils.types"); const consolelogger_1 = require("../../consolelogger"); const common_1 = require("../common"); const GetListItemsById_1 = require("./GetListItemsById"); const logger = consolelogger_1.ConsoleLogger.get("sharepoint.rest/list/common"); function __fixGetListItemsResults(siteUrl, listIdOrTitle, items, foldersBehaviour, expandedLookupFields) { let folders = {}; let itemFileRefMap = {}; if ((0, typecheckers_1.isNotEmptyArray)(items) && (0, typecheckers_1.isNullOrUndefined)(items[0].FileRef)) { //customer support ticket - request was missing teh FileRef column! let additionalItems = (0, GetListItemsById_1.GetItemsByIdSync)(siteUrl, listIdOrTitle, items.map(i => i.Id), { select: ["Id", "FileRef", "FileSystemObjectType"], jsonMetadata: rest_types_1.jsonTypes.nometadata }); itemFileRefMap = (0, collections_base_1.toHash)(additionalItems, i => i.Id.toString(10)); } items.forEach(item => { try { if (itemFileRefMap[item.Id]) { let mappedValue = itemFileRefMap[item.Id]; item.FileRef = mappedValue.FileRef; item.FileSystemObjectType = mappedValue.FileSystemObjectType; } let fileRef = item.FileRef || ""; let parts = fileRef.split('/'); item.FileLeafRef = parts.pop() || ""; item.FileDirRef = parts.join('/'); item.FileOrFolderName = item.FileLeafRef.split('.')[0]; if (item.FileSystemObjectType === 1) { item.FileType = "folder"; item.__Items = []; folders[fileRef.toLowerCase()] = item; item.__DisplayTitle = item.FileLeafRef; } else { //issue 8094 file name might have multiple dots let splitDot = item.FileLeafRef.split('.'); item.FileType = splitDot.length > 1 ? splitDot.pop().toLowerCase() : "folder"; item.__DisplayTitle = splitDot.join('.'); if (item.FileType === '000') { item.FileType = 'listitem'; item.__DisplayTitle = item.Title; if ((0, typecheckers_1.isNullOrEmptyString)(item.__DisplayTitle)) item.__DisplayTitle = `Item #${item.Id}`; } } if ((0, typecheckers_1.isNotEmptyArray)(expandedLookupFields)) expandedLookupFields.forEach((f) => { //ISSUE: 1519 let lookupField = f.LookupField; //ISSUE: 1250 - expanded lookup fields with names that start with '_' //will be returned as OData__name. We have to use the field name returned //from GetFieldNameFromRawValues without the 'Id' appended to the name because //the value has been expanded. let fieldInternalName = f.InternalName; let rawValue = item[f.InternalName]; if ((0, typecheckers_1.isNullOrUndefined)(rawValue)) { fieldInternalName = (0, common_1.GetFieldNameFromRawValues)(f, { excludeIdFromName: true }); rawValue = item[fieldInternalName]; } if (rawValue) { item.FieldValuesAsText = item.FieldValuesAsText || {}; let value = rawValue; if (Array.isArray(value)) //multi value { let id = []; let text = []; value.forEach(v => { id.push(v.Id); if (!(0, typecheckers_1.isNullOrEmptyString)(lookupField) && !(0, typecheckers_1.isNullOrUndefined)(v[lookupField])) { text.push(v[lookupField]); } else { text.push(v.Title); } }); item[`${fieldInternalName}Id`] = id; item.FieldValuesAsText[(0, common_1.DecodeFieldValuesAsTextKey)(fieldInternalName)] = text.join(", "); } else if ((0, typecheckers_1.isNumber)(value && value.Id)) { item[`${fieldInternalName}Id`] = value.Id; //ISSUE: 1519 - condition to check if lookup field exists and get the value from the rawValue object by string index of lookup field key if (!(0, typecheckers_1.isNullOrEmptyString)(lookupField) && !(0, typecheckers_1.isNullOrUndefined)(value[lookupField])) { item.FieldValuesAsText[(0, common_1.DecodeFieldValuesAsTextKey)(fieldInternalName)] = value[lookupField]; } else { item.FieldValuesAsText[(0, common_1.DecodeFieldValuesAsTextKey)(fieldInternalName)] = value.Title; } } } }); } catch (e) { logger.error(`Failed to fix list item result ${item && item.Id || 'unknown id'}`); } }); //loop through items, put them inside folders let itemsInRoot = []; items.forEach(item => { let parentFolder = folders[item.FileDirRef.toLowerCase()]; if (!(0, typecheckers_1.isNullOrUndefined)(parentFolder)) { if (item.FileSystemObjectType === sharepoint_types_1.FileSystemObjectTypes.Folder) { //add it before any items, so we have folders first (sorted) and items after let indexOfLastFolder = (0, collections_base_1.lastIndexOf)(parentFolder.__Items, i => i.FileSystemObjectType === sharepoint_types_1.FileSystemObjectTypes.Folder); parentFolder.__Items.splice(indexOfLastFolder + 1, 0, item); } else parentFolder.__Items.push(item); item.__ParentFolder = parentFolder; } else itemsInRoot.push(item); }); switch (foldersBehaviour) { case sharepoint_utils_types_1.GeListItemsFoldersBehaviour.AllItemsNoFolders: return items.filter(r => r.FileSystemObjectType !== 1); case sharepoint_utils_types_1.GeListItemsFoldersBehaviour.ItemsInsideFolders: return itemsInRoot; case sharepoint_utils_types_1.GeListItemsFoldersBehaviour.ItemsAndFoldersFlat: default: return items; } } exports.__fixGetListItemsResults = __fixGetListItemsResults; exports.SkipFields = [ "appauthor", "appeditor", "linktitle", "linktitlenomenu", "linkfilename", "linkfilenamenomenu", "_copysource", "_complianceflags", "_compliancetag", "_compliancetagwrittentime", "_compliancetaguserid", "_isrecord", //issue 5576: allow user to see version column "_uiversionstring", "itemchildcount", "folderchildcount", "complianceassetid", "xd_progid", "xd_signature", "_shortcuturl", "_shortcutsiteid", "_shortcutwebid", "_shortcutuniqueid", "_hascopydestinations", "sortbehavior", "permmask", "syncclientid", "progid", "scopeid", "virusstatus", "_editmenutablestart", "_editmenutablestart2", "_editmenutableend", "linkfilename2", "basename", "metainfo", "_level", "_iscurrentversion", "originatorid", "noexecute", "bsn", "_listschemaversion", "_dirty", "_parsable", "_stubfile", "_virusstatus", "_virusvendorid", "_virusinfo", "_rmstemplateid", "_iplabelid", "_displayname", "smtotalsize", "smlastmodifieddate", "smtotalfilestreamsize", "smtotalfilecount", "selecttitle", "selectfilename", "edit", "workflowversion", "workflowinstanceid", "parentversionstring", "parentleafname", "docconcurrencynumber", "parentuniqueid", "streamhash", "combine", "repairdocument" ]; //# sourceMappingURL=common.js.map