fix notif 3

This commit is contained in:
2026-07-24 22:37:48 +03:30
parent 107cf5b291
commit 52ff3a088d
7 changed files with 493 additions and 32 deletions
@@ -5,7 +5,9 @@ namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\AppNotification;
use App\Models\AppNotificationRead;
use App\Services\AppNotificationAudienceService;
use App\Services\ConfigService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -18,13 +20,29 @@ class AppNotificationController extends Controller
}
}
private function visibleQuery(int $customerId): Builder
{
return AppNotification::query()
->where('is_active', true)
->where(function (Builder $q) use ($customerId) {
$q->where('audience_type', AppNotificationAudienceService::TYPE_ALL)
->orWhere(function (Builder $inner) use ($customerId) {
$inner->whereIn('audience_type', [
AppNotificationAudienceService::TYPE_SEGMENT,
AppNotificationAudienceService::TYPE_CUSTOMERS,
])->whereHas('recipients', function (Builder $r) use ($customerId) {
$r->where('customer_id', $customerId);
});
});
});
}
public function index()
{
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$rows = AppNotification::query()
->where('is_active', true)
$rows = $this->visibleQuery($customerId)
->orderByDesc('id')
->limit(100)
->get(['id', 'title', 'body', 'severity', 'created_at']);
@@ -55,13 +73,11 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$total = AppNotification::query()->where('is_active', true)->count();
$visibleIds = $this->visibleQuery($customerId)->select('id');
$total = (clone $visibleIds)->count();
$read = AppNotificationRead::query()
->where('customer_id', $customerId)
->whereIn(
'notification_id',
AppNotification::query()->where('is_active', true)->select('id')
)
->whereIn('notification_id', $visibleIds)
->count();
return response()->json([
@@ -74,10 +90,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$exists = AppNotification::query()
->where('id', $id)
->where('is_active', true)
->exists();
$exists = $this->visibleQuery($customerId)->where('id', $id)->exists();
if (! $exists) {
throw new NotFoundHttpException('اعلان یافت نشد.');
}
@@ -100,10 +113,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$ids = AppNotification::query()
->where('is_active', true)
->pluck('id');
$ids = $this->visibleQuery($customerId)->pluck('id');
$now = now();
foreach ($ids as $notificationId) {
AppNotificationRead::query()->updateOrCreate(