feat: rebuild admin account management and ticket assignment
This commit is contained in:
1 parent
27b365622e
commit
f76b738f5f
11 files changed
+1239
-1035
No files matched your search
+54
-27
@@ -195,6 +195,54 @@ export function useWorkorders(filters = {}) {
|
||||
fetchWorkorders()
|
||||
}, [fetchWorkorders])
|
||||
|
||||
const resolveAutomaticTicketFields = async (requestedAssignee = '') => {
|
||||
const [allTicketsResponse, employeesResponse] = await Promise.all([
|
||||
databases.listDocuments(
|
||||
DATABASE_ID,
|
||||
COLLECTIONS.WORKORDERS,
|
||||
[Query.limit(5000)]
|
||||
),
|
||||
databases.listDocuments(
|
||||
DATABASE_ID,
|
||||
COLLECTIONS.EMPLOYEES,
|
||||
[Query.orderAsc('displayName'), Query.limit(500)]
|
||||
),
|
||||
])
|
||||
|
||||
const allTickets = allTicketsResponse.documents || []
|
||||
const employees = employeesResponse.documents || []
|
||||
const maxWoid = allTickets.reduce((highest, ticket) => {
|
||||
const numericId = Number.parseInt(ticket.woid, 10)
|
||||
return Number.isFinite(numericId) ? Math.max(highest, numericId) : highest
|
||||
}, 9999)
|
||||
|
||||
let assignee = requestedAssignee
|
||||
? employees.find((employee) => employee.userId === requestedAssignee)
|
||||
: null
|
||||
|
||||
if (!assignee && employees.length > 0) {
|
||||
const finishedStatuses = new Set(['Closed', 'Completed', 'Cancelled', 'Done'])
|
||||
const activeCounts = new Map(employees.map((employee) => [employee.userId, 0]))
|
||||
for (const ticket of allTickets) {
|
||||
if (!ticket.assignedTo || finishedStatuses.has(ticket.status)) continue
|
||||
if (activeCounts.has(ticket.assignedTo)) {
|
||||
activeCounts.set(ticket.assignedTo, activeCounts.get(ticket.assignedTo) + 1)
|
||||
}
|
||||
}
|
||||
assignee = [...employees].sort((left, right) => {
|
||||
const loadDifference = activeCounts.get(left.userId) - activeCounts.get(right.userId)
|
||||
if (loadDifference !== 0) return loadDifference
|
||||
return (left.displayName || '').localeCompare(right.displayName || '', 'de')
|
||||
})[0]
|
||||
}
|
||||
|
||||
return {
|
||||
woid: String(maxWoid + 1),
|
||||
assignedTo: assignee?.userId || '',
|
||||
assignedName: assignee?.displayName || '',
|
||||
}
|
||||
}
|
||||
|
||||
const createWorkorder = async (data) => {
|
||||
if (DEMO_MODE) {
|
||||
// Finde höchste WOID und +1
|
||||
@@ -214,38 +262,16 @@ export function useWorkorders(filters = {}) {
|
||||
return { success: false, error: 'Das Feld "Topic" ist erforderlich.' }
|
||||
}
|
||||
|
||||
// Status-Automatik: Wenn Mitarbeiter zugewiesen → Status = "Assigned", sonst "Open"
|
||||
const autoStatus = (data.assignedTo && data.assignedTo !== '') ? 'Assigned' : 'Open'
|
||||
|
||||
// Generiere sequentielle 5-stellige WOID (wie im Original-System)
|
||||
const generateWOID = () => {
|
||||
// Finde die höchste bestehende WOID
|
||||
if (workorders.length === 0) {
|
||||
return '10000'; // Starte bei 10000 wenn keine Tickets existieren
|
||||
}
|
||||
|
||||
const maxWoid = Math.max(
|
||||
...workorders
|
||||
.map(wo => parseInt(wo.woid))
|
||||
.filter(woid => !isNaN(woid) && woid > 0)
|
||||
);
|
||||
|
||||
// Wenn keine gültige WOID gefunden wurde, starte bei 10000
|
||||
if (maxWoid === -Infinity || isNaN(maxWoid)) {
|
||||
return '10000';
|
||||
}
|
||||
|
||||
// Gib die nächste Nummer zurück (sequentiell)
|
||||
return (maxWoid + 1).toString();
|
||||
}
|
||||
const automatic = await resolveAutomaticTicketFields(data.assignedTo)
|
||||
const autoStatus = automatic.assignedTo ? 'Assigned' : 'Open'
|
||||
|
||||
// Bereite Daten für Appwrite vor
|
||||
const workorderData = {
|
||||
// Required fields
|
||||
topic: data.topic.trim(),
|
||||
status: data.status || autoStatus, // Verwende übergebenen Status oder automatischen Status
|
||||
status: autoStatus,
|
||||
priority: typeof data.priority === 'number' ? data.priority : parseInt(data.priority) || 1,
|
||||
woid: generateWOID(), // 5-stellige Zahl
|
||||
woid: automatic.woid,
|
||||
|
||||
// Optional fields - nur senden wenn vorhanden
|
||||
type: data.type || '',
|
||||
@@ -253,7 +279,8 @@ export function useWorkorders(filters = {}) {
|
||||
responseLevel: data.responseLevel || '',
|
||||
serviceType: data.serviceType || 'Remote',
|
||||
customerId: data.customerId || '',
|
||||
assignedTo: data.assignedTo || '', // Zugewiesener Mitarbeiter
|
||||
assignedTo: automatic.assignedTo,
|
||||
assignedName: automatic.assignedName,
|
||||
requestedBy: data.requestedBy || '',
|
||||
requestedFor: data.requestedFor || '',
|
||||
startDate: data.startDate || '',
|
||||
|
||||
Reference in new issue
Block a user