Kostenpositionsspeicherung atomar ausführen

This commit is contained in:
gitea_admin committed 2026-09-20 12:20:57 +02:00
1 parent dc52003c01
commit f77ce71bd6
1 file changed
+15 -3
+15 -3
View File
@@ -43,9 +43,10 @@ if ($requestMethod === 'POST') {
echo json_encode(['ok' => true, 'occurrence_id' => (int) $occurrence['id']], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); echo json_encode(['ok' => true, 'occurrence_id' => (int) $occurrence['id']], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit; exit;
} }
$scope = (string) ($payload['scope'] ?? ''); $scope = (string) ($payload['scope'] ?? '');
$id = market_management_require_positive_id($payload['id'] ?? null, 'id'); $id = market_management_require_positive_id($payload['id'] ?? null, 'id');
$fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : []; $fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : [];
$costTransactionStarted = false;
if (($payload['action'] ?? '') === 'delete') { if (($payload['action'] ?? '') === 'delete') {
if (($payload['confirmation'] ?? '') !== 'DELETE') { if (($payload['confirmation'] ?? '') !== 'DELETE') {
throw new InvalidArgumentException('Die Löschbestätigung muss exakt DELETE lauten.'); throw new InvalidArgumentException('Die Löschbestätigung muss exakt DELETE lauten.');
@@ -104,6 +105,10 @@ if ($requestMethod === 'POST') {
if (!is_array($costItems)) { if (!is_array($costItems)) {
throw new InvalidArgumentException('Kostenpositionen sind ungültig.'); throw new InvalidArgumentException('Kostenpositionen sind ungültig.');
} }
if (!$pdo->inTransaction()) {
$pdo->beginTransaction();
$costTransactionStarted = true;
}
unset($fields['cost_items']); unset($fields['cost_items']);
$keptCostItemIds = array_values(array_filter(array_map( $keptCostItemIds = array_values(array_filter(array_map(
static fn (array $costItem): ?int => isset($costItem['id']) ? (int) $costItem['id'] : null, static fn (array $costItem): ?int => isset($costItem['id']) ? (int) $costItem['id'] : null,
@@ -137,6 +142,10 @@ if ($requestMethod === 'POST') {
} }
} }
market_management_update_occurrence($pdo, $id, $fields); market_management_update_occurrence($pdo, $id, $fields);
if ($costTransactionStarted) {
$pdo->commit();
$costTransactionStarted = false;
}
} }
} else { } else {
throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.'); throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.');
@@ -144,6 +153,9 @@ if ($requestMethod === 'POST') {
echo json_encode(['ok' => true]); echo json_encode(['ok' => true]);
exit; exit;
} catch (Throwable $error) { } catch (Throwable $error) {
if ($costTransactionStarted && $pdo->inTransaction()) {
$pdo->rollBack();
}
http_response_code(422); http_response_code(422);
echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit; exit;