Files
Verde-Web/app/Http/Controllers/Api/V1/Me/MyNotificationsController.php
admin 306f8fb4e8 feat(backend): customer-site API gaps + CORS
- GET /partner-stores/nearby — public, residents browse active stores
  ranked by distance via ST_Distance_Sphere; PublicPartnerStoreResource
  excludes commission rate / owner / permit (only what a buyer needs).
- GET /partner-stores/{uuid} — public details, 404 if not active.
- GET /me/collections — paginated resident QR scan history with
  optional from/to date filters.
- GET /me/upcoming-pickups — finds scheduled/in-progress trips whose
  route includes the resident's assigned drop-off point. Returns
  household_assigned: false when no household yet.
- GET /me/notifications — paginated database notifications inbox
  with unread_only filter + unread_count in meta.
- POST /me/notifications/{id}/read, POST .../mark-all-read,
  GET .../unread-count.
- config/cors.php — allow CUSTOMER_APP_URL and any EXTRA_CORS_ORIGINS
  to call the API with credentials. Same-origin admin web is
  unaffected.

202 feature tests passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 17:55:10 +08:00

68 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Me;
use App\Http\Controllers\Api\V1\ApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class MyNotificationsController extends ApiController
{
public function index(Request $request): JsonResponse
{
$data = $request->validate([
'unread_only' => ['nullable', 'boolean'],
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
]);
$perPage = (int) ($data['per_page'] ?? 25);
$unreadOnly = filter_var($data['unread_only'] ?? false, FILTER_VALIDATE_BOOL);
$user = $request->user();
$query = $unreadOnly ? $user->unreadNotifications() : $user->notifications();
$page = $query->paginate($perPage);
$items = $page->getCollection()->map(fn ($n) => [
'id' => $n->id,
'type' => class_basename($n->type),
'data' => $n->data,
'read_at' => $n->read_at?->toIso8601String(),
'created_at' => $n->created_at?->toIso8601String(),
])->all();
return $this->ok($items, null, [
'page' => $page->currentPage(),
'per_page' => $page->perPage(),
'total' => $page->total(),
'last_page' => $page->lastPage(),
'unread_count' => $user->unreadNotifications()->count(),
]);
}
public function markRead(Request $request, string $id): JsonResponse
{
$notification = $request->user()->notifications()->where('id', $id)->first();
if (! $notification) {
return $this->notFound('Notification not found');
}
$notification->markAsRead();
return $this->ok(['read_at' => $notification->read_at->toIso8601String()]);
}
public function markAllRead(Request $request): JsonResponse
{
$count = $request->user()->unreadNotifications()->count();
$request->user()->unreadNotifications->markAsRead();
return $this->ok(['marked_read' => $count], "Marked {$count} as read");
}
public function unreadCount(Request $request): JsonResponse
{
return $this->ok([
'unread_count' => $request->user()->unreadNotifications()->count(),
]);
}
}