@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
119 lines • 7.13 kB
JavaScript
import { msPerDay } from '../../../logic/Time/constants';
import { savePermissionAnalyticsAPI } from './savePermissionAnalyticsAPI';
import { getAnalyticsAPI } from '../Analytics/getAnalyticsAPI';
/**
* 2025-01-03: Migrated from fps-library-v2/lib/pnpjs/Permissions, pnp2/lib/services/sp/perms
* combination of v2: savePermissionHistoryD, savePermissionAnalyticsD fetchAnalyticsDD
*
* import { savePermissionHistory } from "@mikezimm/fps-core-v7/lib/restAPIs/logging/Permissions/savePermissionHistory";
*
*
* 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(fpsSpService, analyticsWeb, analyticsList, SiteLink, webTitle, saveTitle, TargetSite, TargetList, itemInfo1, itemInfo2, result, RichTextJSON1, Setting, RichTextJSON2, RichTextJSON3, userName, BaseTrace) {
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.
*/
/**
* REFACTOR NOTES: IN place of this, review fetchAnalytics from EasyAnalytics, BUT just update the restFilter and top number
* NOTE: restFilter should be based on the pickedWebguid and theListId
*/
//2022-08-29: Refactored return statement to fix compile warning.
const analyticsResults = await getAnalyticsAPI(fpsSpService, analyticsWeb, analyticsList, pickedWebguid, theListId, true, fetchColumns, 200, BaseTrace);
let items = analyticsResults.items;
let lastIsSame = false;
// console.log('RichTextJSON3', RichTextJSON3 );
RichTextJSON3 = JSON.stringify(RichTextJSON3);
//console.log('RichTextJSON3 length = ', RichTextJSON3.length, RichTextJSON3 );
let lastTimeCurrentUserSaved = null;
let saveThisSnapshot = 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 = 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 = 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);
savePermissionAnalyticsAPI(fpsSpService, 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
}
return `Success`;
}
else {
//2022-08-29: Refactored return statement to fix compile warning.
const errMessage = `Unable to fetch permission analytics....`;
console.log(`restAPIs/Logging/Permissions: ${errMessage}`);
return errMessage;
}
}
//# sourceMappingURL=savePermissionHistory.js.map