dienst_echo_whatsappgroep/src/dienstService.js

44 lines
1.2 KiB
JavaScript

const fs = require('fs')
const path = require('path')
let dienstenCache = null
function getDienstenDir() {
if (!process.env.DIENSTEN_DIR) {
throw new Error('DIENSTEN_DIR is required; configure the canonical Whatsapp_Dienst/diensten directory')
}
return path.resolve(process.env.DIENSTEN_DIR)
}
function loadDiensten() {
if (dienstenCache) return dienstenCache
const dienstenDir = getDienstenDir()
const jsonPath = path.join(dienstenDir, 'dienst-tijden.json')
let raw
try {
raw = fs.readFileSync(jsonPath, 'utf-8')
} catch (err) {
throw new Error(`Failed to read canonical diensten JSON at ${jsonPath}: ${err.message}`)
}
try {
dienstenCache = JSON.parse(raw)
} catch (err) {
throw new Error(`Failed to parse canonical diensten JSON at ${jsonPath}: ${err.message}`)
}
return dienstenCache
}
function findDienst(dienstnummer) {
const diensten = loadDiensten()
return diensten.find(d => String(d.dienstnummer) === dienstnummer) || null
}
function getPngFilePaths(dienst) {
const pngDir = path.join(getDienstenDir(), 'png')
return dienst.paginas.map(p => path.join(pngDir, p.bestandsnaam))
}
module.exports = { loadDiensten, findDienst, getPngFilePaths }