@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
139 lines (107 loc) • 6.62 kB
text/typescript
import { Web, Items, } from '@pnp/sp/presets/all';
import { getHelpfullErrorV2, saveThisLogItem } from '../Logging/ErrorHandler';
import { IRailAnalytics } from '../Arrays/grouping';
import { getFullUrlFromSlashSitesUrl } from '../Strings/urlServices';
import { fetchAnalytics } from './fetch';
import { saveAnalytics } from './normal';
import { msPerDay } from '../Time/constants';
import * as AL from './constants';
/**
* This function is for automatically saving permissions from a web, list or library to list for later comparison.
* In Easy Contents, it's fired upon viewing rail function to view list permissions.
* It's also intended to be used in Pivot Tiles when clicking to view list and web permissions.
*
* It does require the list and web with the correct struture to save and then be recoverd in this webpart for comparison.
*
* So it's only going to execute in certain tenanats.
* If you see this and want to re-purpose it, update the function to suit your needs and adjust the window.location.origin check
*
* Best practice is just to update your site and list Url in strings:
* Or just create the site: SharePointAssist
* And create the list: Assists
* And add the columns listed below in the save item
"analyticsListPermissionsHistory": "PermissionsHistory",
*
*/
export async function savePermissionHistory ( analyticsWeb: string, analyticsList: string, SiteLink: any, webTitle: string, saveTitle: string, TargetSite: any, TargetList: any, itemInfo1: string, itemInfo2: string, result: string, RichTextJSON1: any, Setting: string, RichTextJSON2: any, RichTextJSON3: any, userName: string, BaseTrace: string) {
let prefetchStart = new Date();
let pickedWebguid = TargetSite.split('|')[1];
let theListId = TargetList.split('|')[1];
let fetchColumns = [ 'Created','Modified','Author/Name','Author/Id','Author/Title','Id',
'Title','zzzRichText3', 'zzzText3', 'Result', 'WebID','SiteID','CollectionUrl', 'ListID'
];
/**
* In this case I chose to fetch the last 200 items to compare and see if the current user had any history.
* If not, it will save the permissions even if it has not changed since another user checked.
* Choose 200 to make sure that it should almost always be enough to check the last day's worth of items.
*/
//2022-08-29: Refactored return statement to fix compile warning.
let items: IRailAnalytics[] | undefined = await fetchAnalytics( analyticsWeb, analyticsList , pickedWebguid, theListId, true, fetchColumns, 200, BaseTrace );
let lastIsSame: any = false;
// console.log('RichTextJSON3', RichTextJSON3 );
RichTextJSON3 = JSON.stringify(RichTextJSON3);
//console.log('RichTextJSON3 length = ', RichTextJSON3.length, RichTextJSON3 );
let lastTimeCurrentUserSaved: any = null;
let saveThisSnapshot: any = false;
let checkedOtherUsers = false;
let foundMyItem = false;
//2022-08-29: Refactored return statement to fix compile warning.
//Wrapped items.map in if ( items ) check to make sure items has a value.
if ( items ) {
items.map( ( item, index) => {
if ( saveThisSnapshot === false && foundMyItem === false ) {
let itemFromCurrentUser = false;
let itemAny: any = item;
if ( itemAny.Author.Name.indexOf( userName ) > -1 ) {
// console.log('You saved this item:', item );
lastTimeCurrentUserSaved = new Date(item.Created);
itemFromCurrentUser = true;
foundMyItem = true;
}
let userDeltaTime: any = itemFromCurrentUser === false ? null : prefetchStart.getTime() - lastTimeCurrentUserSaved.getTime() ;
if ( itemFromCurrentUser === true && userDeltaTime > msPerDay ) { //one day = 24*60*60*1000
saveThisSnapshot = true; //Save this item if the current user has not saved permissions in last 24 hours
} else if ( checkedOtherUsers === false && lastIsSame === false ) { //this check happens if
//This section checks if the current item has the same permissions settings as the current check
let zzzRichText3 = item.zzzRichText3.replace(/\\\"/g,'"');
zzzRichText3 = zzzRichText3.slice( 1, -1 );//Have to remove the leading and trailing ""
// console.log('zzzRichText3 length = ', zzzRichText3.length, zzzRichText3 );
let thisHasUniqueRoleAssignments = itemInfo2.split('|')[0];
console.log('compareHasUnique ~ perm.ts 83', item.zzzText3, thisHasUniqueRoleAssignments );
if ( item.zzzText3 === thisHasUniqueRoleAssignments
&& zzzRichText3.length === RichTextJSON3.length
&& zzzRichText3 === RichTextJSON3 ) {
lastIsSame = true;
}
checkedOtherUsers = true; //This is used so it only does this check one time.
}
}
});
//Final check for other conditions to save.
if ( saveThisSnapshot === true ){
//No need for further checks
} if ( foundMyItem === false ) { //automatically save since this user never saved permissions
saveThisSnapshot = true;
} else if ( lastIsSame === false ) { //Save if the last item is not the same as current permissions
saveThisSnapshot = true;
}
let prefetchEnd = new Date();
let timeToPreFetch = prefetchEnd.getTime() - prefetchStart.getTime();
itemInfo2 += '||Time to check old Permissions: ' + items.length + ' snaps' + ' / ' + timeToPreFetch + 'ms' ;
console.log('savePermissionHistory lastIsSame', lastIsSame );
if ( saveThisSnapshot === true ) {
RichTextJSON1 = JSON.stringify(RichTextJSON1);
RichTextJSON2 = JSON.stringify(RichTextJSON2);
saveAnalytics( analyticsWeb, analyticsList , //analyticsWeb, analyticsList,
SiteLink, webTitle,//serverRelativeUrl, webTitle,
saveTitle, TargetSite, TargetList, //saveTitle, TargetSite, TargetList
itemInfo1, itemInfo2, result, //itemInfo1, itemInfo2, result,
RichTextJSON1, Setting, RichTextJSON2, RichTextJSON3 ); //richText, Setting, richText2, richText3
}
} else {
//2022-08-29: Refactored return statement to fix compile warning.
const errMessage: string = `Unable to fetch permission analytics....`;
console.log(`npmFunctions/Analytics/Permissions: ${errMessage}`)
return errMessage;
}
}