rework internal order system

This commit is contained in:
Matthias Hochmeister
2026-03-24 08:11:32 +01:00
parent 742c37b8de
commit 99f02b8425
11 changed files with 792 additions and 397 deletions

View File

@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import ausruestungsanfrageService from '../services/ausruestungsanfrage.service';
import notificationService from '../services/notification.service';
import { permissionService } from '../services/permission.service';
import logger from '../utils/logger';
class AusruestungsanfrageController {
@@ -129,9 +130,11 @@ class AusruestungsanfrageController {
async createRequest(req: Request, res: Response): Promise<void> {
try {
const { items, notizen } = req.body as {
const { items, notizen, bezeichnung, fuer_benutzer_id } = req.body as {
items?: { artikel_id?: number; bezeichnung: string; menge: number; notizen?: string }[];
notizen?: string;
bezeichnung?: string;
fuer_benutzer_id?: string;
};
if (!items || items.length === 0) {
@@ -150,7 +153,19 @@ class AusruestungsanfrageController {
}
}
const request = await ausruestungsanfrageService.createRequest(req.user!.id, items, notizen);
// Determine anfrager: self or on behalf of another user
let anfragerId = req.user!.id;
if (fuer_benutzer_id && fuer_benutzer_id !== req.user!.id) {
const groups = req.user?.groups ?? [];
const canOrderForUser = groups.includes('dashboard_admin') || permissionService.hasPermission(groups, 'ausruestungsanfrage:order_for_user');
if (!canOrderForUser) {
res.status(403).json({ success: false, message: 'Keine Berechtigung für Bestellung im Auftrag' });
return;
}
anfragerId = fuer_benutzer_id;
}
const request = await ausruestungsanfrageService.createRequest(anfragerId, items, notizen, bezeichnung);
res.status(201).json({ success: true, data: request });
} catch (error) {
logger.error('AusruestungsanfrageController.createRequest error', { error });
@@ -158,6 +173,56 @@ class AusruestungsanfrageController {
}
}
async updateRequest(req: Request, res: Response): Promise<void> {
try {
const id = Number(req.params.id);
const { bezeichnung, notizen, items } = req.body as {
bezeichnung?: string;
notizen?: string;
items?: { artikel_id?: number; bezeichnung: string; menge: number; notizen?: string }[];
};
// Validate items if provided
if (items) {
if (items.length === 0) {
res.status(400).json({ success: false, message: 'Mindestens eine Position ist erforderlich' });
return;
}
for (const item of items) {
if (!item.bezeichnung || item.bezeichnung.trim().length === 0) {
res.status(400).json({ success: false, message: 'Bezeichnung ist für alle Positionen erforderlich' });
return;
}
if (!item.menge || item.menge < 1) {
res.status(400).json({ success: false, message: 'Menge muss mindestens 1 sein' });
return;
}
}
}
const existing = await ausruestungsanfrageService.getRequestById(id);
if (!existing) {
res.status(404).json({ success: false, message: 'Anfrage nicht gefunden' });
return;
}
// Check permission: owner + status=offen, OR ausruestungsanfrage:edit
const groups = req.user?.groups ?? [];
const canEditAny = groups.includes('dashboard_admin') || permissionService.hasPermission(groups, 'ausruestungsanfrage:edit');
const isOwner = existing.anfrager_id === req.user!.id;
if (!canEditAny && !(isOwner && existing.status === 'offen')) {
res.status(403).json({ success: false, message: 'Keine Berechtigung zum Bearbeiten dieser Anfrage' });
return;
}
const updated = await ausruestungsanfrageService.updateRequest(id, { bezeichnung, notizen, items });
res.status(200).json({ success: true, data: updated });
} catch (error) {
logger.error('AusruestungsanfrageController.updateRequest error', { error });
res.status(500).json({ success: false, message: 'Anfrage konnte nicht aktualisiert werden' });
}
}
async updateRequestStatus(req: Request, res: Response): Promise<void> {
try {
const id = Number(req.params.id);