Commit e76deb6f authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: filter logic bugs — coins/date range now properly combinable

- Moved level, coins, and created_at to PostgREST `and` operator so
  min+max ranges work together instead of overwriting each other
- Batch-insert campaign recipients (100/batch) instead of N HTTP calls
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 0188bd9e
...@@ -136,15 +136,21 @@ class NotificationsController ...@@ -136,15 +136,21 @@ class NotificationsController
} }
} }
// Save recipients // Save recipients in batches
foreach ($players as $player) { $recipientChunks = array_chunk($players, 100);
$this->db->insert('push_campaign_recipients', [ $sentAt = date('c');
foreach ($recipientChunks as $chunk) {
$recipientRows = [];
foreach ($chunk as $player) {
$recipientRows[] = [
'campaign_id' => $campaignId, 'campaign_id' => $campaignId,
'user_id' => $player['id'], 'user_id' => $player['id'],
'display_name' => $player['display_name'] ?? null, 'display_name' => $player['display_name'] ?? null,
'status' => 'sent', 'status' => 'sent',
'sent_at' => date('c'), 'sent_at' => $sentAt,
]); ];
}
$this->db->insert('push_campaign_recipients', $recipientRows);
} }
// Update campaign // Update campaign
...@@ -468,13 +474,13 @@ class NotificationsController ...@@ -468,13 +474,13 @@ class NotificationsController
private function applyFilters(array &$queryParams, array $filters): void private function applyFilters(array &$queryParams, array $filters): void
{ {
if (!empty($filters['level_min']) && !empty($filters['level_max'])) { $andConditions = [];
$queryParams['level'] = "gte.{$filters['level_min']}";
$queryParams['and'] = "(level.lte.{$filters['level_max']})"; if (!empty($filters['level_min'])) {
} elseif (!empty($filters['level_min'])) { $andConditions[] = "level.gte.{$filters['level_min']}";
$queryParams['level'] = "gte.{$filters['level_min']}"; }
} elseif (!empty($filters['level_max'])) { if (!empty($filters['level_max'])) {
$queryParams['level'] = "lte.{$filters['level_max']}"; $andConditions[] = "level.lte.{$filters['level_max']}";
} }
if (!empty($filters['games_min'])) { if (!empty($filters['games_min'])) {
...@@ -482,9 +488,10 @@ class NotificationsController ...@@ -482,9 +488,10 @@ class NotificationsController
} }
if (!empty($filters['coins_min'])) { if (!empty($filters['coins_min'])) {
$queryParams['coins'] = "gte.{$filters['coins_min']}"; $andConditions[] = "coins.gte.{$filters['coins_min']}";
} elseif (!empty($filters['coins_max'])) { }
$queryParams['coins'] = "lte.{$filters['coins_max']}"; if (!empty($filters['coins_max'])) {
$andConditions[] = "coins.lte.{$filters['coins_max']}";
} }
if (!empty($filters['country'])) { if (!empty($filters['country'])) {
...@@ -509,10 +516,14 @@ class NotificationsController ...@@ -509,10 +516,14 @@ class NotificationsController
} }
if (!empty($filters['registered_after'])) { if (!empty($filters['registered_after'])) {
$queryParams['created_at'] = "gte.{$filters['registered_after']}T00:00:00"; $andConditions[] = "created_at.gte.{$filters['registered_after']}T00:00:00";
} }
if (!empty($filters['registered_before'])) { if (!empty($filters['registered_before'])) {
$queryParams['created_at'] = "lte.{$filters['registered_before']}T23:59:59"; $andConditions[] = "created_at.lte.{$filters['registered_before']}T23:59:59";
}
if (!empty($andConditions)) {
$queryParams['and'] = '(' . implode(',', $andConditions) . ')';
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment