Files
Webklar-Kundenbereich/server/routes/admin/analyse.js
T
2026-10-01 10:19:37 +00:00

50 lines
2.1 KiB
JavaScript

// Admin-API (WOMS): Website-Analyse je Projekt ein-/ausschalten. AVV wird mündlich bestätigt.
import { Router } from 'express'
import { config } from '../../config.js'
import { getDocument } from '../../services/appwriteAdmin.js'
import { requireAppwriteStaff } from '../../middleware/appwriteStaff.js'
import { activateAnalyse, deactivateAnalyse } from '../../services/analyse.js'
const router = Router()
router.use(requireAppwriteStaff)
function sendError(res, err) {
const status = [400, 403, 404, 409, 503].includes(err.status) && !err.type ? err.status : 500
if (status === 500) console.error('[admin-analyse]', err.message)
res.status(status).json({ error: status === 500 ? 'Die Analyse konnte nicht geändert werden' : err.message })
}
router.post('/:projectId', async (req, res) => {
try {
if (!config.analyse.enabled) return res.status(409).json({ error: 'Website-Analyse ist noch nicht freigeschaltet' })
const { on, avvConfirmedVerbally } = req.body || {}
if (typeof on !== 'boolean') return res.status(400).json({ error: 'on muss true oder false sein' })
if (on && avvConfirmedVerbally !== true) {
return res.status(400).json({ error: 'Bitte bestätigen, dass der Kunde den AVV bestätigt hat' })
}
let project
try {
project = await getDocument(config.collections.websiteProjects, req.params.projectId)
} catch (err) {
if (err.status === 404) return res.status(404).json({ error: 'Projekt nicht gefunden' })
throw err
}
const customerId = project.customerId
if (!customerId) return res.status(400).json({ error: 'Projekt hat keinen Kunden' })
const by = String(req.appwriteUser?.name || req.appwriteUser?.email || 'Mitarbeiter').slice(0, 100)
const { doc } = on
? await activateAnalyse({
customerId,
projectId: project.$id,
consent: { accepted: true, avvVersion: config.analyse.avvVersion },
by,
})
: await deactivateAnalyse({ customerId, projectId: project.$id, by })
res.json({ ok: true, state: doc?.state || 'off' })
} catch (err) {
sendError(res, err)
}
})
export default router