Entscheidungen vom 04.10.2026: - Projekt-Routen (website-projects, preview, gitea, services, analyse) fuer jeden Mitarbeiter (Label mitarbeiter oder admin); Kunden-, Mitarbeiter- und Finanz-Verwaltung bleiben nur fuer Admins - Neue Repos aus der Vorlage werden privat angelegt - Archivierte Projekte sieht der Kunde nicht mehr: Projektliste, Projektdetails, Preview-Zugang, Funktionen (Mitarbeiter-Ansicht schon) - Projektliste des Kunden mit Limit (Appwrite liefert sonst nur 25) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.1 KiB
JavaScript
83 lines
3.1 KiB
JavaScript
import { Router } from 'express'
|
|
import { config } from '../../config.js'
|
|
import { listDocuments, Query } from '../../services/appwriteAdmin.js'
|
|
import { servicesCatalog } from '../../services/projectKinds.js'
|
|
import { requireAppwriteTeam } from '../../middleware/appwriteStaff.js'
|
|
|
|
const router = Router()
|
|
|
|
// Projekt-Aktionen: jeder Mitarbeiter (nicht nur Admins)
|
|
router.use(requireAppwriteTeam)
|
|
|
|
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: 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)
|
|
|
|
const [customers, tickets] = await Promise.all([
|
|
loadByIds(config.collections.customers, projects.map((p) => p.customerId)),
|
|
loadByIds(config.collections.workorders, projects.map((p) => p.ticketId)),
|
|
])
|
|
|
|
// Ein Fehler hier darf die Services-Seite nicht kosten: dann ohne Analyse-Zustand
|
|
let analyseState = new Map()
|
|
try {
|
|
const analyseDocs = await listDocuments(config.collections.portalFeatures, [
|
|
Query.equal('featureKey', 'analyse'),
|
|
Query.limit(5000),
|
|
])
|
|
analyseState = new Map((analyseDocs || []).map((d) => [d.projectId, d.state || 'off']))
|
|
} catch (err) {
|
|
console.warn('[admin-services] Analyse-Zustände nicht geladen:', err.message)
|
|
}
|
|
|
|
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),
|
|
previewState: p.previewState || '',
|
|
features: { analyse: analyseState.get(p.$id) || null },
|
|
})
|
|
}
|
|
|
|
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
|