diff --git a/backend/Admin/marketing/get_campaigns_log.php b/backend/Admin/marketing/get_campaigns_log.php index 3f13ee34..8073a282 100644 --- a/backend/Admin/marketing/get_campaigns_log.php +++ b/backend/Admin/marketing/get_campaigns_log.php @@ -37,8 +37,18 @@ try { $stmt->execute(); $logs = $stmt->fetchAll(PDO::FETCH_ASSOC); - // Decrypt names or just return them - // (Names are not encrypted in this schema, only phones are, so we can return directly) + // Decrypt names since they are encrypted in the passengers table + foreach ($logs as &$log) { + if (!empty($log['first_name'])) { + $decName = $encryptionHelper->decryptData($log['first_name']); + if ($decName) $log['first_name'] = $decName; + } + if (!empty($log['last_name'])) { + $decName = $encryptionHelper->decryptData($log['last_name']); + if ($decName) $log['last_name'] = $decName; + } + } + unset($log); // Aggregate statistics for Dashboard charts $sqlStats = "SELECT message_type, COUNT(*) as count diff --git a/backend/Admin/marketing/trigger_campaign.php b/backend/Admin/marketing/trigger_campaign.php index db227fba..75668832 100644 --- a/backend/Admin/marketing/trigger_campaign.php +++ b/backend/Admin/marketing/trigger_campaign.php @@ -130,32 +130,39 @@ try { $stmtToken->execute([':pid' => $passengerId]); $fcmToken = $stmtToken->fetchColumn(); + $pushSent = false; if ($fcmToken) { - // Send FCM Push Notification (Free channel - no anti-spam restriction needed) - $fcmData = [ - 'type' => 'marketing_campaign', - 'promo_code' => $promoCode, - 'discount' => (string)$discountVal - ]; - - $fcmResult = sendFcmNotification( - $fcmToken, - $pushTitle, - $pushBody, - $fcmData, - 'Marketing', - 'notification' - ); + $decryptedToken = $encryptionHelper->decryptData($fcmToken); + if ($decryptedToken) { + // Send FCM Push Notification (Free channel - no anti-spam restriction needed) + $fcmData = [ + 'type' => 'marketing_campaign', + 'promo_code' => $promoCode, + 'discount' => (string)$discountVal + ]; + + $fcmResult = sendFcmNotification( + $decryptedToken, + $pushTitle, + $pushBody, + $fcmData, + 'Marketing', + 'notification' + ); - if ($fcmResult['status'] === 'success') { - $sentFcm++; - // Log campaign dispatch - $logStmt = $con->prepare("INSERT INTO marketing_campaigns_log (passenger_id, message_type, country_code, region_name, triggered_by) VALUES (?, 'push', ?, ?, 'autopilot')"); - $logStmt->execute([$passengerId, $countryCode, $regionName]); - $dispatchedPassengers[] = $passengerId; + if ($fcmResult['status'] === 'success') { + $sentFcm++; + // Log campaign dispatch + $logStmt = $con->prepare("INSERT INTO marketing_campaigns_log (passenger_id, message_type, country_code, region_name, triggered_by) VALUES (?, 'push', ?, ?, 'autopilot')"); + $logStmt->execute([$passengerId, $countryCode, $regionName]); + $dispatchedPassengers[] = $passengerId; + $pushSent = true; + } } - } else { - // Churned user (Deleted the app / No token) -> Send WhatsApp or SMS + } + + if (!$pushSent) { + // Fallback: Churned user (No token) OR Push failed -> Send WhatsApp or SMS // Check anti-spam first to prevent unnecessary marketing cost if ($spamCount === 0) { // Fetch and decrypt passenger phone number