lamassu-server/packages/server/lib/new-admin/services/restriction-level.js
padreug 84e6e81f04 fix: handle empty machines table in restrictionLevel query
Return 0 when no machines exist instead of throwing an error.
This fixes the authentication error on fresh installations where
the machines table is empty.
2025-12-31 19:05:01 +01:00

26 lines
690 B
JavaScript

const mem = require('mem')
const { machines } = require('typesafe-db')
// Cache configuration: 30 minutes
const CACHE_DURATION = 30 * 60 * 1000
const _getHighestRestrictionLevel = async () => {
try {
const level = await machines.getHighestRestrictionLevel()
// Return 0 if null/undefined (no machines in database)
return level ?? 0
} catch (err) {
// Log error and return 0 for empty database or other errors
console.error('Error fetching restriction level:', err.message)
return 0
}
}
const getCachedRestrictionLevel = mem(_getHighestRestrictionLevel, {
maxAge: CACHE_DURATION,
cacheKey: () => '',
})
module.exports = {
getCachedRestrictionLevel,
}