Skip to content

Commit

Permalink
fix: notification sent on the wrong day
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ribeiro committed Nov 14, 2024
1 parent 0a8bb4d commit 37a14ee
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
$stmt = $db->prepare($query);
$usersToNotify = $stmt->execute();

function getDaysText($days) {
if ($days == 0) {
return "Today";
} elseif ($days == 1) {
return "Tomorrow";
} else {
return "In " . $days . " days";
}
}

while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$userId = $userToNotify['id'];
if (php_sapi_name() !== 'cli') {
Expand Down Expand Up @@ -213,8 +223,16 @@
$daysToCompare = $days;
}
$nextPaymentDate = new DateTime($rowSubscription['next_payment']);
$difference = $currentDate->diff($nextPaymentDate)->days + 1;

$difference = $currentDate->diff($nextPaymentDate)->days;
if ($nextPaymentDate > $currentDate) {
$difference += 1;
}

if ($difference === $daysToCompare) {
echo "Next payment date: " . $nextPaymentDate->format('Y-m-d') . "<br />";
echo "Current date: " . $currentDate->format('Y-m-d') . "<br />";
echo "Difference: " . $difference . "<br />";
$notify[$rowSubscription['payer_user_id']][$i]['name'] = $rowSubscription['name'];
$notify[$rowSubscription['payer_user_id']][$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol'];
$notify[$rowSubscription['payer_user_id']][$i]['currency'] = $currencies[$rowSubscription['currency_id']]['name'];
Expand Down Expand Up @@ -244,7 +262,7 @@
$message = "The following subscriptions are up for renewal:\n";

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -313,7 +331,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -366,7 +384,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -420,7 +438,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -469,7 +487,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -511,7 +529,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down

0 comments on commit 37a14ee

Please sign in to comment.