Chore: throw error if undefined operatorId

This commit is contained in:
csrapr 2021-03-18 18:14:19 +00:00 committed by Josh Harvey
parent 8d6ed7c2a3
commit 35224e415c
2 changed files with 29 additions and 19 deletions

View file

@ -12,25 +12,29 @@ const populateSettings = function (req, res, next) {
state.oldVersionId = versionId
}
// Clear cache every hour
if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) {
settingsCache.clear(operatorId)
}
try {
// Clear cache every hour
if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) {
settingsCache.clear(operatorId)
}
if (!versionId && settingsCache.getCache(operatorId)) {
req.settings = settingsCache.getCache(operatorId)
return next()
}
if (!versionId && settingsCache.getCache(operatorId)) {
req.settings = settingsCache.getCache(operatorId)
return next()
}
if (!versionId && !settingsCache.getCache(operatorId)) {
return newSettingsLoader.loadLatest()
.then(settings => {
settingsCache.setCache(operatorId, settings)
settingsCache.setTimestamp(operatorId, Date.now())
req.settings = settings
})
.then(() => next())
.catch(next)
if (!versionId && !settingsCache.getCache(operatorId)) {
return newSettingsLoader.loadLatest()
.then(settings => {
settingsCache.setCache(operatorId, settings)
settingsCache.setTimestamp(operatorId, Date.now())
req.settings = settings
})
.then(() => next())
.catch(next)
}
} catch (e) {
console.error(e)
}
newSettingsLoader.load(versionId)

View file

@ -1,9 +1,15 @@
const _ = require('lodash/fp')
const state = require('./state')
const getTimestamp = (operatorId) => state.settingsCache[operatorId] ? state.settingsCache[operatorId].timestamp : null
const getTimestamp = (operatorId) => {
if (!operatorId) throw new Error('operatorId must not be nil or empty')
return state.settingsCache[operatorId] ? state.settingsCache[operatorId].timestamp : null
}
const getCache = (operatorId) => state.settingsCache[operatorId] ? state.settingsCache[operatorId].cache : null
const getCache = (operatorId) => {
if (!operatorId) throw new Error('operatorId must not be nil or empty')
return state.settingsCache[operatorId] ? state.settingsCache[operatorId].cache : null
}
const setTimestamp = (operatorId, newTimestamp) => {
state.settingsCache = _.set([operatorId, 'timestamp'], newTimestamp, state.settingsCache)