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 state.oldVersionId = versionId
} }
// Clear cache every hour try {
if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) { // Clear cache every hour
settingsCache.clear(operatorId) if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) {
} settingsCache.clear(operatorId)
}
if (!versionId && settingsCache.getCache(operatorId)) { if (!versionId && settingsCache.getCache(operatorId)) {
req.settings = settingsCache.getCache(operatorId) req.settings = settingsCache.getCache(operatorId)
return next() return next()
} }
if (!versionId && !settingsCache.getCache(operatorId)) { if (!versionId && !settingsCache.getCache(operatorId)) {
return newSettingsLoader.loadLatest() return newSettingsLoader.loadLatest()
.then(settings => { .then(settings => {
settingsCache.setCache(operatorId, settings) settingsCache.setCache(operatorId, settings)
settingsCache.setTimestamp(operatorId, Date.now()) settingsCache.setTimestamp(operatorId, Date.now())
req.settings = settings req.settings = settings
}) })
.then(() => next()) .then(() => next())
.catch(next) .catch(next)
}
} catch (e) {
console.error(e)
} }
newSettingsLoader.load(versionId) newSettingsLoader.load(versionId)

View file

@ -1,9 +1,15 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const state = require('./state') 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) => { const setTimestamp = (operatorId, newTimestamp) => {
state.settingsCache = _.set([operatorId, 'timestamp'], newTimestamp, state.settingsCache) state.settingsCache = _.set([operatorId, 'timestamp'], newTimestamp, state.settingsCache)