diff --git a/server/routes/admin/analyse.js b/server/routes/admin/analyse.js index 1bf49cb..ceee15c 100644 --- a/server/routes/admin/analyse.js +++ b/server/routes/admin/analyse.js @@ -3,7 +3,8 @@ 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' +import { activateAnalyse, analyseEligibility, deactivateAnalyse, ELIGIBILITY_MESSAGES } from '../../services/analyse.js' +import { getFeatureDoc } from '../../services/siteFeatures.js' const router = Router() router.use(requireAppwriteStaff) @@ -14,6 +15,38 @@ function sendError(res, err) { res.status(status).json({ error: status === 500 ? 'Die Analyse konnte nicht geändert werden' : err.message }) } +// Lesezugriff fuer das WOMS: Zustand + AVV-Angaben (nie siteKey oder Geheimnisse) +router.get('/:projectId', async (req, res) => { + try { + 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 || '' + const [doc, eligibility] = await Promise.all([ + customerId ? getFeatureDoc(customerId, project.$id, 'analyse') : null, + analyseEligibility(customerId, project.$id), + ]) + res.json({ + enabled: config.analyse.enabled, + state: doc ? doc.state || 'off' : null, + accentColor: doc?.accentColor || '#111111', + avvVersion: config.analyse.avvVersion, + avvAcceptedAt: doc?.avvAcceptedAt || null, + avvAcceptedBy: doc?.avvAcceptedBy || null, + activatedAt: doc?.activatedAt || null, + offSince: doc?.offSince || null, + eligible: eligibility.ok, + reasonText: eligibility.reason ? ELIGIBILITY_MESSAGES[eligibility.reason] : null, + }) + } catch (err) { + sendError(res, err) + } +}) + router.post('/:projectId', async (req, res) => { try { if (!config.analyse.enabled) return res.status(409).json({ error: 'Website-Analyse ist noch nicht freigeschaltet' }) diff --git a/server/routes/admin/services.js b/server/routes/admin/services.js index 62457ff..86dd63b 100644 --- a/server/routes/admin/services.js +++ b/server/routes/admin/services.js @@ -30,7 +30,11 @@ function previewUrlFor(p) { router.get('/', async (_req, res) => { try { - const { kinds, features } = servicesCatalog() + const { kinds, features: catalogFeatures } = servicesCatalog() + // Katalog nicht mutieren: Analyse ist 'available', sobald der Schalter ANALYSE_ENABLED an ist + const features = config.analyse.enabled + ? { ...catalogFeatures, analyse: { ...catalogFeatures.analyse, status: 'available' } } + : catalogFeatures const projRes = await listDocuments(config.collections.websiteProjects, [Query.limit(5000)]) const projects = (projRes || []).filter((p) => !p.archived && !p.isTemplate) @@ -39,6 +43,12 @@ router.get('/', async (_req, res) => { loadByIds(config.collections.workorders, projects.map((p) => p.ticketId)), ]) + const analyseDocs = await listDocuments(config.collections.portalFeatures, [ + Query.equal('featureKey', 'analyse'), + Query.limit(5000), + ]) + const analyseState = new Map((analyseDocs || []).map((d) => [d.projectId, d.state || 'off'])) + const groups = {} for (const p of projects) { const key = kinds.some((k) => k.key === p.kind) ? p.kind : 'missing' @@ -52,7 +62,7 @@ router.get('/', async (_req, res) => { woid: tickets.get(p.ticketId)?.woid || '', previewUrl: previewUrlFor(p), previewState: p.previewState || '', - features: {}, + features: { analyse: analyseState.get(p.$id) || null }, }) } diff --git a/test/analyseRoutes.test.js b/test/analyseRoutes.test.js index e4e52c9..e377dab 100644 --- a/test/analyseRoutes.test.js +++ b/test/analyseRoutes.test.js @@ -192,6 +192,30 @@ test('Admin: 401, 400, 200', async () => { assert.deepEqual(off.body, { ok: true, state: 'off' }) }) +test('Admin: Lesezugriff GET', async () => { + const adminGet = (id, jwt = 'jwt-admin') => rawGet(`/api/admin/analyse/${id}`, { headers: { Authorization: `Bearer ${jwt}` } }) + assert.equal((await rawGet('/api/admin/analyse/p1')).status, 401) + assert.equal((await adminGet('p1', 'jwt-kunde')).status, 403) + assert.equal((await adminGet('nix')).status, 404) + await adminPost('p1', { on: false }) + const off = await adminGet('p1') + assert.equal(off.status, 200) + assert.equal(off.body.enabled, true) + assert.equal(off.body.eligible, true) + assert.equal(off.body.reasonText, null) + assert.equal(off.body.avvVersion, config.analyse.avvVersion) + await adminPost('p1', { on: true, avvConfirmedVerbally: true }) + const on = await adminGet('p1') + assert.equal(on.body.state, 'active') + assert.equal(on.body.avvAcceptedBy, 'kenso@webklar.com') + assert.ok(on.body.activatedAt) + assert.ok(!('siteKey' in on.body) && !JSON.stringify(on.body).includes((await featureDoc('c1', 'p1')).siteKey)) + const priv = await adminGet('p2') + assert.equal(priv.body.eligible, false) + assert.equal(priv.body.state, null) + assert.equal(priv.body.reasonText, 'Die Website-Analyse gibt es nur für öffentliche Websites.') +}) + test('Fix a: Kunde ändert gesperrtes Dokument nicht, Mitarbeiter schon', async () => { await adminPost('p6', { on: true, avvConfirmedVerbally: true }) const cur = await featureDoc('c1', 'p6') diff --git a/test/servicesRoute.test.js b/test/servicesRoute.test.js index 3e056ec..6464a9f 100644 --- a/test/servicesRoute.test.js +++ b/test/servicesRoute.test.js @@ -18,6 +18,7 @@ before(async () => { ], workorders: [{ $id: 't1', woid: '100115', customerId: 'c1', status: 'Open' }], customers: [{ $id: 'c1', name: 'Döner Nation' }], + portalFeatures: [{ $id: 'f1', customerId: 'c1', projectId: 'p1', featureKey: 'analyse', state: 'active' }], }, }) Object.assign(process.env, { APPWRITE_ENDPOINT: aw.endpoint, APPWRITE_API_KEY: 'test-key' }) @@ -53,10 +54,29 @@ test('Katalog + Gruppen, Archiv/Vorlage ausgeschlossen, Namen aufgelöst', async assert.deepEqual(body.groups.website[0], { id: 'p1', projectName: 'Doener', repoFullName: 'JUSN/doener', customerId: 'c1', customerName: 'Döner Nation', ticketId: 't1', woid: '100115', - previewUrl: 'https://doener.project.webklar.com', previewState: '', features: {}, + previewUrl: 'https://doener.project.webklar.com', previewState: '', features: { analyse: 'active' }, }) assert.equal(body.groups.missing[0].previewUrl, 'https://ohne.de') assert.equal(body.groups.missing[0].customerName, '') const all = Object.values(body.groups).flat().map((p) => p.id) assert.ok(!all.includes('p3') && !all.includes('p4')) }) + +test('Analyse: features.analyse je Projekt, Katalogstatus folgt ANALYSE_ENABLED', async () => { + const { config } = await import('../server/config.js') + const was = config.analyse.enabled + try { + config.analyse.enabled = false + let body = await (await get('jwt-admin')).json() + assert.equal(body.features.analyse.status, 'in_development') + config.analyse.enabled = true + body = await (await get('jwt-admin')).json() + assert.equal(body.features.analyse.status, 'available') + assert.equal(body.groups.website[0].features.analyse, 'active') + assert.equal(body.groups.missing[0].features.analyse, null) + } finally { + config.analyse.enabled = was + } + const { FEATURES } = await import('../server/services/projectKinds.js') + assert.equal(FEATURES.analyse.status, 'in_development') +})