2026-07-23 22:24:39 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\AppNotification;
|
|
|
|
|
use App\Models\AppNotificationRead;
|
2026-07-29 17:00:51 +03:30
|
|
|
use App\Services\AppNotificationUnreadService;
|
2026-07-23 22:24:39 +03:30
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
|
|
class AppNotificationController extends Controller
|
|
|
|
|
{
|
2026-07-29 17:00:51 +03:30
|
|
|
public function __construct(
|
|
|
|
|
private readonly AppNotificationUnreadService $unread,
|
|
|
|
|
) {
|
2026-07-23 22:24:39 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
private function assertFeatureEnabled(): void
|
2026-07-24 22:37:48 +03:30
|
|
|
{
|
2026-07-29 17:00:51 +03:30
|
|
|
if (! $this->unread->featureEnabled()) {
|
|
|
|
|
throw new NotFoundHttpException('اعلان داخل اپ غیرفعال است.');
|
|
|
|
|
}
|
2026-07-24 22:37:48 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-23 22:24:39 +03:30
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$this->assertFeatureEnabled();
|
|
|
|
|
$customerId = (int) auth()->id();
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
$rows = $this->unread->visibleQuery($customerId)
|
2026-07-23 22:24:39 +03:30
|
|
|
->orderByDesc('id')
|
|
|
|
|
->limit(100)
|
|
|
|
|
->get(['id', 'title', 'body', 'severity', 'created_at']);
|
|
|
|
|
|
|
|
|
|
$readIds = AppNotificationRead::query()
|
|
|
|
|
->where('customer_id', $customerId)
|
|
|
|
|
->whereIn('notification_id', $rows->pluck('id'))
|
|
|
|
|
->pluck('notification_id')
|
|
|
|
|
->all();
|
|
|
|
|
$readSet = array_fill_keys($readIds, true);
|
|
|
|
|
|
|
|
|
|
$payload = $rows->map(function (AppNotification $row) use ($readSet) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $row->id,
|
|
|
|
|
'title' => $row->title,
|
|
|
|
|
'body' => $row->body,
|
|
|
|
|
'severity' => $row->severity,
|
|
|
|
|
'created_at' => $row->created_at,
|
|
|
|
|
'is_read' => isset($readSet[$row->id]),
|
|
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response()->json($payload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unreadCount()
|
|
|
|
|
{
|
|
|
|
|
$this->assertFeatureEnabled();
|
|
|
|
|
$customerId = (int) auth()->id();
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
return response()->json($this->unread->payloadForCustomer($customerId));
|
2026-07-23 22:24:39 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function markRead(Request $request, int $id)
|
|
|
|
|
{
|
|
|
|
|
$this->assertFeatureEnabled();
|
|
|
|
|
$customerId = (int) auth()->id();
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
$exists = $this->unread->visibleQuery($customerId)->where('id', $id)->exists();
|
2026-07-23 22:24:39 +03:30
|
|
|
if (! $exists) {
|
|
|
|
|
throw new NotFoundHttpException('اعلان یافت نشد.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppNotificationRead::query()->updateOrCreate(
|
|
|
|
|
[
|
|
|
|
|
'notification_id' => $id,
|
|
|
|
|
'customer_id' => $customerId,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'read_at' => now(),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
$this->unread->publishForCustomer($customerId);
|
|
|
|
|
|
2026-07-23 22:24:39 +03:30
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function markAllRead()
|
|
|
|
|
{
|
|
|
|
|
$this->assertFeatureEnabled();
|
|
|
|
|
$customerId = (int) auth()->id();
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
$ids = $this->unread->visibleQuery($customerId)->pluck('id');
|
2026-07-23 22:24:39 +03:30
|
|
|
$now = now();
|
|
|
|
|
foreach ($ids as $notificationId) {
|
|
|
|
|
AppNotificationRead::query()->updateOrCreate(
|
|
|
|
|
[
|
|
|
|
|
'notification_id' => $notificationId,
|
|
|
|
|
'customer_id' => $customerId,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'read_at' => $now,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-29 17:00:51 +03:30
|
|
|
$this->unread->publishForCustomer($customerId);
|
|
|
|
|
|
2026-07-23 22:24:39 +03:30
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
}
|