Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.61% |
19 / 23 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InAppNotificationRepository | |
82.61% |
19 / 23 |
|
85.71% |
6 / 7 |
7.26 | |
0.00% |
0 / 1 |
| getPendingForUser | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| markAsSent | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| updateStatus | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getByIdAndUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createBatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExistingCampaignIdsForUser | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| countSentTodayForUser | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Enums\InAppNotificationStatus; |
| 6 | use App\Http\Models\InAppNotification; |
| 7 | use App\Http\Repositories\interfaces\IInAppNotificationRepository; |
| 8 | use Carbon\Carbon; |
| 9 | use Illuminate\Database\Eloquent\Collection; |
| 10 | |
| 11 | /** |
| 12 | * Repository for in-app notification (per-user) data access operations. |
| 13 | */ |
| 14 | class InAppNotificationRepository implements IInAppNotificationRepository |
| 15 | { |
| 16 | /** |
| 17 | * {@inheritDoc} |
| 18 | */ |
| 19 | public function getPendingForUser(string $userId): Collection |
| 20 | { |
| 21 | return InAppNotification::where('user_id', $userId) |
| 22 | ->where('status', InAppNotificationStatus::PENDING->value) |
| 23 | ->with('campaign') |
| 24 | ->get(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * {@inheritDoc} |
| 29 | */ |
| 30 | public function markAsSent(array $ids): void |
| 31 | { |
| 32 | InAppNotification::whereIn('_id', $ids) |
| 33 | ->update([ |
| 34 | 'status' => InAppNotificationStatus::SENT->value, |
| 35 | 'sent_at' => Carbon::now(), |
| 36 | ]); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritDoc} |
| 41 | */ |
| 42 | public function updateStatus(InAppNotification $notification, array $data): InAppNotification |
| 43 | { |
| 44 | $notification->update($data); |
| 45 | |
| 46 | return $notification->fresh(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritDoc} |
| 51 | */ |
| 52 | public function getByIdAndUser(string $id, string $userId): ?InAppNotification |
| 53 | { |
| 54 | return InAppNotification::where('_id', $id) |
| 55 | ->where('user_id', $userId) |
| 56 | ->first(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * {@inheritDoc} |
| 61 | */ |
| 62 | public function createBatch(array $records): void |
| 63 | { |
| 64 | InAppNotification::insert($records); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * {@inheritDoc} |
| 69 | */ |
| 70 | public function getExistingCampaignIdsForUser(string $userId, array $campaignIds): array |
| 71 | { |
| 72 | return InAppNotification::where('user_id', $userId) |
| 73 | ->whereIn('campaign_id', $campaignIds) |
| 74 | ->pluck('campaign_id') |
| 75 | ->toArray(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritDoc} |
| 80 | */ |
| 81 | public function countSentTodayForUser(string $userId): int |
| 82 | { |
| 83 | return InAppNotification::where('user_id', $userId) |
| 84 | ->where('status', '!=', InAppNotificationStatus::PENDING->value) |
| 85 | ->where('sent_at', '>=', Carbon::today()) |
| 86 | ->count(); |
| 87 | } |
| 88 | } |