307 lines
No EOL
11 KiB
JavaScript
307 lines
No EOL
11 KiB
JavaScript
/**
|
|
* mandates.js
|
|
* Mandanten-Modul mit dem generischen Entitätsansatz
|
|
* Angepasst an das überarbeitete Backend und apiCalls.js
|
|
*/
|
|
|
|
import api from '../shared/apiCalls.js';
|
|
|
|
/**
|
|
* Initialisierungsfunktion für das Mandanten-Modul
|
|
* @param {Object} globalState - Globaler Zustand der Anwendung
|
|
*/
|
|
function initMandatesModule(globalState) {
|
|
// Prüfen, ob der Benutzer die erforderlichen Rechte hat (nur SysAdmin)
|
|
if (!globalState.user || !globalState.user.isSysAdmin) {
|
|
// Mandanten-Sektion ausblenden oder Hinweis anzeigen
|
|
const mandatesView = document.getElementById('mandates-view');
|
|
if (mandatesView) {
|
|
mandatesView.innerHTML = `
|
|
<h2>Mandantenverwaltung</h2>
|
|
<div class="card">
|
|
<div class="alert alert-warning">
|
|
<i class="fas fa-exclamation-triangle"></i>
|
|
Sie benötigen SysAdmin-Rechte, um Mandanten zu verwalten.
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Generisches Entitätsmodul für Mandanten initialisieren
|
|
window.genericEntityModule.init(globalState, {
|
|
entityType: 'mandate',
|
|
apiEndpoint: {
|
|
get: api.getMandates, // API-Funktion zum Abrufen aller Mandanten
|
|
create: api.createMandate, // API-Funktion zum Erstellen eines Mandanten
|
|
update: api.updateMandate, // API-Funktion zum Aktualisieren eines Mandanten
|
|
delete: api.deleteMandate // API-Funktion zum Löschen eines Mandanten
|
|
},
|
|
listContainerId: 'mandates-list-container',
|
|
addButtonId: 'add-mandate-btn',
|
|
|
|
// Benutzerdefinierte Render-Funktion für Mandanten
|
|
renderItem: function(mandate) {
|
|
// Status-Badge (aktiv/deaktiviert)
|
|
const statusBadge = mandate.disabled ?
|
|
'<span class="badge badge-red">Deaktiviert</span>' :
|
|
'<span class="badge badge-green">Aktiv</span>';
|
|
|
|
return `
|
|
<div class="list-header">
|
|
<div>
|
|
<h4>${mandate.name || 'Unbenannter Mandant'}</h4>
|
|
</div>
|
|
<div class="mandate-badges">
|
|
${statusBadge}
|
|
<span class="badge badge-blue">Sprache: ${mandate.language || 'de'}</span>
|
|
</div>
|
|
</div>
|
|
<div class="list-body">
|
|
${mandate.description ? `<div class="mandate-description">${mandate.description}</div>` : ''}
|
|
${mandate.contactEmail ? `<div class="mandate-email">${mandate.contactEmail}</div>` : ''}
|
|
${mandate.createdAt ? `Erstellt am: ${new Date(mandate.createdAt).toLocaleString()}` : ''}
|
|
</div>
|
|
`;
|
|
},
|
|
|
|
// Behandlung spezieller Attributformate
|
|
transformFormData: function(formData) {
|
|
// Boolean-Wert für "disabled" korrekt umwandeln
|
|
if (formData.hasOwnProperty('disabled') && typeof formData.disabled === 'string') {
|
|
formData.disabled = formData.disabled.toLowerCase() === 'true';
|
|
}
|
|
|
|
return formData;
|
|
},
|
|
|
|
// Zusätzliche Aktionen für Mandanten
|
|
getItemActions: function(mandate) {
|
|
const actions = [];
|
|
|
|
// Benutzer anzeigen Aktion
|
|
actions.push({
|
|
text: 'Benutzer anzeigen',
|
|
className: 'entity-view-btn',
|
|
html: '<i class="fas fa-users"></i>',
|
|
handler: function(mandate) {
|
|
showMandateUsers(mandate);
|
|
}
|
|
});
|
|
|
|
// Mandant aktivieren/deaktivieren
|
|
if (mandate.disabled) {
|
|
actions.push({
|
|
text: 'Aktivieren',
|
|
className: 'entity-activate-btn',
|
|
html: '<i class="fas fa-check-circle"></i>',
|
|
handler: function(mandate) {
|
|
toggleMandateStatus(mandate, false);
|
|
}
|
|
});
|
|
} else {
|
|
actions.push({
|
|
text: 'Deaktivieren',
|
|
className: 'entity-deactivate-btn',
|
|
html: '<i class="fas fa-ban"></i>',
|
|
handler: function(mandate) {
|
|
toggleMandateStatus(mandate, true);
|
|
}
|
|
});
|
|
}
|
|
|
|
return actions;
|
|
},
|
|
|
|
// Callbacks
|
|
onItemCreated: function(mandate) {
|
|
showToast(`Mandant "${mandate.name}" erstellt`);
|
|
},
|
|
|
|
onItemUpdated: function(mandate) {
|
|
showToast(`Mandant "${mandate.name}" aktualisiert`);
|
|
},
|
|
|
|
onItemDeleted: function(mandateId) {
|
|
showToast(`Mandant gelöscht`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Mandant aktivieren/deaktivieren
|
|
* @param {Object} mandate - Der Mandant
|
|
* @param {boolean} disabled - Neuer Status (true = deaktiviert, false = aktiviert)
|
|
*/
|
|
async function toggleMandateStatus(mandate, disabled) {
|
|
try {
|
|
// Status ändern via API
|
|
const response = await api.updateMandate(mandate.id, { disabled });
|
|
|
|
if (response) {
|
|
showToast(`Mandant "${mandate.name}" ${disabled ? 'deaktiviert' : 'aktiviert'}`);
|
|
|
|
// Liste neu laden
|
|
if (window.genericEntityModule) {
|
|
window.genericEntityModule.loadAndRenderItems();
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`Fehler beim Ändern des Mandantenstatus:`, error);
|
|
showError(`Fehler beim Ändern des Mandantenstatus`, error.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Zeigt alle Benutzer eines Mandanten an
|
|
* @param {Object} mandate - Der Mandant
|
|
*/
|
|
async function showMandateUsers(mandate) {
|
|
try {
|
|
// Benutzer für den Mandanten abrufen
|
|
const users = await api.getUsers(mandate.id);
|
|
|
|
if (!users || users.length === 0) {
|
|
showToast(`Keine Benutzer für Mandant "${mandate.name}" gefunden.`);
|
|
return;
|
|
}
|
|
|
|
// Modal mit Benutzerliste erstellen
|
|
let userListHTML = `
|
|
<h3>Benutzer von Mandant "${mandate.name}"</h3>
|
|
<div class="mandate-users-list">
|
|
<table class="users-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Benutzername</th>
|
|
<th>Name</th>
|
|
<th>E-Mail</th>
|
|
<th>Rolle</th>
|
|
<th>Status</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
`;
|
|
|
|
users.forEach(user => {
|
|
// Status und Rolle ermitteln
|
|
const status = user.disabled ?
|
|
'<span class="badge badge-red">Deaktiviert</span>' :
|
|
'<span class="badge badge-green">Aktiv</span>';
|
|
|
|
let role = '<span class="badge badge-gray">User</span>';
|
|
if (user.privilege === 'admin') {
|
|
role = '<span class="badge badge-blue">Admin</span>';
|
|
} else if (user.privilege === 'sysadmin') {
|
|
role = '<span class="badge badge-purple">SysAdmin</span>';
|
|
}
|
|
|
|
userListHTML += `
|
|
<tr>
|
|
<td>${user.username}</td>
|
|
<td>${user.fullName || '-'}</td>
|
|
<td>${user.email || '-'}</td>
|
|
<td>${role}</td>
|
|
<td>${status}</td>
|
|
</tr>
|
|
`;
|
|
});
|
|
|
|
userListHTML += `
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
`;
|
|
|
|
// Modal anzeigen
|
|
if (window.appUtils && window.appUtils.ui && window.appUtils.ui.createModal) {
|
|
window.appUtils.ui.createModal({
|
|
title: `Benutzer von Mandant "${mandate.name}"`,
|
|
content: userListHTML,
|
|
submitText: 'Schließen',
|
|
cancelText: null, // Kein Abbrechen-Button
|
|
onSubmit: () => {
|
|
// Einfach schließen
|
|
}
|
|
});
|
|
} else {
|
|
// Fallback: Alert mit Benutzerzahl
|
|
showToast(`Der Mandant "${mandate.name}" hat ${users.length} Benutzer.`);
|
|
}
|
|
} catch (error) {
|
|
console.error(`Fehler beim Abrufen der Mandantenbenutzer:`, error);
|
|
showError(`Fehler beim Abrufen der Benutzer`, error.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Erstellt einen neuen Mandanten mit Default-Werten
|
|
* @param {Object} globalStateObj - Globaler Zustand der Anwendung
|
|
* @returns {Promise<Object>} - Der erstellte Mandant
|
|
*/
|
|
async function createDefaultMandate(globalStateObj) {
|
|
try {
|
|
// Default-Mandant erstellen
|
|
const defaultMandate = {
|
|
name: "Neuer Mandant",
|
|
description: "Beschreibung des Mandanten",
|
|
language: "de",
|
|
contactEmail: ""
|
|
};
|
|
|
|
// An die API senden
|
|
const newMandate = await api.createMandate(defaultMandate);
|
|
|
|
// UI aktualisieren
|
|
if (window.genericEntityModule) {
|
|
window.genericEntityModule.loadAndRenderItems();
|
|
}
|
|
|
|
return newMandate;
|
|
} catch (error) {
|
|
console.error("Fehler beim Erstellen des Default-Mandanten:", error);
|
|
showError("Fehler beim Erstellen", error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Zeigt eine Toast-Nachricht an
|
|
* @param {string} message - Anzuzeigende Nachricht
|
|
*/
|
|
function showToast(message) {
|
|
if (window.appUtils && window.appUtils.ui && window.appUtils.ui.showToast) {
|
|
window.appUtils.ui.showToast(message);
|
|
} else if (window.globalUtils && window.globalUtils.showError) {
|
|
window.globalUtils.showError("Info", message);
|
|
} else {
|
|
console.log(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Zeigt eine Fehlermeldung an
|
|
* @param {string} title - Titel der Fehlermeldung
|
|
* @param {string} message - Fehlermeldung
|
|
*/
|
|
function showError(title, message) {
|
|
if (window.appUtils && window.appUtils.ui && window.appUtils.ui.showError) {
|
|
window.appUtils.ui.showError(title, message);
|
|
} else if (window.globalUtils && window.globalUtils.showError) {
|
|
window.globalUtils.showError(title, message);
|
|
} else {
|
|
console.error(`${title}: ${message}`);
|
|
}
|
|
}
|
|
|
|
// Mandates-Modul-Schnittstelle definieren und exportieren
|
|
window.initMandatesModule = initMandatesModule;
|
|
|
|
// Export für ES Module
|
|
export {
|
|
initMandatesModule,
|
|
toggleMandateStatus,
|
|
showMandateUsers,
|
|
createDefaultMandate
|
|
}; |