Decrypt token before FCM push and fallback to SMS on push fail, also decrypt names in campaigns log

This commit is contained in:
Hamza-Ayed
2026-06-30 21:52:15 +03:00
parent de06d1cd75
commit cf748dfd7c
2 changed files with 42 additions and 25 deletions

View File

@@ -37,8 +37,18 @@ try {
$stmt->execute(); $stmt->execute();
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC); $logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Decrypt names or just return them // Decrypt names since they are encrypted in the passengers table
// (Names are not encrypted in this schema, only phones are, so we can return directly) 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 // Aggregate statistics for Dashboard charts
$sqlStats = "SELECT message_type, COUNT(*) as count $sqlStats = "SELECT message_type, COUNT(*) as count

View File

@@ -130,7 +130,10 @@ try {
$stmtToken->execute([':pid' => $passengerId]); $stmtToken->execute([':pid' => $passengerId]);
$fcmToken = $stmtToken->fetchColumn(); $fcmToken = $stmtToken->fetchColumn();
$pushSent = false;
if ($fcmToken) { if ($fcmToken) {
$decryptedToken = $encryptionHelper->decryptData($fcmToken);
if ($decryptedToken) {
// Send FCM Push Notification (Free channel - no anti-spam restriction needed) // Send FCM Push Notification (Free channel - no anti-spam restriction needed)
$fcmData = [ $fcmData = [
'type' => 'marketing_campaign', 'type' => 'marketing_campaign',
@@ -139,7 +142,7 @@ try {
]; ];
$fcmResult = sendFcmNotification( $fcmResult = sendFcmNotification(
$fcmToken, $decryptedToken,
$pushTitle, $pushTitle,
$pushBody, $pushBody,
$fcmData, $fcmData,
@@ -153,9 +156,13 @@ try {
$logStmt = $con->prepare("INSERT INTO marketing_campaigns_log (passenger_id, message_type, country_code, region_name, triggered_by) VALUES (?, 'push', ?, ?, 'autopilot')"); $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]); $logStmt->execute([$passengerId, $countryCode, $regionName]);
$dispatchedPassengers[] = $passengerId; $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 // Check anti-spam first to prevent unnecessary marketing cost
if ($spamCount === 0) { if ($spamCount === 0) {
// Fetch and decrypt passenger phone number // Fetch and decrypt passenger phone number