From 08958dee772662fb180f6b13c1b29be7237d809c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 30 Sep 2026 17:29:37 +0000 Subject: [PATCH] Projektarten: Services-Uebersicht fuer WOMS Co-Authored-By: Claude Sonnet 5.5 --- server/index.js | 2 ++ server/routes/admin/services.js | 64 +++++++++++++++++++++++++++++++++ test/servicesRoute.test.js | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 server/routes/admin/services.js create mode 100644 test/servicesRoute.test.js diff --git a/server/index.js b/server/index.js index bee591c..78c0dce 100644 --- a/server/index.js +++ b/server/index.js @@ -22,6 +22,7 @@ import giteaWebhookRoutes from './routes/webhook/gitea.js' import pushTimeRoutes from './routes/pushTime.js' import adminGiteaRoutes from './routes/admin/gitea.js' import adminWebsiteProjectsRoutes from './routes/admin/websiteProjects.js' +import adminServicesRoutes from './routes/admin/services.js' import adminCustomersRoutes from './routes/admin/customers.js' import adminEmployeesRoutes from './routes/admin/employees.js' import adminInvoiceClientsRoutes from './routes/admin/invoiceClients.js' @@ -71,6 +72,7 @@ app.use('/api/funktionen', funktionenRoutes) app.use('/api/portal-admin', portalAdminRoutes) app.use('/api/admin/gitea', adminGiteaRoutes) app.use('/api/admin/website-projects', adminWebsiteProjectsRoutes) +app.use('/api/admin/services', adminServicesRoutes) app.use('/api/admin/customers', adminCustomersRoutes) app.use('/api/admin/employees', adminEmployeesRoutes) app.use('/api/admin/invoice-clients', adminInvoiceClientsRoutes) diff --git a/server/routes/admin/services.js b/server/routes/admin/services.js new file mode 100644 index 0000000..048e312 --- /dev/null +++ b/server/routes/admin/services.js @@ -0,0 +1,64 @@ +import { Router } from 'express' +import { config } from '../../config.js' +import { listDocuments, Query } from '../../services/appwriteAdmin.js' +import { servicesCatalog } from '../../services/projectKinds.js' +import { requireAppwriteStaff } from '../../middleware/appwriteStaff.js' + +const router = Router() + +router.use(requireAppwriteStaff) + +const CHUNK = 100 + +// Lädt Dokumente per $id in Blöcken (ein Request je 100 IDs statt je Projekt) +async function loadByIds(collectionId, ids) { + const map = new Map() + const unique = [...new Set(ids.filter(Boolean))] + for (let i = 0; i < unique.length; i += CHUNK) { + const part = unique.slice(i, i + CHUNK) + const res = await listDocuments(collectionId, [Query.equal('$id', part), Query.limit(CHUNK)]) + for (const doc of res || []) map.set(doc.$id, doc) + } + return map +} + +function previewUrlFor(p) { + if (p.liveDomain) return `https://${p.liveDomain}` + if (p.subdomain) return `https://${p.subdomain}.project.webklar.com` + return '' +} + +router.get('/', async (_req, res) => { + try { + const { kinds, features } = servicesCatalog() + const projRes = await listDocuments(config.collections.websiteProjects, [Query.limit(5000)]) + const projects = (projRes || []).filter((p) => !p.archived && !p.isTemplate) + + const [customers, tickets] = await Promise.all([ + loadByIds(config.collections.customers, projects.map((p) => p.customerId)), + loadByIds(config.collections.workorders, projects.map((p) => p.ticketId)), + ]) + + const groups = {} + for (const p of projects) { + const key = kinds.some((k) => k.key === p.kind) ? p.kind : 'missing' + ;(groups[key] ||= []).push({ + id: p.$id, + projectName: p.projectName || '', + repoFullName: p.repoFullName || '', + customerId: p.customerId || '', + customerName: customers.get(p.customerId)?.name || '', + ticketId: p.ticketId || '', + woid: tickets.get(p.ticketId)?.woid || '', + previewUrl: previewUrlFor(p), + features: {}, + }) + } + + return res.json({ kinds, features, groups }) + } catch (err) { + return res.status(err.status || 500).json({ error: err.message || 'Services konnten nicht geladen werden' }) + } +}) + +export default router diff --git a/test/servicesRoute.test.js b/test/servicesRoute.test.js new file mode 100644 index 0000000..9d1a3dd --- /dev/null +++ b/test/servicesRoute.test.js @@ -0,0 +1,62 @@ +import { test, before, after } from 'node:test' +import assert from 'node:assert/strict' +import express from 'express' +import { startFakeAppwrite } from './helpers/fakeAppwrite.js' + +let aw +let server +let baseUrl + +before(async () => { + aw = await startFakeAppwrite({ + seed: { + websiteProjects: [ + { $id: 'p1', projectName: 'Doener', repoFullName: 'JUSN/doener', customerId: 'c1', ticketId: 't1', subdomain: 'doener', kind: 'website' }, + { $id: 'p2', projectName: 'Ohne Art', repoFullName: 'JUSN/ohne', customerId: '', ticketId: '', liveDomain: 'ohne.de', subdomain: 'ohne' }, + { $id: 'p3', projectName: 'Alt', repoFullName: 'JUSN/alt', kind: 'website', archived: true }, + { $id: 'p4', projectName: 'Vorlage', repoFullName: 'JUSN/tpl', kind: 'website', isTemplate: true }, + ], + workorders: [{ $id: 't1', woid: '100115', customerId: 'c1', status: 'Open' }], + customers: [{ $id: 'c1', name: 'Döner Nation' }], + }, + }) + Object.assign(process.env, { APPWRITE_ENDPOINT: aw.endpoint, APPWRITE_API_KEY: 'test-key' }) + const { default: routes } = await import('../server/routes/admin/services.js') + const app = express() + app.use('/api/admin/services', routes) + server = await new Promise((resolve) => { + const s = app.listen(0, '127.0.0.1', () => resolve(s)) + }) + baseUrl = `http://127.0.0.1:${server.address().port}` +}) + +after(async () => { + await new Promise((resolve) => server.close(resolve)) + await aw.close() +}) + +const get = (jwt) => + fetch(`${baseUrl}/api/admin/services`, { headers: jwt ? { Authorization: `Bearer ${jwt}` } : {} }) + +test('ohne Staff-JWT ⇒ 401', async () => { + assert.equal((await get()).status, 401) +}) + +test('Katalog + Gruppen, Archiv/Vorlage ausgeschlossen, Namen aufgelöst', async () => { + const r = await get('jwt-admin') + assert.equal(r.status, 200) + const body = await r.json() + assert.equal(body.kinds.length, 6) + assert.equal(body.features.analyse.label, 'Website-Analyse') + assert.equal(body.groups.website.length, 1) + assert.equal(body.groups.missing.length, 1) + 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', features: {}, + }) + 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')) +})