Files
musadaq-saas/app/modules_app/batches/status.php
T

61 lines
1.6 KiB
PHP

<?php
/**
* Batch Status Endpoint
* GET /v1/batches/status
*
* Returns the processing status of a batch and its items.
*/
declare(strict_types=1);
use App\Core\Database;
use App\Middleware\AuthMiddleware;
use App\Core\Security;
$decoded = AuthMiddleware::check();
$tenantId = $decoded['tenant_id'];
$userId = $decoded['user_id'];
$data = Security::sanitize($_GET);
$batchId = $data['batch_id'] ?? null;
if (!$batchId) {
json_error('معرّف الدفعة مطلوب', 422);
}
$db = Database::getInstance();
// 1. Get batch info
$stmt = $db->prepare("
SELECT id, tenant_id, status, total_images, processed_images, failed_images, created_at, completed_at
FROM invoice_batches
WHERE id = ?
");
$stmt->execute([$batchId]);
$batch = $stmt->fetch();
if (!$batch || ($decoded['role'] !== 'super_admin' && $batch['tenant_id'] !== $tenantId)) {
json_error('الدفعة غير موجودة', 404);
}
// 2. Get items
$stmt = $db->prepare("
SELECT id, invoice_id, image_order, status, attempts, max_attempts, error_message, created_at, processed_at
FROM invoice_processing_queue
WHERE batch_id = ?
ORDER BY image_order ASC, id ASC
");
$stmt->execute([$batchId]);
$items = $stmt->fetchAll();
// 3. Tell the client explicitly whether it should keep polling. Without this the
// app polled forever whenever a batch ended in anything other than 'done'.
$isTerminal = in_array($batch['status'], ['done', 'partial_fail', 'failed'], true);
json_success([
'batch' => $batch,
'items' => $items,
'is_terminal' => $isTerminal,
'should_poll' => !$isTerminal,
], 'تم جلب حالة الدفعة');