"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getContracts = getContracts; exports.getContract = getContract; exports.createContract = createContract; exports.updateContract = updateContract; exports.deleteContract = deleteContract; exports.createFollowUp = createFollowUp; exports.getContractPassword = getContractPassword; exports.getSimCardCredentials = getSimCardCredentials; exports.getInternetCredentials = getInternetCredentials; exports.getSipCredentials = getSipCredentials; exports.getCockpit = getCockpit; const contractService = __importStar(require("../services/contract.service.js")); const contractCockpitService = __importStar(require("../services/contractCockpit.service.js")); async function getContracts(req, res) { try { const { customerId, type, status, search, page, limit, tree } = req.query; // Baumstruktur für Kundenansicht if (tree === 'true' && customerId) { const treeData = await contractService.getContractTreeForCustomer(parseInt(customerId)); res.json({ success: true, data: treeData }); return; } // Für Kundenportal-Benutzer: nur eigene + vertretene Kunden-Verträge anzeigen let customerIds; if (req.user?.isCustomerPortal && req.user.customerId) { // Eigene Customer-ID + alle vertretenen Kunden-IDs customerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])]; } const result = await contractService.getAllContracts({ customerId: customerId ? parseInt(customerId) : undefined, customerIds, // Wird nur für Kundenportal-Benutzer gesetzt type: type, status: status, search: search, page: page ? parseInt(page) : undefined, limit: limit ? parseInt(limit) : undefined, }); res.json({ success: true, data: result.contracts, pagination: result.pagination, }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Laden der Verträge', }); } } async function getContract(req, res) { try { const contract = await contractService.getContractById(parseInt(req.params.id)); if (!contract) { res.status(404).json({ success: false, error: 'Vertrag nicht gefunden', }); return; } // Für Kundenportal-Benutzer: Zugriff nur auf eigene + vertretene Kunden-Verträge if (req.user?.isCustomerPortal && req.user.customerId) { const allowedCustomerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])]; if (!allowedCustomerIds.includes(contract.customerId)) { res.status(403).json({ success: false, error: 'Kein Zugriff auf diesen Vertrag', }); return; } } res.json({ success: true, data: contract }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Laden des Vertrags', }); } } async function createContract(req, res) { try { const contract = await contractService.createContract(req.body); res.status(201).json({ success: true, data: contract }); } catch (error) { res.status(400).json({ success: false, error: error instanceof Error ? error.message : 'Fehler beim Erstellen des Vertrags', }); } } async function updateContract(req, res) { try { const contract = await contractService.updateContract(parseInt(req.params.id), req.body); res.json({ success: true, data: contract }); } catch (error) { res.status(400).json({ success: false, error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren des Vertrags', }); } } async function deleteContract(req, res) { try { await contractService.deleteContract(parseInt(req.params.id)); res.json({ success: true, message: 'Vertrag gelöscht' }); } catch (error) { res.status(400).json({ success: false, error: error instanceof Error ? error.message : 'Fehler beim Löschen des Vertrags', }); } } async function createFollowUp(req, res) { try { const contract = await contractService.createFollowUpContract(parseInt(req.params.id)); res.status(201).json({ success: true, data: contract }); } catch (error) { res.status(400).json({ success: false, error: error instanceof Error ? error.message : 'Fehler beim Erstellen des Folgevertrags', }); } } async function getContractPassword(req, res) { try { const password = await contractService.getContractPassword(parseInt(req.params.id)); if (password === null) { res.status(404).json({ success: false, error: 'Kein Passwort hinterlegt', }); return; } res.json({ success: true, data: { password } }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Entschlüsseln des Passworts', }); } } async function getSimCardCredentials(req, res) { try { const credentials = await contractService.getSimCardCredentials(parseInt(req.params.simCardId)); res.json({ success: true, data: credentials }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Entschlüsseln der SIM-Karten-Daten', }); } } async function getInternetCredentials(req, res) { try { const credentials = await contractService.getInternetCredentials(parseInt(req.params.id)); res.json({ success: true, data: credentials }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Entschlüsseln des Internet-Passworts', }); } } async function getSipCredentials(req, res) { try { const credentials = await contractService.getSipCredentials(parseInt(req.params.phoneNumberId)); res.json({ success: true, data: credentials }); } catch (error) { res.status(500).json({ success: false, error: 'Fehler beim Entschlüsseln des SIP-Passworts', }); } } // ==================== VERTRAGS-COCKPIT ==================== async function getCockpit(req, res) { try { const cockpitData = await contractCockpitService.getCockpitData(); res.json({ success: true, data: cockpitData }); } catch (error) { console.error('Cockpit error:', error); res.status(500).json({ success: false, error: 'Fehler beim Laden des Vertrags-Cockpits', }); } } //# sourceMappingURL=contract.controller.js.map