added notif 2
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\DB\Te\TableEngine;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AppNotification;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AppNotificationController extends Controller
|
||||
{
|
||||
public function table(Request $request)
|
||||
{
|
||||
$query = AppNotification::query();
|
||||
|
||||
TableEngine::create($query, $request->all())
|
||||
->initSearch(['title', 'body', 'severity'])
|
||||
->select(['*'])
|
||||
->export($query);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $this->validatedPayload($request);
|
||||
$user = auth()->user();
|
||||
|
||||
$row = AppNotification::create([
|
||||
...$data,
|
||||
'is_active' => $data['is_active'] ?? true,
|
||||
'created_by' => $user?->id,
|
||||
]);
|
||||
|
||||
return response()->json($row, 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id)
|
||||
{
|
||||
$row = AppNotification::findOrFail($id);
|
||||
$data = $this->validatedPayload($request, true);
|
||||
$row->update($data);
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
public function toggleActive(int $id)
|
||||
{
|
||||
$row = AppNotification::findOrFail($id);
|
||||
$row->is_active = ! $row->is_active;
|
||||
$row->save();
|
||||
|
||||
return response()->json([
|
||||
'is_active' => (bool) $row->is_active,
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id)
|
||||
{
|
||||
AppNotification::findOrFail($id)->delete();
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
private function validatedPayload(Request $request, bool $partial = false): array
|
||||
{
|
||||
$rules = [
|
||||
'title' => ($partial ? 'sometimes|' : '') . 'required|string|max:191',
|
||||
'body' => ($partial ? 'sometimes|' : '') . 'required|string|max:5000',
|
||||
'severity' => ($partial ? 'sometimes|' : '') . 'required|in:info,success,warning,danger',
|
||||
'is_active' => 'sometimes|boolean',
|
||||
];
|
||||
|
||||
return $request->validate($rules);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Customer;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AppNotification;
|
||||
use App\Models\AppNotificationRead;
|
||||
use App\Services\ConfigService;
|
||||
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', false)) {
|
||||
throw new NotFoundHttpException('اعلان داخل اپ غیرفعال است.');
|
||||
}
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->assertFeatureEnabled();
|
||||
$customerId = (int) auth()->id();
|
||||
|
||||
$rows = AppNotification::query()
|
||||
->where('is_active', true)
|
||||
->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();
|
||||
|
||||
$total = AppNotification::query()->where('is_active', true)->count();
|
||||
$read = AppNotificationRead::query()
|
||||
->where('customer_id', $customerId)
|
||||
->whereIn(
|
||||
'notification_id',
|
||||
AppNotification::query()->where('is_active', true)->select('id')
|
||||
)
|
||||
->count();
|
||||
|
||||
return response()->json([
|
||||
'unread_count' => max(0, $total - $read),
|
||||
]);
|
||||
}
|
||||
|
||||
public function markRead(Request $request, int $id)
|
||||
{
|
||||
$this->assertFeatureEnabled();
|
||||
$customerId = (int) auth()->id();
|
||||
|
||||
$exists = AppNotification::query()
|
||||
->where('id', $id)
|
||||
->where('is_active', true)
|
||||
->exists();
|
||||
if (! $exists) {
|
||||
throw new NotFoundHttpException('اعلان یافت نشد.');
|
||||
}
|
||||
|
||||
AppNotificationRead::query()->updateOrCreate(
|
||||
[
|
||||
'notification_id' => $id,
|
||||
'customer_id' => $customerId,
|
||||
],
|
||||
[
|
||||
'read_at' => now(),
|
||||
]
|
||||
);
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
public function markAllRead()
|
||||
{
|
||||
$this->assertFeatureEnabled();
|
||||
$customerId = (int) auth()->id();
|
||||
|
||||
$ids = AppNotification::query()
|
||||
->where('is_active', true)
|
||||
->pluck('id');
|
||||
|
||||
$now = now();
|
||||
foreach ($ids as $notificationId) {
|
||||
AppNotificationRead::query()->updateOrCreate(
|
||||
[
|
||||
'notification_id' => $notificationId,
|
||||
'customer_id' => $customerId,
|
||||
],
|
||||
[
|
||||
'read_at' => $now,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class AppNotification extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'app_notifications';
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'body',
|
||||
'severity',
|
||||
'is_active',
|
||||
'created_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function reads(): HasMany
|
||||
{
|
||||
return $this->hasMany(AppNotificationRead::class, 'notification_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AppNotificationRead extends Model
|
||||
{
|
||||
protected $table = 'app_notification_reads';
|
||||
|
||||
protected $fillable = [
|
||||
'notification_id',
|
||||
'customer_id',
|
||||
'read_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'read_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function notification(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AppNotification::class, 'notification_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user