Add VO management, tariff-option assignment, and 2-step schaltung wizard
VOs (Vertriebspartner-Nummern) - New vos table (one row per VO, scoped to a provider). Migrations add tarife.vo_id and schaltungen.vo_id columns (nullable so legacy rows survive). - New /api/vos CRUD (list, create, update, delete with use-checks). - Admin: new "VOs (Vertriebspartner)" sub-tab. List/create with provider dropdown; edit via prompt; delete refuses while used. - /api/tarife?vo_id=X filter for fetching tariffs of a specific VO. Tariff editor - Add-tariff form requires a VO; new dependent VO dropdown that populates after the provider is chosen. - Edit-tariff modal now shows the VO dropdown and a checkbox grid of all anbieter-wide tarifoptionen. Saving the dialog applies the selection via the existing tarifoptionen_tarife table. - New GET /api/tarife/:id/tarifoptionen-multi returns the option set scoped to the tariff's provider, each annotated with whether it is currently assigned to this tariff. - POST/PUT /api/tarife accept vo_id (required for create, validated against the provider) and an optional tarifoption_ids array. Schaltung wizard - The Schaltung-erfassen tab is now a 2-step wizard: step 1 picks a provider from a button row (mirrors the screenshot the user shared); step 2 shows a 2-column form mirroring the Aktivierungs-/Vertragsdaten layout. - Step 2 cascades: VO -> tariffs filtered by VO -> tarifoptionen filtered by tariff. Erfasser is autofilled from the logged-in user (read-only). - Removed customer fields that the user does not need (Kundennummer, Kennwort/Service-PIN never existed; Rufnummer is now optional both in the form and in the API validation). - After saving, the wizard resets to step 1 ready for the next entry. Schema - tarife: vo_id added. - schaltungen: vo_id added; rufnummer is nullable in new DBs. Existing DBs may still have NOT NULL on rufnummer; the schaltungen route now sends an empty string instead of NULL to remain compatible. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ea3a3fc12c
commit
f2ab23f261
7 files changed
+768
-212
No files matched your search
@@ -0,0 +1,92 @@
|
||||
const express = require('express');
|
||||
const db = require('../db');
|
||||
const asyncHandler = require('../middleware/asyncHandler');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// List all VOs (optionally filtered by anbieter via ?anbieter_id=)
|
||||
router.get('/', asyncHandler(async (req, res) => {
|
||||
const { anbieter_id } = req.query;
|
||||
if (anbieter_id) {
|
||||
const rows = await db.all(
|
||||
`SELECT v.*, a.name AS anbieter_name
|
||||
FROM vos v JOIN anbieter a ON v.anbieter_id = a.id
|
||||
WHERE v.anbieter_id = ? AND v.aktiv = 1
|
||||
ORDER BY v.nummer`,
|
||||
[anbieter_id]
|
||||
);
|
||||
return res.json(rows);
|
||||
}
|
||||
const rows = await db.all(
|
||||
`SELECT v.*, a.name AS anbieter_name
|
||||
FROM vos v JOIN anbieter a ON v.anbieter_id = a.id
|
||||
ORDER BY a.name, v.nummer`
|
||||
);
|
||||
res.json(rows);
|
||||
}));
|
||||
|
||||
router.post('/', asyncHandler(async (req, res) => {
|
||||
const { anbieter_id, nummer, bezeichnung } = req.body || {};
|
||||
if (!anbieter_id) return res.status(400).json({ error: 'Anbieter ist erforderlich' });
|
||||
if (!nummer || !String(nummer).trim()) return res.status(400).json({ error: 'VO-Nummer ist erforderlich' });
|
||||
try {
|
||||
const result = await db.run(
|
||||
'INSERT INTO vos (anbieter_id, nummer, bezeichnung) VALUES (?, ?, ?)',
|
||||
[anbieter_id, String(nummer).trim(), bezeichnung ? String(bezeichnung).trim() : null]
|
||||
);
|
||||
res.json({ id: result.lastID, anbieter_id, nummer: String(nummer).trim(), bezeichnung: bezeichnung || null });
|
||||
} catch (err) {
|
||||
if (/UNIQUE/.test(err.message)) {
|
||||
return res.status(400).json({ error: 'VO mit dieser Nummer existiert bereits für diesen Anbieter' });
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
router.put('/:id', asyncHandler(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { nummer, bezeichnung, aktiv } = req.body || {};
|
||||
const current = await db.get('SELECT * FROM vos WHERE id = ?', [id]);
|
||||
if (!current) return res.status(404).json({ error: 'VO nicht gefunden' });
|
||||
const nextNummer = nummer !== undefined ? String(nummer).trim() : current.nummer;
|
||||
if (!nextNummer) return res.status(400).json({ error: 'VO-Nummer darf nicht leer sein' });
|
||||
try {
|
||||
await db.run(
|
||||
'UPDATE vos SET nummer = ?, bezeichnung = ?, aktiv = ? WHERE id = ?',
|
||||
[
|
||||
nextNummer,
|
||||
bezeichnung !== undefined ? (bezeichnung ? String(bezeichnung).trim() : null) : current.bezeichnung,
|
||||
aktiv !== undefined ? (aktiv ? 1 : 0) : current.aktiv,
|
||||
id,
|
||||
]
|
||||
);
|
||||
res.json({ message: 'VO erfolgreich aktualisiert' });
|
||||
} catch (err) {
|
||||
if (/UNIQUE/.test(err.message)) {
|
||||
return res.status(400).json({ error: 'VO mit dieser Nummer existiert bereits für diesen Anbieter' });
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
router.delete('/:id', asyncHandler(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
// Prevent deletion if any tariff or schaltung references it.
|
||||
const used = await db.get(
|
||||
`SELECT
|
||||
(SELECT COUNT(*) FROM tarife WHERE vo_id = ?) AS n_tarife,
|
||||
(SELECT COUNT(*) FROM schaltungen WHERE vo_id = ?) AS n_schaltungen`,
|
||||
[id, id]
|
||||
);
|
||||
if (used.n_tarife + used.n_schaltungen > 0) {
|
||||
const parts = [];
|
||||
if (used.n_tarife > 0) parts.push(`${used.n_tarife} Tarif(e)`);
|
||||
if (used.n_schaltungen > 0) parts.push(`${used.n_schaltungen} Schaltung(en)`);
|
||||
return res.status(400).json({ error: `VO kann nicht gelöscht werden: ${parts.join(', ')} verwenden diese VO` });
|
||||
}
|
||||
const result = await db.run('DELETE FROM vos WHERE id = ?', [id]);
|
||||
if (result.changes === 0) return res.status(404).json({ error: 'VO nicht gefunden' });
|
||||
res.json({ message: 'VO erfolgreich gelöscht' });
|
||||
}));
|
||||
|
||||
module.exports = router;
|
||||
Reference in new issue
Block a user