Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| InAppNotificationController | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateStatus | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Requests\v2\InAppNotification\UpdateInAppNotificationStatusRequest; |
| 7 | use App\Http\Resources\v2\InAppNotificationResource; |
| 8 | use App\Http\Services\InAppNotificationService; |
| 9 | use Illuminate\Http\JsonResponse; |
| 10 | |
| 11 | /** |
| 12 | * Controller for user-facing in-app notification operations. |
| 13 | * |
| 14 | * Provides the PATCH endpoint for users to report notification interactions |
| 15 | * (viewed, clicked, dismissed). |
| 16 | */ |
| 17 | class InAppNotificationController extends Controller |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly InAppNotificationService $inAppNotificationService |
| 21 | ) {} |
| 22 | |
| 23 | /** |
| 24 | * Update the status of an in-app notification. |
| 25 | * |
| 26 | * @param UpdateInAppNotificationStatusRequest $request Validated request with status |
| 27 | * @param string $id The notification ID |
| 28 | * |
| 29 | * @response 200 { |
| 30 | * "result": { |
| 31 | * "data": { |
| 32 | * "id": "507f1f77bcf86cd799439011", |
| 33 | * "campaign_id": "507f1f77bcf86cd799439012", |
| 34 | * "status": "viewed", |
| 35 | * "sent_at": 1642521600, |
| 36 | * "viewed_at": 1642521700, |
| 37 | * "clicked_at": null, |
| 38 | * "dismissed_at": null, |
| 39 | * "impression_count": 1, |
| 40 | * "created_at": 1642521500, |
| 41 | * "updated_at": 1642521700 |
| 42 | * } |
| 43 | * } |
| 44 | * } |
| 45 | * @response 404 {"error": "Notification not found or invalid status transition"} |
| 46 | */ |
| 47 | public function updateStatus(UpdateInAppNotificationStatusRequest $request, string $id): JsonResponse |
| 48 | { |
| 49 | $notification = $this->inAppNotificationService->updateNotificationStatus( |
| 50 | $id, |
| 51 | (string) $request->user()->_id, |
| 52 | $request->validated('status') |
| 53 | ); |
| 54 | |
| 55 | if (! $notification) { |
| 56 | return response()->json(['error' => 'Notification not found or invalid status transition'], 404); |
| 57 | } |
| 58 | |
| 59 | return (new InAppNotificationResource($notification)) |
| 60 | ->response() |
| 61 | ->setStatusCode(200); |
| 62 | } |
| 63 | } |