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>
231 lines
9.6 KiB
JavaScript
231 lines
9.6 KiB
JavaScript
const express = require('express');
|
|
const db = require('../db');
|
|
const asyncHandler = require('../middleware/asyncHandler');
|
|
|
|
const router = express.Router();
|
|
|
|
function validateDate(s) {
|
|
return typeof s === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(s) && !isNaN(new Date(s).getTime());
|
|
}
|
|
|
|
function validateHardware(h) {
|
|
return !h || String(h).length === 15;
|
|
}
|
|
|
|
function validateAmount(a) {
|
|
return a === null || a === undefined || (Number.isFinite(parseFloat(a)) && parseFloat(a) >= 0);
|
|
}
|
|
|
|
function validateSchaltungBody(body) {
|
|
const { schaltungs_id, eingabe_datum, schaltungs_datum, vorname, nachname,
|
|
anbieter_id, tarif_id, hardware, hardware_ek, kundenverkaufspreis_netto } = body || {};
|
|
if (!schaltungs_id || !schaltungs_id.trim()) return 'Schaltungs-ID ist erforderlich';
|
|
if (!validateDate(eingabe_datum)) return 'Ungültiges Eingabe-Datum (Format: YYYY-MM-DD)';
|
|
if (!validateDate(schaltungs_datum)) return 'Ungültiges Schaltungs-Datum (Format: YYYY-MM-DD)';
|
|
if (!vorname || !vorname.trim()) return 'Vorname ist erforderlich';
|
|
if (!nachname || !nachname.trim()) return 'Nachname ist erforderlich';
|
|
if (!anbieter_id) return 'Anbieter ist erforderlich';
|
|
if (!tarif_id) return 'Tarif ist erforderlich';
|
|
if (!validateHardware(hardware)) return 'Hardware muss genau 15 Zeichen lang sein';
|
|
if (hardware && hardware.trim() !== '') {
|
|
if (!validateAmount(hardware_ek)) return 'Hardware-EK muss eine positive Zahl sein';
|
|
if (!validateAmount(kundenverkaufspreis_netto)) return 'Kundenverkaufspreis muss eine positive Zahl sein';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
router.get('/', asyncHandler(async (req, res) => {
|
|
const rows = await db.all(
|
|
`SELECT s.*, a.name AS anbieter_name, t.name AS tarif_name
|
|
FROM schaltungen s
|
|
JOIN anbieter a ON s.anbieter_id = a.id
|
|
JOIN tarife t ON s.tarif_id = t.id
|
|
ORDER BY s.created_at DESC`
|
|
);
|
|
res.json(rows);
|
|
}));
|
|
|
|
router.post('/', asyncHandler(async (req, res) => {
|
|
const err = validateSchaltungBody(req.body);
|
|
if (err) return res.status(400).json({ error: err });
|
|
|
|
const b = req.body;
|
|
// rufnummer may be empty - existing DBs may have a NOT NULL constraint, so
|
|
// we pass an empty string rather than NULL.
|
|
const rufnummerValue = b.rufnummer && String(b.rufnummer).trim() ? String(b.rufnummer).trim() : '';
|
|
const tarifoptionen = Array.isArray(b.tarifoptionen) ? b.tarifoptionen : [];
|
|
try {
|
|
const newId = await db.transaction(async () => {
|
|
const result = await db.run(
|
|
`INSERT INTO schaltungen
|
|
(schaltungs_id, eingabe_datum, schaltungs_datum, vorname, nachname, firma, rufnummer,
|
|
anbieter_id, vo_id, tarif_id, hardware, hardware_ek, kundenverkaufspreis_netto)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
b.schaltungs_id, b.eingabe_datum, b.schaltungs_datum, b.vorname, b.nachname,
|
|
b.firma || null, rufnummerValue, b.anbieter_id, b.vo_id || null, b.tarif_id, b.hardware || null,
|
|
b.hardware_ek || null, b.kundenverkaufspreis_netto || null,
|
|
]
|
|
);
|
|
for (const tarifoptionId of tarifoptionen) {
|
|
await db.run(
|
|
'INSERT OR IGNORE INTO schaltungen_tarifoptionen (schaltung_id, tarifoption_id) VALUES (?, ?)',
|
|
[result.lastID, tarifoptionId]
|
|
);
|
|
}
|
|
return result.lastID;
|
|
});
|
|
res.json({ id: newId, message: 'Schaltung erfolgreich erstellt' });
|
|
} catch (e) {
|
|
if (/UNIQUE/.test(e.message)) {
|
|
return res.status(400).json({ error: 'Schaltungs-ID existiert bereits' });
|
|
}
|
|
throw e;
|
|
}
|
|
}));
|
|
|
|
router.put('/:id', asyncHandler(async (req, res) => {
|
|
const { id } = req.params;
|
|
const err = validateSchaltungBody(req.body);
|
|
if (err) return res.status(400).json({ error: err });
|
|
|
|
const b = req.body;
|
|
const tarifoptionen = Array.isArray(b.tarifoptionen) ? b.tarifoptionen : [];
|
|
const rufnummerValue = b.rufnummer && String(b.rufnummer).trim() ? String(b.rufnummer).trim() : '';
|
|
|
|
await db.transaction(async () => {
|
|
const upd = await db.run(
|
|
`UPDATE schaltungen
|
|
SET schaltungs_id = ?, eingabe_datum = ?, schaltungs_datum = ?, vorname = ?, nachname = ?,
|
|
firma = ?, rufnummer = ?, anbieter_id = ?, vo_id = ?, tarif_id = ?, hardware = ?,
|
|
hardware_ek = ?, kundenverkaufspreis_netto = ?
|
|
WHERE id = ?`,
|
|
[
|
|
b.schaltungs_id, b.eingabe_datum, b.schaltungs_datum, b.vorname, b.nachname,
|
|
b.firma || null, rufnummerValue, b.anbieter_id, b.vo_id || null, b.tarif_id, b.hardware || null,
|
|
b.hardware_ek || null, b.kundenverkaufspreis_netto || null, id,
|
|
]
|
|
);
|
|
if (upd.changes === 0) {
|
|
const e = new Error('Schaltung nicht gefunden');
|
|
e.status = 404;
|
|
throw e;
|
|
}
|
|
await db.run('DELETE FROM schaltungen_tarifoptionen WHERE schaltung_id = ?', [id]);
|
|
for (const tarifoptionId of tarifoptionen) {
|
|
await db.run(
|
|
'INSERT INTO schaltungen_tarifoptionen (schaltung_id, tarifoption_id) VALUES (?, ?)',
|
|
[id, tarifoptionId]
|
|
);
|
|
}
|
|
});
|
|
res.json({ message: 'Schaltung erfolgreich aktualisiert' });
|
|
}));
|
|
|
|
router.delete('/:id', asyncHandler(async (req, res) => {
|
|
const { id } = req.params;
|
|
const { loeschgrund } = req.body || {};
|
|
if (!loeschgrund || !loeschgrund.trim()) {
|
|
return res.status(400).json({ error: 'Löschgrund ist erforderlich' });
|
|
}
|
|
|
|
const schaltung = await db.get(
|
|
`SELECT s.*, a.name AS anbieter_name, t.name AS tarif_name
|
|
FROM schaltungen s
|
|
JOIN anbieter a ON s.anbieter_id = a.id
|
|
JOIN tarife t ON s.tarif_id = t.id
|
|
WHERE s.id = ?`,
|
|
[id]
|
|
);
|
|
if (!schaltung) return res.status(404).json({ error: 'Schaltung nicht gefunden' });
|
|
|
|
const documents = await db.all('SELECT filepath FROM dokumente WHERE schaltung_id = ?', [id]);
|
|
const { safeUploadPath } = req.app.locals;
|
|
const fs = require('fs');
|
|
|
|
await db.transaction(async () => {
|
|
await db.run(
|
|
`INSERT INTO geloeschte_schaltungen (
|
|
original_id, schaltungs_id, eingabe_datum, schaltungs_datum, vorname, nachname,
|
|
firma, rufnummer, anbieter_id, tarif_id, hardware, anbieter_name, tarif_name, loeschgrund, geloescht_von
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
schaltung.id, schaltung.schaltungs_id, schaltung.eingabe_datum, schaltung.schaltungs_datum,
|
|
schaltung.vorname, schaltung.nachname, schaltung.firma, schaltung.rufnummer,
|
|
schaltung.anbieter_id, schaltung.tarif_id, schaltung.hardware,
|
|
schaltung.anbieter_name, schaltung.tarif_name, loeschgrund.trim(),
|
|
req.session && req.session.username ? req.session.username : 'System',
|
|
]
|
|
);
|
|
// Existing DBs may lack ON DELETE CASCADE on dokumente — delete explicitly.
|
|
await db.run('DELETE FROM dokumente WHERE schaltung_id = ?', [id]);
|
|
await db.run('DELETE FROM schaltungen WHERE id = ?', [id]);
|
|
});
|
|
|
|
// Best-effort file cleanup, outside the transaction.
|
|
let deletedFiles = 0;
|
|
for (const doc of documents) {
|
|
const filePath = safeUploadPath(doc.filepath);
|
|
if (!filePath) {
|
|
console.error(`Skipping file delete - invalid path: ${doc.filepath}`);
|
|
continue;
|
|
}
|
|
try {
|
|
fs.unlinkSync(filePath);
|
|
deletedFiles++;
|
|
} catch (e) {
|
|
console.error(`Error deleting ${filePath}:`, e.message);
|
|
}
|
|
}
|
|
|
|
res.json({
|
|
message: 'Schaltung erfolgreich gelöscht und archiviert',
|
|
loeschgrund: loeschgrund.trim(),
|
|
deleted_files: deletedFiles,
|
|
});
|
|
}));
|
|
|
|
// Tarifoptionen linked to a specific schaltung
|
|
router.get('/:id/tarifoptionen', asyncHandler(async (req, res) => {
|
|
const { id } = req.params;
|
|
const rows = await db.all(
|
|
`SELECT t.*, st.id AS schaltung_tarifoption_id
|
|
FROM tarifoptionen t
|
|
JOIN schaltungen_tarifoptionen st ON t.id = st.tarifoption_id
|
|
WHERE st.schaltung_id = ?
|
|
ORDER BY t.name`,
|
|
[id]
|
|
);
|
|
res.json(rows);
|
|
}));
|
|
|
|
router.post('/:id/tarifoptionen', asyncHandler(async (req, res) => {
|
|
const schaltung_id = req.params.id;
|
|
const { tarifoption_id } = req.body || {};
|
|
if (!tarifoption_id) return res.status(400).json({ error: 'tarifoption_id ist erforderlich' });
|
|
try {
|
|
const result = await db.run(
|
|
'INSERT INTO schaltungen_tarifoptionen (schaltung_id, tarifoption_id) VALUES (?, ?)',
|
|
[schaltung_id, tarifoption_id]
|
|
);
|
|
res.json({ id: result.lastID, message: 'Tarifoption erfolgreich hinzugefügt' });
|
|
} catch (err) {
|
|
if (/UNIQUE/.test(err.message)) {
|
|
return res.status(400).json({ error: 'Tarifoption bereits hinzugefügt' });
|
|
}
|
|
throw err;
|
|
}
|
|
}));
|
|
|
|
router.delete('/:schaltung_id/tarifoptionen/:tarifoption_id', asyncHandler(async (req, res) => {
|
|
const { schaltung_id, tarifoption_id } = req.params;
|
|
const result = await db.run(
|
|
'DELETE FROM schaltungen_tarifoptionen WHERE schaltung_id = ? AND tarifoption_id = ?',
|
|
[schaltung_id, tarifoption_id]
|
|
);
|
|
if (result.changes === 0) return res.status(404).json({ error: 'Verknüpfung nicht gefunden' });
|
|
res.json({ message: 'Tarifoption erfolgreich entfernt' });
|
|
}));
|
|
|
|
module.exports = router;
|