Neue Team-Aktionen abnahme (Freischalten ohne Ticket-Schliessung, K16 einmal, unter feature-activate:<id> mit Haken freischaltHindernis) und ticket (Ticket nachlegen: Zahlung bei Stripe pruefen und Webhook-Weg, geloeschtes Ticket ersetzen, vorhandenes unberuehrt). Ticketanlage aus markPaid in legeTicketAn herausgeloest. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
119 lines
4.7 KiB
JavaScript
119 lines
4.7 KiB
JavaScript
import { Router } from 'express'
|
||
import { config } from '../../config.js'
|
||
import { getDocument } from '../../services/appwriteAdmin.js'
|
||
import { featureConfig } from '../../services/featureRegistry.js'
|
||
import { withLocks } from '../../services/keyedLock.js'
|
||
import {
|
||
FEATURE_KEYS,
|
||
FEATURE_LABELS,
|
||
adminActivate,
|
||
assertOwnProject,
|
||
disable,
|
||
freischaltenNachAbnahme,
|
||
getFeatureDoc,
|
||
listCustomerFeatureDocs,
|
||
listCustomerFeatures,
|
||
ticketNachlegen,
|
||
} from '../../services/siteFeatures.js'
|
||
|
||
/**
|
||
* Funktionen (News-Pinnwand, Terminplaner) je Kundenprojekt im Ticketsystem (Kunden-Reiter "Funktionen").
|
||
* Eingehaengt in routes/team/index.js unter /api/team/funktionen, dort hinter requireStaffJwt.
|
||
*/
|
||
export const funktionenAdminRouter = Router()
|
||
|
||
const NOTE_MAX = 300
|
||
const adminName = (req) => req.session.adminName || req.session.name || 'Admin'
|
||
|
||
function sendError(res, err) {
|
||
const status = err.status >= 400 && err.status < 600 ? err.status : 500
|
||
if (status >= 500) console.error('[funktionen-admin]', err.message)
|
||
return res.status(status).json({ error: err.message || 'Interner Serverfehler' })
|
||
}
|
||
|
||
/** Neuester Eintrag zuerst; aeltere Eintraege fallen vorn heraus, sobald 300 Zeichen voll sind. */
|
||
export function composeAdminNote(previous, { by, action, note }, now = new Date()) {
|
||
const entry = `${now.toISOString()} ${by}: ${action}${note ? ` – ${note}` : ''}`.slice(0, NOTE_MAX)
|
||
const older = String(previous || '')
|
||
return older ? `${entry} | ${older}`.slice(0, NOTE_MAX) : entry
|
||
}
|
||
|
||
async function ticketExists(ticketId) {
|
||
if (!ticketId) return false
|
||
try {
|
||
await getDocument(config.collections.workorders, ticketId)
|
||
return true
|
||
} catch (err) {
|
||
if (err.status === 404) return false
|
||
throw err
|
||
}
|
||
}
|
||
|
||
// Die Funktionsdokumente des Kunden werden einmal gelesen und fuer alle Projekte und Funktionen verwendet
|
||
async function adminView(customerId) {
|
||
const docs = await listCustomerFeatureDocs(customerId)
|
||
const projects = await listCustomerFeatures(customerId, { docs })
|
||
for (const p of projects) {
|
||
for (const key of FEATURE_KEYS) {
|
||
const view = p.features[key]
|
||
const doc = docs.find((d) => d.projectId === p.projectId && d.featureKey === key)
|
||
view.adminNote = doc?.adminNote || ''
|
||
view.ticketMissing = view.state === 'setup' && !(doc?.ticketId && (await ticketExists(doc.ticketId)))
|
||
}
|
||
}
|
||
return projects
|
||
}
|
||
|
||
funktionenAdminRouter.get('/:customerId', async (req, res) => {
|
||
try {
|
||
res.json({ projects: await adminView(req.params.customerId) })
|
||
} catch (err) {
|
||
sendError(res, err)
|
||
}
|
||
})
|
||
|
||
// Je Funktion (/news, /termine; jeder andere Schluessel -> 404): activate (ohne Zahlung), disable, abnahme (Freischalten nach Abnahme, in
|
||
// setup, ohne das Ticket zu schliessen) und ticket (fehlendes Einrichtungs-Ticket nachlegen, in setup). abnahme und ticket gibt es nur fuer
|
||
// Funktionen mit ticketFreischaltung im Register.
|
||
const ACTIONS = ['activate', 'disable', 'abnahme', 'ticket']
|
||
const TICKET_ACTIONS = ['abnahme', 'ticket']
|
||
|
||
funktionenAdminRouter.post('/:customerId/:key', async (req, res) => {
|
||
try {
|
||
const { customerId, key } = req.params
|
||
if (!FEATURE_KEYS.includes(key)) return res.status(404).json({ error: 'Unbekannte Funktion' })
|
||
const { projectId, action } = req.body || {}
|
||
const note = typeof req.body?.note === 'string' ? req.body.note.trim().slice(0, 120) : ''
|
||
if (!ACTIONS.includes(action)) {
|
||
return res.status(400).json({ error: 'Unbekannte Aktion' })
|
||
}
|
||
if (typeof projectId !== 'string' || !projectId) {
|
||
return res.status(400).json({ error: 'Projekt fehlt' })
|
||
}
|
||
if (TICKET_ACTIONS.includes(action) && featureConfig(key)?.ticketFreischaltung !== true) {
|
||
return res.status(409).json({ error: 'Diese Aktion gibt es für diese Funktion nicht.' })
|
||
}
|
||
await assertOwnProject(customerId, projectId)
|
||
const by = adminName(req)
|
||
await withLocks([`feature-admin:${projectId}`], async () => {
|
||
const doc = await getFeatureDoc(customerId, projectId, key)
|
||
const adminNote = composeAdminNote(doc?.adminNote, { by, action, note })
|
||
if (action === 'activate') {
|
||
await adminActivate(customerId, projectId, key, { by, adminNote })
|
||
} else if (action === 'abnahme') {
|
||
await freischaltenNachAbnahme({ customerId, projectId, key, by, adminNote })
|
||
} else if (action === 'ticket') {
|
||
await ticketNachlegen({ customerId, projectId, key, by, adminNote })
|
||
} else {
|
||
if (!doc) {
|
||
throw Object.assign(new Error(`Für dieses Projekt gibt es noch ${FEATURE_LABELS[key].keine}`), { status: 409 })
|
||
}
|
||
await disable(doc.$id, { by, adminNote })
|
||
}
|
||
})
|
||
res.json({ projects: (await adminView(customerId)).filter((p) => p.projectId === projectId) })
|
||
} catch (err) {
|
||
sendError(res, err)
|
||
}
|
||
})
|