Kostenpositionsspeicherung atomar ausführen
This commit is contained in:
1 parent
dc52003c01
commit
f77ce71bd6
1 file changed
+15
-3
@@ -43,9 +43,10 @@ if ($requestMethod === 'POST') {
|
||||
echo json_encode(['ok' => true, 'occurrence_id' => (int) $occurrence['id']], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
$scope = (string) ($payload['scope'] ?? '');
|
||||
$id = market_management_require_positive_id($payload['id'] ?? null, 'id');
|
||||
$fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : [];
|
||||
$scope = (string) ($payload['scope'] ?? '');
|
||||
$id = market_management_require_positive_id($payload['id'] ?? null, 'id');
|
||||
$fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : [];
|
||||
$costTransactionStarted = false;
|
||||
if (($payload['action'] ?? '') === 'delete') {
|
||||
if (($payload['confirmation'] ?? '') !== 'DELETE') {
|
||||
throw new InvalidArgumentException('Die Löschbestätigung muss exakt DELETE lauten.');
|
||||
@@ -104,6 +105,10 @@ if ($requestMethod === 'POST') {
|
||||
if (!is_array($costItems)) {
|
||||
throw new InvalidArgumentException('Kostenpositionen sind ungültig.');
|
||||
}
|
||||
if (!$pdo->inTransaction()) {
|
||||
$pdo->beginTransaction();
|
||||
$costTransactionStarted = true;
|
||||
}
|
||||
unset($fields['cost_items']);
|
||||
$keptCostItemIds = array_values(array_filter(array_map(
|
||||
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);
|
||||
if ($costTransactionStarted) {
|
||||
$pdo->commit();
|
||||
$costTransactionStarted = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.');
|
||||
@@ -144,6 +153,9 @@ if ($requestMethod === 'POST') {
|
||||
echo json_encode(['ok' => true]);
|
||||
exit;
|
||||
} catch (Throwable $error) {
|
||||
if ($costTransactionStarted && $pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
http_response_code(422);
|
||||
echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
|
||||
Reference in new issue
Block a user