fix some bugs

This commit is contained in:
2026-07-29 17:00:51 +03:30
parent dd77e2ce81
commit 16b947adc5
6 changed files with 138 additions and 44 deletions
@@ -5,36 +5,22 @@ 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 App\Services\AppNotificationUnreadService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AppNotificationController extends Controller
{
private function assertFeatureEnabled(): void
{
if (! app(ConfigService::class)->getBool('has_page_notification', true)) {
throw new NotFoundHttpException('اعلان داخل اپ غیرفعال است.');
}
public function __construct(
private readonly AppNotificationUnreadService $unread,
) {
}
private function visibleQuery(int $customerId): Builder
private function assertFeatureEnabled(): void
{
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);
});
});
});
if (! $this->unread->featureEnabled()) {
throw new NotFoundHttpException('اعلان داخل اپ غیرفعال است.');
}
}
public function index()
@@ -42,7 +28,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$rows = $this->visibleQuery($customerId)
$rows = $this->unread->visibleQuery($customerId)
->orderByDesc('id')
->limit(100)
->get(['id', 'title', 'body', 'severity', 'created_at']);
@@ -73,16 +59,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$visibleIds = $this->visibleQuery($customerId)->select('id');
$total = (clone $visibleIds)->count();
$read = AppNotificationRead::query()
->where('customer_id', $customerId)
->whereIn('notification_id', $visibleIds)
->count();
return response()->json([
'unread_count' => max(0, $total - $read),
]);
return response()->json($this->unread->payloadForCustomer($customerId));
}
public function markRead(Request $request, int $id)
@@ -90,7 +67,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$exists = $this->visibleQuery($customerId)->where('id', $id)->exists();
$exists = $this->unread->visibleQuery($customerId)->where('id', $id)->exists();
if (! $exists) {
throw new NotFoundHttpException('اعلان یافت نشد.');
}
@@ -105,6 +82,8 @@ class AppNotificationController extends Controller
]
);
$this->unread->publishForCustomer($customerId);
return response()->json(true);
}
@@ -113,7 +92,7 @@ class AppNotificationController extends Controller
$this->assertFeatureEnabled();
$customerId = (int) auth()->id();
$ids = $this->visibleQuery($customerId)->pluck('id');
$ids = $this->unread->visibleQuery($customerId)->pluck('id');
$now = now();
foreach ($ids as $notificationId) {
AppNotificationRead::query()->updateOrCreate(
@@ -127,6 +106,8 @@ class AppNotificationController extends Controller
);
}
$this->unread->publishForCustomer($customerId);
return response()->json(true);
}
}