first commit
This commit is contained in:
+449
@@ -0,0 +1,449 @@
|
||||
"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.getAllTasks = getAllTasks;
|
||||
exports.getTaskStats = getTaskStats;
|
||||
exports.getTasks = getTasks;
|
||||
exports.createTask = createTask;
|
||||
exports.createSupportTicket = createSupportTicket;
|
||||
exports.updateTask = updateTask;
|
||||
exports.completeTask = completeTask;
|
||||
exports.reopenTask = reopenTask;
|
||||
exports.deleteTask = deleteTask;
|
||||
exports.createSubtask = createSubtask;
|
||||
exports.createCustomerReply = createCustomerReply;
|
||||
exports.updateSubtask = updateSubtask;
|
||||
exports.completeSubtask = completeSubtask;
|
||||
exports.reopenSubtask = reopenSubtask;
|
||||
exports.deleteSubtask = deleteSubtask;
|
||||
const contractTaskService = __importStar(require("../services/contractTask.service.js"));
|
||||
const contractService = __importStar(require("../services/contract.service.js"));
|
||||
const customerService = __importStar(require("../services/customer.service.js"));
|
||||
const appSettingService = __importStar(require("../services/appSetting.service.js"));
|
||||
// ==================== ALL TASKS (Dashboard & Task List) ====================
|
||||
async function getAllTasks(req, res) {
|
||||
try {
|
||||
const { status, customerId } = req.query;
|
||||
// Für Kundenportal: Filter auf erlaubte Kunden
|
||||
let customerPortalCustomerIds;
|
||||
let customerPortalEmails;
|
||||
if (req.user?.isCustomerPortal && req.user.customerId) {
|
||||
customerPortalCustomerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])];
|
||||
const customers = await customerService.getCustomersByIds(customerPortalCustomerIds);
|
||||
customerPortalEmails = customers
|
||||
.map((c) => c.portalEmail)
|
||||
.filter((email) => !!email);
|
||||
}
|
||||
const tasks = await contractTaskService.getAllTasks({
|
||||
status: status,
|
||||
customerId: customerId ? parseInt(customerId) : undefined,
|
||||
customerPortalCustomerIds,
|
||||
customerPortalEmails,
|
||||
});
|
||||
res.json({ success: true, data: tasks });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Fehler beim Laden der Aufgaben',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function getTaskStats(req, res) {
|
||||
try {
|
||||
// Für Kundenportal: Filter auf erlaubte Kunden
|
||||
let customerPortalCustomerIds;
|
||||
let customerPortalEmails;
|
||||
if (req.user?.isCustomerPortal && req.user.customerId) {
|
||||
customerPortalCustomerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])];
|
||||
const customers = await customerService.getCustomersByIds(customerPortalCustomerIds);
|
||||
customerPortalEmails = customers
|
||||
.map((c) => c.portalEmail)
|
||||
.filter((email) => !!email);
|
||||
}
|
||||
const stats = await contractTaskService.getTaskStats({
|
||||
customerPortalCustomerIds,
|
||||
customerPortalEmails,
|
||||
});
|
||||
res.json({ success: true, data: stats });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Fehler beim Laden der Statistik',
|
||||
});
|
||||
}
|
||||
}
|
||||
// ==================== TASKS BY CONTRACT ====================
|
||||
async function getTasks(req, res) {
|
||||
try {
|
||||
const contractId = parseInt(req.params.contractId);
|
||||
const { status } = req.query;
|
||||
// Prüfe Zugriff auf den Vertrag
|
||||
const contract = await contractService.getContractById(contractId);
|
||||
if (!contract) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Vertrag nicht gefunden',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Für Kundenportal: Zugriffsprüfung
|
||||
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;
|
||||
}
|
||||
}
|
||||
// Für Kundenportal-Benutzer: Lade E-Mails der erlaubten Kunden
|
||||
let customerPortalEmails;
|
||||
if (req.user?.isCustomerPortal && req.user.customerId) {
|
||||
const allowedCustomerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])];
|
||||
const customers = await customerService.getCustomersByIds(allowedCustomerIds);
|
||||
customerPortalEmails = customers
|
||||
.map((c) => c.portalEmail)
|
||||
.filter((email) => !!email);
|
||||
}
|
||||
const tasks = await contractTaskService.getTasksByContract({
|
||||
contractId,
|
||||
status: status,
|
||||
customerPortalEmails,
|
||||
});
|
||||
res.json({ success: true, data: tasks });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Fehler beim Laden der Aufgaben',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function createTask(req, res) {
|
||||
try {
|
||||
const contractId = parseInt(req.params.contractId);
|
||||
const { title, description, visibleInPortal } = req.body;
|
||||
if (!title) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Titel ist erforderlich',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const createdBy = req.user?.email;
|
||||
// Für Kundenportal-Benutzer: visibleInPortal wird automatisch auf true gesetzt
|
||||
const finalVisibleInPortal = req.user?.isCustomerPortal ? true : visibleInPortal;
|
||||
const task = await contractTaskService.createTask({
|
||||
contractId,
|
||||
title,
|
||||
description,
|
||||
visibleInPortal: finalVisibleInPortal,
|
||||
createdBy,
|
||||
});
|
||||
res.status(201).json({ success: true, data: task });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Erstellen der Aufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
// Für Kundenportal-Benutzer: Support-Anfrage erstellen (ohne contracts:update Permission)
|
||||
async function createSupportTicket(req, res) {
|
||||
try {
|
||||
// Prüfe ob Support-Tickets aktiviert sind
|
||||
const supportEnabled = await appSettingService.getSettingBool('customerSupportTicketsEnabled');
|
||||
if (!supportEnabled) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
error: 'Support-Anfragen sind nicht aktiviert',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const contractId = parseInt(req.params.contractId);
|
||||
const { title, description } = req.body;
|
||||
if (!title) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Titel ist erforderlich',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Prüfe Zugriff auf den Vertrag
|
||||
const contract = await contractService.getContractById(contractId);
|
||||
if (!contract) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Vertrag nicht gefunden',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Zugriffsprüfung für Kundenportal
|
||||
if (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;
|
||||
}
|
||||
}
|
||||
const createdBy = req.user?.email;
|
||||
const task = await contractTaskService.createTask({
|
||||
contractId,
|
||||
title,
|
||||
description,
|
||||
visibleInPortal: true, // Immer sichtbar im Portal
|
||||
createdBy,
|
||||
});
|
||||
res.status(201).json({ success: true, data: task });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Erstellen der Support-Anfrage',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function updateTask(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
const { title, description, visibleInPortal } = req.body;
|
||||
const task = await contractTaskService.updateTask(taskId, {
|
||||
title,
|
||||
description,
|
||||
visibleInPortal,
|
||||
});
|
||||
res.json({ success: true, data: task });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren der Aufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function completeTask(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
const task = await contractTaskService.completeTask(taskId);
|
||||
res.json({ success: true, data: task });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Abschließen der Aufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function reopenTask(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
const task = await contractTaskService.reopenTask(taskId);
|
||||
res.json({ success: true, data: task });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Wiedereröffnen der Aufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function deleteTask(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
await contractTaskService.deleteTask(taskId);
|
||||
res.json({ success: true, message: 'Aufgabe gelöscht' });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Löschen der Aufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
// ==================== SUBTASKS ====================
|
||||
async function createSubtask(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
const { title } = req.body;
|
||||
if (!title) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Titel ist erforderlich',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const createdBy = req.user?.email;
|
||||
const subtask = await contractTaskService.createSubtask({
|
||||
taskId,
|
||||
title,
|
||||
createdBy,
|
||||
});
|
||||
res.status(201).json({ success: true, data: subtask });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Erstellen der Unteraufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
// Kundenportal: Antwort auf eigenes Ticket erstellen
|
||||
async function createCustomerReply(req, res) {
|
||||
try {
|
||||
const taskId = parseInt(req.params.taskId);
|
||||
const { title } = req.body;
|
||||
if (!title) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Antwort ist erforderlich',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Hole den Task
|
||||
const task = await contractTaskService.getTaskById(taskId);
|
||||
if (!task) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Anfrage nicht gefunden',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Prüfe ob der Kunde berechtigt ist (eigenes Ticket oder freigegebener Kunde)
|
||||
if (req.user?.isCustomerPortal && req.user.customerId) {
|
||||
const allowedCustomerIds = [req.user.customerId, ...(req.user.representedCustomerIds || [])];
|
||||
const customers = await customerService.getCustomersByIds(allowedCustomerIds);
|
||||
const allowedEmails = customers
|
||||
.map((c) => c.portalEmail)
|
||||
.filter((email) => !!email);
|
||||
// Task muss entweder visibleInPortal sein ODER vom Kunden erstellt worden sein
|
||||
const isOwnTask = task.createdBy && allowedEmails.includes(task.createdBy);
|
||||
if (!task.visibleInPortal && !isOwnTask) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
error: 'Kein Zugriff auf diese Anfrage',
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
error: 'Nur für Kundenportal-Benutzer',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const createdBy = req.user?.email;
|
||||
const subtask = await contractTaskService.createSubtask({
|
||||
taskId,
|
||||
title,
|
||||
createdBy,
|
||||
});
|
||||
res.status(201).json({ success: true, data: subtask });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Erstellen der Antwort',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function updateSubtask(req, res) {
|
||||
try {
|
||||
const subtaskId = parseInt(req.params.subtaskId);
|
||||
const { title } = req.body;
|
||||
if (!title) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Titel ist erforderlich',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const subtask = await contractTaskService.updateSubtask(subtaskId, { title });
|
||||
res.json({ success: true, data: subtask });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren der Unteraufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function completeSubtask(req, res) {
|
||||
try {
|
||||
const subtaskId = parseInt(req.params.subtaskId);
|
||||
const subtask = await contractTaskService.completeSubtask(subtaskId);
|
||||
res.json({ success: true, data: subtask });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Abschließen der Unteraufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function reopenSubtask(req, res) {
|
||||
try {
|
||||
const subtaskId = parseInt(req.params.subtaskId);
|
||||
const subtask = await contractTaskService.reopenSubtask(subtaskId);
|
||||
res.json({ success: true, data: subtask });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Wiedereröffnen der Unteraufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
async function deleteSubtask(req, res) {
|
||||
try {
|
||||
const subtaskId = parseInt(req.params.subtaskId);
|
||||
await contractTaskService.deleteSubtask(subtaskId);
|
||||
res.json({ success: true, message: 'Unteraufgabe gelöscht' });
|
||||
}
|
||||
catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Löschen der Unteraufgabe',
|
||||
});
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=contractTask.controller.js.map
|
||||
Reference in New Issue
Block a user