fieldOptionsService.js
813 Bytes
const { fetchFieldOptionsAdapter } = require('./fieldOptionsAdapters.js')
const optionsCache = new Map()
function buildCacheKey(params = {}) {
return [
params.optionsKey || '',
params.merchantId || '',
params.staffUuid || '',
params.reportDate || '',
params.reportType || 'store'
].join('|')
}
async function fetchFieldOptions(params = {}, options = {}) {
const { useCache = true } = options
const cacheKey = buildCacheKey(params)
if (useCache && optionsCache.has(cacheKey)) {
return optionsCache.get(cacheKey)
}
const result = await fetchFieldOptionsAdapter(params)
if (result?.code === 0 && useCache) {
optionsCache.set(cacheKey, result)
}
return result
}
function clearFieldOptionsCache() {
optionsCache.clear()
}
module.exports = {
fetchFieldOptions,
clearFieldOptionsCache
}