Update: 2026-05-08 14:29:22

This commit is contained in:
Hamza-Ayed
2026-05-08 14:29:22 +03:00
parent be0571648a
commit 9bfd394b26
4 changed files with 64 additions and 18 deletions

View File

@@ -33,7 +33,7 @@ class NotificationService
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND device_fingerprint = ? AND push_token IS NOT NULL");
$stmt->execute([$userId, $deviceId]);
} else {
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND push_token IS NOT NULL AND is_active = 1");
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND push_token IS NOT NULL");
$stmt->execute([$userId]);
}
@@ -74,7 +74,7 @@ class NotificationService
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND device_fingerprint = ? AND push_token IS NOT NULL");
$stmt->execute([$userId, $deviceId]);
} else {
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND push_token IS NOT NULL AND is_active = 1");
$stmt = $db->prepare("SELECT push_token FROM user_devices WHERE user_id = ? AND push_token IS NOT NULL");
$stmt->execute([$userId]);
}
@@ -130,6 +130,31 @@ class NotificationService
],
],
];
} else {
// Silent push / Live Activity Update
$message['android'] = [
'priority' => 'high'
];
$message['apns'] = [
'headers' => [
'apns-priority' => '5',
'apns-push-type' => 'background'
],
'payload' => [
'aps' => [
'content-available' => 1
]
]
];
// If the data contains live activity update markers, adjust headers for iOS ActivityKit
if (isset($data['type']) && $data['type'] === 'batch_progress') {
$message['apns']['headers']['apns-push-type'] = 'liveactivity';
$message['apns']['headers']['apns-priority'] = '10';
$message['apns']['payload']['aps']['content-state'] = $data;
$message['apns']['payload']['aps']['timestamp'] = time();
$message['apns']['payload']['aps']['event'] = 'update';
}
}
$payload = ['message' => $message];

View File

@@ -126,6 +126,9 @@ try {
// Update batch progress
$db->prepare("UPDATE invoice_batches SET processed_images = processed_images + 1 WHERE id = ?")->execute([$batchId]);
// Increment Quota
QuotaMiddleware::incrementInvoiceUsage($tenantId);
$db->commit();
echo "Success: Created Invoice $invoiceId\n";
@@ -143,14 +146,17 @@ try {
]);
}
// Increment Quota
QuotaMiddleware::incrementInvoiceUsage($tenantId);
} catch (Exception $e) {
$db->rollBack();
} catch (\Exception $pushErr) {
echo "Push error: " . $pushErr->getMessage() . "\n";
}
} catch (\Exception $e) {
if ($db->inTransaction()) {
$db->rollBack();
}
echo "DB Error: " . $e->getMessage() . "\n";
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = ? WHERE id = ?")->execute([$e->getMessage(), $queueId]);
try {
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = ? WHERE id = ?")->execute([$e->getMessage(), $queueId]);
} catch (\Exception $e2) {}
}
// Check if batch is complete

View File

@@ -57,6 +57,12 @@ $stmt = $db->prepare("
");
$stmt->execute([$batchId]);
// 3. If it's a single invoice, try triggering the worker in the background immediately
// This helps if the Cron Job is delayed or failing.
$workerPath = ROOT_PATH . '/app/cron/process_batches.php';
$logPath = STORAGE_PATH . '/logs/cron.log';
exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &");
json_success([
'batch_id' => $batchId,
'status' => 'processing',