- adoptPaidSession übernimmt ein erneut zugestelltes checkout.session.completed nicht mehr, wenn das Dokument nach 30 Tagen zurückgesetzt wurde (off, purchaseStatus zurueckgesetzt, gleiche Sitzung): kein Setup, kein Ticket, keine K15/T16 - meldeFreigeschaltet liefert true/false; die Staff-Aktivierung in Textform vermerkt eine nicht gesendete K16 mit Unterlagen in adminNote und antwortet mit warnung: true - disable() nutzt die Sperre feature-activate:<id> statt feature-disable:<id> (Freischaltung und Abschalten überholen sich nicht) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
266 lines
13 KiB
JavaScript
266 lines
13 KiB
JavaScript
import { Router } from 'express'
|
||
import { config } from '../../config.js'
|
||
import { aboMemo } from '../../services/aboStatus.js'
|
||
import { validateTextform } from '../../services/analyse.js'
|
||
import { getDocument, updateDocument } from '../../services/appwriteAdmin.js'
|
||
import { featureConfig } from '../../services/featureRegistry.js'
|
||
import { withLocks } from '../../services/keyedLock.js'
|
||
import { featureLegalAttachments, featureLegalPaths } from '../../services/legalDocs.js'
|
||
import {
|
||
FEATURE_KEYS,
|
||
FEATURE_LABELS,
|
||
adminActivate,
|
||
assertOwnProject,
|
||
customerOf,
|
||
disable,
|
||
freischaltenNachAbnahme,
|
||
getFeatureDoc,
|
||
listCustomerFeatureDocs,
|
||
listCustomerFeatures,
|
||
ticketNachlegen,
|
||
} from '../../services/siteFeatures.js'
|
||
import { driftWarnung, einbauanleitung, ticketStand } from '../../services/termine/einbauanleitung.js'
|
||
import { freischaltHindernis, meldeFreigeschaltet } from '../../services/termine/freischaltung.js'
|
||
import { ladeStand } from '../../services/termine/kontext.js'
|
||
import { termineTextformNoetig } from '../../services/termine/schalter.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
|
||
}
|
||
|
||
/** Das Ticket oder null (keine ticketId, Ticket geloescht); jeder andere Lesefehler wird weitergereicht. */
|
||
async function readTicket(ticketId) {
|
||
if (!ticketId) return null
|
||
try {
|
||
return await getDocument(config.collections.workorders, ticketId)
|
||
} catch (err) {
|
||
if (err.status === 404) return null
|
||
throw err
|
||
}
|
||
}
|
||
|
||
/** Name des Kunden fuer die Anleitung; wie im Ticket (termineTicketDetails) die Kunden-ID, wenn er fehlt oder nicht lesbar ist (nur Kosmetik, kein Fehler). */
|
||
async function customerName(customerId) {
|
||
try {
|
||
return (await customerOf(customerId))?.name || customerId
|
||
} catch (err) {
|
||
console.error(`[funktionen-admin] Kunde ${customerId} fuer die Anleitung nicht lesbar:`, err.message)
|
||
return customerId
|
||
}
|
||
}
|
||
|
||
const K16_FEHLT = 'K16 mit Unterlagen NICHT gesendet – von Hand nachsenden'
|
||
|
||
/**
|
||
* Vermerkt in der Notiz des Funktionsdokuments, dass die K16 mit den Unterlagen nicht rausging (Staff-Aktivierung in Textform, die
|
||
* Unterlagen sind der dauerhafte Datentraeger). Best effort unter der Sperre der Verwaltung: scheitert das Schreiben, steht der Fehler im
|
||
* Log, die Antwort traegt die Warnung trotzdem.
|
||
*/
|
||
async function notiereK16Fehlt(projectId, doc) {
|
||
try {
|
||
await withLocks([`feature-admin:${projectId}`], async () => {
|
||
const frisch = await getDocument(config.collections.portalFeatures, doc.$id)
|
||
await updateDocument(config.collections.portalFeatures, doc.$id, {
|
||
adminNote: composeAdminNote(frisch.adminNote, { by: 'System', action: K16_FEHLT }),
|
||
})
|
||
})
|
||
} catch (err) {
|
||
console.error('[funktionen-admin] Notiz zur nicht gesendeten K16 nicht geschrieben:', err.message)
|
||
}
|
||
}
|
||
|
||
const ticketExists = async (ticketId) => (await readTicket(ticketId)) !== null
|
||
|
||
/**
|
||
* Zustimmung des Kunden in Textform fuer die Aktivierung des Terminplaners durch das Team (F13a). null, wenn sie entfaellt: andere
|
||
* Funktion, Modus off oder pilot, Pilotadresse (termineTextformNoetig) oder ein Dokument, das nichts mehr aktiviert und dem nichts
|
||
* fehlt (schon aktiv, oder durch Kauf freigeschaltet, dann liegt die Zustimmung des Checkouts vor). Sonst { date, channel }; fehlt
|
||
* oder ist eine Angabe ungueltig (Datum vor der Fassung oder in der Zukunft, kein Weg), 400 - ohne dass etwas angelegt wird.
|
||
*/
|
||
async function textformFuerAktivierung({ body, customerId, key, doc }) {
|
||
if (key !== 'termine') return null
|
||
if (doc?.state === 'active' || doc?.unlockedByPurchase === true) return null
|
||
const kunde = await customerOf(customerId)
|
||
if (!termineTextformNoetig(kunde?.email)) return null
|
||
// nur Texte: validateTextform macht aus jedem Wert einen Text (aus ['chat'] wuerde 'chat')
|
||
const text = (wert) => (typeof wert === 'string' ? wert : '')
|
||
return validateTextform({ date: text(body?.textformDate), channel: text(body?.textformChannel) }, { minDate: config.termine.avvVersion })
|
||
}
|
||
|
||
/** Leistungsbeschreibung und AVV als PDF fuer die K16 der Textform-Aktivierung; nicht lesbar => 503, bevor etwas geaendert wird. */
|
||
async function unterlagenAlsAnhang() {
|
||
try {
|
||
return await featureLegalAttachments('termine', { ohne: ['widerruf'] })
|
||
} catch (err) {
|
||
console.error('[funktionen-admin] Rechtsunterlagen (PDF) fuer die Freischaltung nicht lesbar:', err.message)
|
||
throw Object.assign(new Error('Die Rechtsunterlagen (PDF) sind nicht lesbar. Der Terminplaner wurde nicht freigeschaltet. Bitte später erneut versuchen.'), { status: 503 })
|
||
}
|
||
}
|
||
|
||
// Die Funktionsdokumente des Kunden werden einmal gelesen und fuer alle Projekte und Funktionen verwendet.
|
||
// hindernis (freischaltHindernis, termine/freischaltung.js): was die Freischaltung eines Terminplaners in setup noch hindert
|
||
// ('kein_abo' | 'keine_einstellung' | 'abo_unbekannt'), null = nichts; ausserhalb von setup und bei News immer null. Das Abo wird
|
||
// je Anfrage einmal gelesen (ein gemeinsamer aboMemo fuer alle Projekte des Kunden), portalFeatures nicht erneut.
|
||
async function adminView(customerId) {
|
||
const docs = await listCustomerFeatureDocs(customerId)
|
||
const projects = await listCustomerFeatures(customerId, { docs })
|
||
const abo = aboMemo()
|
||
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)))
|
||
view.hindernis = key === 'termine' && doc?.state === 'setup' ? await freischaltHindernis(doc, { abo }) : null
|
||
}
|
||
}
|
||
return projects
|
||
}
|
||
|
||
/**
|
||
* Angaben des Terminplaners fuer die Karte im WOMS (F13a): textformNoetig = verlangt die Aktivierung dieses Kunden die Zustimmung in
|
||
* Textform (Modus on und Adresse nicht in der Pilotliste), avvVersion = Fassung der Unterlagen, auf die sich die Zustimmung bezieht,
|
||
* links = absolute, versionierte Adressen von Leistungsbeschreibung und AVV fuer die Chat-Vorlage (nie Adressen ohne Fassung).
|
||
*/
|
||
async function termineFeld(customerId) {
|
||
const kunde = await customerOf(customerId)
|
||
const { lb, avv } = featureLegalPaths('termine')
|
||
return {
|
||
textformNoetig: termineTextformNoetig(kunde?.email),
|
||
avvVersion: config.termine.avvVersion,
|
||
links: { lb: `${config.publicBaseUrl}${lb}`, avv: `${config.publicBaseUrl}${avv}` },
|
||
}
|
||
}
|
||
|
||
funktionenAdminRouter.get('/:customerId', async (req, res) => {
|
||
try {
|
||
const projects = await adminView(req.params.customerId)
|
||
res.json({ projects, termine: await termineFeld(req.params.customerId) })
|
||
} catch (err) {
|
||
sendError(res, err)
|
||
}
|
||
})
|
||
|
||
/**
|
||
* Einbauanleitung des Terminplaners eines Projekts fuer das WOMS-Panel (Task D8): die volle Fassung des Generators ohne `kurz` (die geht nur
|
||
* ins Ticket), nur lesend. Gilt in jedem Zustand und bei jedem Modus (kein TERMINE_ENABLED-Gate) und ist kein Kundenzugriff (kein
|
||
* customerViewLog, kein Pfad im Kundenrouter). Rechnung und Preis kommen live aus dem Dokument.
|
||
* state: Zustand des Dokuments. ticketWoid, ticketVersion: Ticket des Dokuments und der Stand, auf dem es beruht (null ohne Ticket oder
|
||
* ohne Standzeile). hindernis: nur in setup, was die Freischaltung noch hindert (siehe adminView), sonst null.
|
||
* Ein gespeicherter Stand, der neuer ist als das Ticket, haengt eine Warnung ans Ende von warnungen (driftWarnung); ein geloeschtes
|
||
* Ticket (404) ist keine Warnung und kein Fehler, jeder andere Lesefehler ist einer (keine stille Luecke).
|
||
*/
|
||
funktionenAdminRouter.get('/:customerId/termine/anleitung', async (req, res) => {
|
||
try {
|
||
const { customerId } = req.params
|
||
const { projectId } = req.query
|
||
if (typeof projectId !== 'string' || !projectId) return res.status(400).json({ error: 'Projekt fehlt' })
|
||
const project = await assertOwnProject(customerId, projectId)
|
||
const doc = await getFeatureDoc(customerId, projectId, 'termine')
|
||
if (!doc) {
|
||
throw Object.assign(new Error(`Für dieses Projekt gibt es noch ${FEATURE_LABELS.termine.keine}`), { status: 409 })
|
||
}
|
||
const stand = await ladeStand(doc.$id)
|
||
const ticket = await readTicket(doc.ticketId)
|
||
const ticketVersion = ticket ? ticketStand(ticket.details) : null
|
||
const { stand: anleitungsStand, warnungen, abschnitte, text } = einbauanleitung({
|
||
feature: doc,
|
||
project,
|
||
einstellung: stand?.einstellung ?? null,
|
||
fragebogen: stand?.fragebogen ?? null,
|
||
version: stand?.version ?? 0,
|
||
updatedAt: stand?.updatedAt ?? null,
|
||
kunde: { name: await customerName(customerId) },
|
||
now: new Date(),
|
||
})
|
||
const drift = driftWarnung(ticketVersion, anleitungsStand.version)
|
||
// Felder einzeln benannt: `kurz` (nur fuers Ticket) und alles, was der Generator spaeter dazubekommt, geht nie unbesehen nach aussen
|
||
res.json({
|
||
stand: anleitungsStand,
|
||
warnungen: drift ? [...warnungen, drift] : warnungen,
|
||
abschnitte,
|
||
text,
|
||
state: doc.state,
|
||
ticketWoid: ticket?.woid || null,
|
||
ticketVersion,
|
||
hindernis: doc.state === 'setup' ? await freischaltHindernis(doc) : null,
|
||
})
|
||
} 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)
|
||
let freigeschaltet = null // Terminplaner mit Zustimmung in Textform: K16 mit den Unterlagen nach der Sperre
|
||
await withLocks([`feature-admin:${projectId}`], async () => {
|
||
const doc = await getFeatureDoc(customerId, projectId, key)
|
||
const adminNote = composeAdminNote(doc?.adminNote, { by, action, note })
|
||
if (action === 'activate') {
|
||
const textform = await textformFuerAktivierung({ body: req.body, customerId, key, doc })
|
||
const attachments = textform ? await unterlagenAlsAnhang() : []
|
||
const aktiviert = await adminActivate(customerId, projectId, key, { by, adminNote, textform })
|
||
if (textform) freigeschaltet = { doc: aktiviert, attachments }
|
||
} 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 })
|
||
}
|
||
})
|
||
// Der Kunde bekommt die Unterlagen auf einem dauerhaften Datentraeger (Anhang); ein Versandfehler wird dort geloggt, nie geworfen.
|
||
// Ging K16 nicht raus (Versand, Kunde ohne Adresse), steht es in der Notiz und die Antwort traegt warnung: true - das Team sendet von Hand nach
|
||
let warnung = false
|
||
if (freigeschaltet && !(await meldeFreigeschaltet(freigeschaltet.doc, { attachments: freigeschaltet.attachments }))) {
|
||
warnung = true
|
||
await notiereK16Fehlt(projectId, freigeschaltet.doc)
|
||
}
|
||
res.json({ projects: (await adminView(customerId)).filter((p) => p.projectId === projectId), ...(warnung ? { warnung } : {}) })
|
||
} catch (err) {
|
||
sendError(res, err)
|
||
}
|
||
})
|