Files
hoshpoint-backend/app/Http/Controllers/Api/NotificationController.php
soheil khaledabadi 46ae711a20 feat(api): add AI text-to-deck generation, wallet deposit structure, and profile/notification endpoints
Fills the gaps between the admin panel screens and the backend: a full
free-text + slide-count + language -> AI-generated presentation flow with
new slide-cloning logic in TemplateFillService, a "my presentations" list
endpoint, a swappable payment-gateway structure for wallet top-ups (fake
driver for now), and avatar upload / public-profile toggle / account
deletion / password change / notification preference endpoints.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-29 01:36:17 +03:30

182 lines
7.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\UpdateNotificationPreferencesRequest;
use App\Http\Resources\NotificationPreferenceResource;
use App\Http\Resources\NotificationResource;
use App\Models\NotificationPreference;
use App\Services\NotificationService;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;
class NotificationController extends Controller
{
use ApiResponse;
public function __construct(private readonly NotificationService $notificationService) {}
#[OA\Get(
path: '/notifications',
description: 'Get the authenticated user notifications',
summary: 'List notifications',
security: [['sanctum' => []]],
tags: ['Notifications'],
parameters: [
new OA\Parameter(name: 'per_page', description: 'Items per page', in: 'query', schema: new OA\Schema(type: 'integer', example: 15)),
],
responses: [
new OA\Response(
response: 200,
description: 'Notifications fetched successfully',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'نوتیفیکیشن‌ها با موفقیت دریافت شد.'),
new OA\Property(
property: 'data',
properties: [
new OA\Property(
property: 'notifications',
type: 'array',
items: new OA\Items(ref: '#/components/schemas/Notification')
),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
]
)]
public function index(Request $request): JsonResponse
{
$perPage = min(max((int) $request->integer('per_page', 15), 1), 100);
$notifications = $request->user()->notifications()->paginate($perPage);
return $this->success('نوتیفیکیشن‌ها با موفقیت دریافت شد.', [
'notifications' => NotificationResource::collection($notifications)->response()->getData(true),
]);
}
#[OA\Post(
path: '/notifications/read-all',
description: 'Mark all the authenticated user notifications as read',
summary: 'Mark all notifications as read',
security: [['sanctum' => []]],
tags: ['Notifications'],
responses: [
new OA\Response(
response: 200,
description: 'Notifications marked as read successfully',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'همه نوتیفیکیشن‌ها خوانده شد.'),
new OA\Property(property: 'data', type: 'object'),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
]
)]
public function markAllAsRead(Request $request): JsonResponse
{
$this->notificationService->markAllAsRead($request->user());
return $this->success('همه نوتیفیکیشن‌ها خوانده شد.');
}
#[OA\Get(
path: '/notifications/preferences',
description: 'Get the authenticated user notification preferences',
summary: 'Show notification preferences',
security: [['sanctum' => []]],
tags: ['Notifications'],
responses: [
new OA\Response(
response: 200,
description: 'Notification preferences fetched successfully',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'تنظیمات اعلان‌ها با موفقیت دریافت شد.'),
new OA\Property(
property: 'data',
properties: [
new OA\Property(property: 'preferences', ref: '#/components/schemas/NotificationPreference'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
]
)]
public function preferences(Request $request): JsonResponse
{
$preferences = $this->firstOrCreatePreferences($request);
return $this->success('تنظیمات اعلان‌ها با موفقیت دریافت شد.', [
'preferences' => new NotificationPreferenceResource($preferences),
]);
}
#[OA\Put(
path: '/notifications/preferences',
description: 'Update the authenticated user notification preferences',
summary: 'Update notification preferences',
security: [['sanctum' => []]],
tags: ['Notifications'],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/NotificationPreference')
),
responses: [
new OA\Response(
response: 200,
description: 'Notification preferences updated successfully',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'تنظیمات اعلان‌ها با موفقیت به‌روزرسانی شد.'),
new OA\Property(
property: 'data',
properties: [
new OA\Property(property: 'preferences', ref: '#/components/schemas/NotificationPreference'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
new OA\Response(response: 422, description: 'Validation error'),
]
)]
public function updatePreferences(UpdateNotificationPreferencesRequest $request): JsonResponse
{
$preferences = $this->firstOrCreatePreferences($request);
$preferences->update($request->validated());
return $this->success('تنظیمات اعلان‌ها با موفقیت به‌روزرسانی شد.', [
'preferences' => new NotificationPreferenceResource($preferences->fresh()),
]);
}
private function firstOrCreatePreferences(Request $request): NotificationPreference
{
return $request->user()->notificationPreference()->firstOrCreate([], [
'comment_on_presentation' => true,
'form_reply' => false,
'new_follower' => false,
'news_announcements' => false,
'weekly_product_updates' => true,
'weekly_blog_digest' => false,
]);
}
}