73 lines
2.5 KiB
JavaScript
73 lines
2.5 KiB
JavaScript
import { Router } from 'express'
|
|
import { requireAppwriteStaff } from '../../middleware/appwriteStaff.js'
|
|
import { PRODUCT_KEYS } from '../../services/products.js'
|
|
import { ACTIVE_STATUSES, getCurrentSubscription } from '../../services/stripeBilling.js'
|
|
import {
|
|
createCustomerWithPortalAccess,
|
|
deleteCustomerWithPortalAccess,
|
|
listCustomersForAdmin,
|
|
updateCustomerWithPortalAccess,
|
|
} from '../../services/customerAdmin.js'
|
|
|
|
const router = Router()
|
|
|
|
router.use(requireAppwriteStaff)
|
|
|
|
router.get('/', async (_req, res) => {
|
|
try {
|
|
const customers = await listCustomersForAdmin()
|
|
return res.json({ customers })
|
|
} catch (err) {
|
|
const status = err.status || 500
|
|
return res.status(status).json({ error: err.message || 'Kunden konnten nicht geladen werden' })
|
|
}
|
|
})
|
|
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const result = await createCustomerWithPortalAccess(req.body || {})
|
|
return res.status(201).json(result)
|
|
} catch (err) {
|
|
const status = err.status || 500
|
|
return res.status(status).json({ error: err.message || 'Kunde konnte nicht angelegt werden' })
|
|
}
|
|
})
|
|
|
|
router.patch('/:customerId', async (req, res) => {
|
|
try {
|
|
const result = await updateCustomerWithPortalAccess(req.params.customerId, req.body || {})
|
|
return res.json(result)
|
|
} catch (err) {
|
|
const status = err.status || 500
|
|
return res.status(status).json({ error: err.message || 'Kunde konnte nicht aktualisiert werden' })
|
|
}
|
|
})
|
|
|
|
// Schnittstelle fuers Ticketsystem (Task 13): Produkt und Stand des Abos eines Kunden. WOMS liest daraus,
|
|
// ob der Kunde ein Wartungs-Abo hat (Arbeitszeit-Kontingent). Altbestand ohne productKey gilt als
|
|
// Wartungs-Abo (wie offers.js/takeoverCredits.js); ohne Abo productKey null.
|
|
router.get('/:customerId/abo', async (req, res) => {
|
|
try {
|
|
const sub = await getCurrentSubscription(req.params.customerId)
|
|
if (!sub) return res.json({ productKey: null, active: false })
|
|
return res.json({
|
|
productKey: PRODUCT_KEYS.includes(sub.productKey) ? sub.productKey : 'wartung',
|
|
active: ACTIVE_STATUSES.has(sub.status),
|
|
})
|
|
} catch (err) {
|
|
const status = err.status || 500
|
|
return res.status(status).json({ error: err.message || 'Abo konnte nicht geladen werden' })
|
|
}
|
|
})
|
|
|
|
router.delete('/:customerId', async (req, res) => {
|
|
try {
|
|
return res.json(await deleteCustomerWithPortalAccess(req.params.customerId))
|
|
} catch (err) {
|
|
const status = err.status || 500
|
|
return res.status(status).json({ error: err.message || 'Kunde konnte nicht gelöscht werden' })
|
|
}
|
|
})
|
|
|
|
export default router
|