@fdot/arculus-staff-service
Version:
Simplifies usage of the Arculus Staff Service
207 lines (206 loc) • 7.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearCache = exports.setManager = exports.getManager = exports.setDirectReports = exports.getDirectReports = exports.setAllStaff = exports.getAllStaff = exports.setByFilterCriteria = exports.getByFilterCriteria = exports.isCachingEnabled = exports.initialize = void 0;
var StatusFilter_1 = require("../models/enums/StatusFilter");
var timed_cache_1 = __importDefault(require("timed-cache"));
var _cacheOptions = {
cachingEnabled: false,
cacheDurationInMinutes: 0,
slidingExpirationEnabled: false,
};
// Caching Setup
var cache;
var allStaffKey = "ALL_STAFF_KEY";
var directReportsKeyPrefix = "DIRECT_REPORTS";
var managerKeyPrefix = "MANAGER";
var initialize = function (options) {
if (options) {
_cacheOptions = options;
var defaultTtl = _cacheOptions.cacheDurationInMinutes * 60 * 1000;
cache = new timed_cache_1.default({ defaultTtl: defaultTtl });
}
else {
cache = new timed_cache_1.default();
}
};
exports.initialize = initialize;
var isCachingEnabled = function () {
return _cacheOptions.cachingEnabled;
};
exports.isCachingEnabled = isCachingEnabled;
var getByFilterCriteria = function (filterCriteria, cacheOptions) {
var key = getCacheKeyForFilterCriteria(filterCriteria);
return getFromCache(key, cacheOptions);
};
exports.getByFilterCriteria = getByFilterCriteria;
var setByFilterCriteria = function (filterCriteria, staffMembers, cacheOptions) {
var key = getCacheKeyForFilterCriteria(filterCriteria);
addStaffToCache(key, staffMembers, cacheOptions);
};
exports.setByFilterCriteria = setByFilterCriteria;
var getAllStaff = function (statusFilter, cacheOptions) {
var cachedValue = cache.get(allStaffKey + statusFilter);
if (cachedValue !== undefined) {
// Passed in cache options trump global cache options
var slidingExpirationEnabled = cacheOptions !== undefined
? cacheOptions.slidingExpirationEnabled
: _cacheOptions.slidingExpirationEnabled;
if (slidingExpirationEnabled) {
(0, exports.setAllStaff)(statusFilter, cachedValue, cacheOptions);
}
}
return cachedValue;
};
exports.getAllStaff = getAllStaff;
var setAllStaff = function (statusFilter, staffMembers, cacheOptions) {
var key = allStaffKey + statusFilter;
if (cacheOptions === undefined) {
cache.put(key, staffMembers);
}
else {
cache.put(key, staffMembers, {
ttl: 60 * 1000 * cacheOptions.cacheDurationInMinutes,
});
}
};
exports.setAllStaff = setAllStaff;
var getDirectReports = function (srsId, cacheOptions) {
var key = "".concat(directReportsKeyPrefix, "__").concat(srsId);
var cachedValue = cache.get(key);
if (cachedValue !== undefined) {
// Passed in cache options trump global cache options
var slidingExpirationEnabled = cacheOptions !== undefined
? cacheOptions.slidingExpirationEnabled
: _cacheOptions.slidingExpirationEnabled;
if (slidingExpirationEnabled) {
(0, exports.setDirectReports)(srsId, cachedValue, cacheOptions);
}
}
return cachedValue;
};
exports.getDirectReports = getDirectReports;
var setDirectReports = function (srsId, staffMembers, cacheOptions) {
var key = "".concat(directReportsKeyPrefix, "__").concat(srsId);
if (cacheOptions === undefined) {
cache.put(key, staffMembers);
}
else {
cache.put(key, staffMembers, {
ttl: 60 * 1000 * cacheOptions.cacheDurationInMinutes,
});
}
};
exports.setDirectReports = setDirectReports;
var getManager = function (srsId, cacheOptions) {
var key = "".concat(managerKeyPrefix, "__").concat(srsId);
var cachedValue = cache.get(key);
if (cachedValue !== undefined) {
// Passed in cache options trump global cache options
var slidingExpirationEnabled = cacheOptions !== undefined
? cacheOptions.slidingExpirationEnabled
: _cacheOptions.slidingExpirationEnabled;
if (slidingExpirationEnabled) {
(0, exports.setManager)(srsId, cachedValue, cacheOptions);
}
}
return cachedValue;
};
exports.getManager = getManager;
var setManager = function (srsId, staffMember, cacheOptions) {
var key = "".concat(managerKeyPrefix, "__").concat(srsId);
if (cacheOptions === undefined) {
cache.put(key, staffMember);
}
else {
cache.put(key, staffMember, {
ttl: 60 * 1000 * cacheOptions.cacheDurationInMinutes,
});
}
};
exports.setManager = setManager;
var clearCache = function () {
cache.clear();
};
exports.clearCache = clearCache;
var addStaffToCache = function (key, staffMembers, cacheOptions) {
if (cacheOptions === undefined) {
cache.put(key, staffMembers);
}
else {
cache.put(key, staffMembers, {
ttl: 60 * 1000 * cacheOptions.cacheDurationInMinutes,
});
}
};
var getFromCache = function (key, cacheOptions) {
var cachedValue = cache.get(key);
if (cachedValue !== undefined) {
// Passed in cache options trump global cache options
var slidingExpirationEnabled = cacheOptions !== undefined
? cacheOptions.slidingExpirationEnabled
: _cacheOptions.slidingExpirationEnabled;
if (slidingExpirationEnabled) {
addStaffToCache(key, cachedValue, cacheOptions);
}
}
return cachedValue;
};
var getCacheKeyForFilterCriteria = function (filterCriteria) {
var keys = Object.keys(filterCriteria).sort();
var sortedFilterCriteria = {};
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
var value = filterCriteria[key];
if (value !== undefined && value !== null && value !== "") {
if (key === "azureAdOids") {
var currentValue = value;
if (currentValue.length > 0) {
currentValue.sort();
sortedFilterCriteria.azureAdOids = currentValue;
}
}
else if (key === "racfIds") {
var currentValue = value;
if (currentValue.length > 0) {
currentValue.sort();
sortedFilterCriteria.racfIds = currentValue;
}
}
else if (key === "staffIds") {
var currentValue = value;
if (currentValue.length > 0) {
currentValue.sort(function (a, b) {
if (a < b)
return -1;
if (a > b)
return 1;
else
return 0;
});
sortedFilterCriteria.staffIds = currentValue;
}
}
else if (key === "staffTypeCodes") {
var currentValue = value;
if (currentValue.length > 0) {
currentValue.sort();
sortedFilterCriteria.staffTypeCodes = currentValue;
}
}
else if (key === "status") {
// Active only is the default so if its explicity set its the same as if its not present
if (value !== StatusFilter_1.StatusFilter.ActiveOnly) {
sortedFilterCriteria.status = value;
}
}
else {
// Non array properties need no special treatment
sortedFilterCriteria[key] = value;
}
}
}
return JSON.stringify(sortedFilterCriteria);
};