Durchführungsstatus im Tagescron automatisch abschliessen
This commit is contained in:
1 parent
2a1144f0aa
commit
30c11c5ca7
3 files changed
+198
No files matched your search
@@ -123,6 +123,91 @@ function market_management_category_exists(PDO $pdo, int $categoryId): bool
|
||||
return $stmt->fetchColumn() !== false;
|
||||
}
|
||||
|
||||
function market_management_auto_complete_occurrences(PDO $pdo, ?DateTimeImmutable $asOfDate = null): array
|
||||
{
|
||||
$asOfDate ??= new DateTimeImmutable('today', new DateTimeZone('Europe/Zurich'));
|
||||
$processName = 'market_management.occurrence_status_completion';
|
||||
$scope = ['as_of_date' => $asOfDate->format('Y-m-d')];
|
||||
$runStmt = $pdo->prepare(
|
||||
"INSERT INTO process_runs (process_name, status, scope_json, requested_by)
|
||||
VALUES (:process_name, 'running', :scope_json::jsonb, 'cron')
|
||||
RETURNING id"
|
||||
);
|
||||
$runStmt->execute([
|
||||
':process_name' => $processName,
|
||||
':scope_json' => json_encode($scope, JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
$runId = (int) $runStmt->fetchColumn();
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
$stmt = $pdo->prepare(
|
||||
"WITH eligible AS (
|
||||
SELECT o.id
|
||||
FROM market_occurrence o
|
||||
INNER JOIN market_occurrence_day d ON d.occurrence_id = o.id
|
||||
WHERE o.archived_at IS NULL
|
||||
AND o.status <> 'cancelled'
|
||||
AND o.status <> 'completed'
|
||||
GROUP BY o.id
|
||||
HAVING MAX(d.date) < :as_of_date::DATE
|
||||
)
|
||||
UPDATE market_occurrence o
|
||||
SET status = 'completed', updated_at = NOW()
|
||||
FROM eligible e
|
||||
WHERE o.id = e.id
|
||||
RETURNING o.id"
|
||||
);
|
||||
$stmt->execute([':as_of_date' => $asOfDate->format('Y-m-d')]);
|
||||
$completedIds = array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||
$pdo->commit();
|
||||
|
||||
$result = [
|
||||
'status' => 'done',
|
||||
'as_of_date' => $asOfDate->format('Y-m-d'),
|
||||
'completed_count' => count($completedIds),
|
||||
'completed_occurrence_ids' => $completedIds,
|
||||
'technical_run_id' => $runId,
|
||||
];
|
||||
$finishStmt = $pdo->prepare(
|
||||
'UPDATE process_runs
|
||||
SET status = :status, result_json = :result_json::jsonb, finished_at = NOW()
|
||||
WHERE id = :id'
|
||||
);
|
||||
$finishStmt->execute([
|
||||
':status' => 'done',
|
||||
':result_json' => json_encode($result, JSON_THROW_ON_ERROR),
|
||||
':id' => $runId,
|
||||
]);
|
||||
return $result;
|
||||
} catch (Throwable $error) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$result = [
|
||||
'status' => 'failed',
|
||||
'as_of_date' => $asOfDate->format('Y-m-d'),
|
||||
'completed_count' => 0,
|
||||
'completed_occurrence_ids' => [],
|
||||
'technical_run_id' => $runId,
|
||||
];
|
||||
$finishStmt = $pdo->prepare(
|
||||
'UPDATE process_runs
|
||||
SET status = :status, result_json = :result_json::jsonb, finished_at = NOW(),
|
||||
error_code = :error_code, error_message = :error_message
|
||||
WHERE id = :id'
|
||||
);
|
||||
$finishStmt->execute([
|
||||
':status' => 'failed',
|
||||
':result_json' => json_encode($result, JSON_THROW_ON_ERROR),
|
||||
':error_code' => 'occurrence_status_completion_failed',
|
||||
':error_message' => $error->getMessage(),
|
||||
':id' => $runId,
|
||||
]);
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
function market_management_create_market(PDO $pdo, array $input): array
|
||||
{
|
||||
$name = trim((string) ($input['name'] ?? ''));
|
||||
|
||||
Reference in new issue
Block a user