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.
26 lines
690 B
JavaScript
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,
|
|
}
|