Files
hoshpoint-backend/app/Http/Controllers/Api/ProfileController.php
soheil khaledabadi 936f789ac8 feat: Persian panel/API fixes and structural-model tweaks
- lang/fa: add validation/auth/passwords/pagination translations so admin
  and API validation errors are in Persian
- templates: soft-delete instead of cascade-deleting their generations;
  keep resolving trashed template on generations (labels/downloads)
- templates: add TemplateInfolist with a real thumbnail ImageEntry
- generations: auto-name template-based generations and use that name
  for the download file and as a searchable column
- admin: show all dates as Jalali via ->jalaliDate()/->jalaliDateTime()
  Filament macros (App\Support\Jalali)
- wallets: harden the deposit/withdraw action to surface the exact error
  instead of a 500
- profile: normalize Iranian mobile numbers (App\Support\Mobile),
  add social_links, coerce notification-preference booleans, require the
  current password to delete an account

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013P66ea1dC5CVM9t29496t5
2026-09-07 01:00:09 +03:30

202 lines
7.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Profile\ChangePasswordRequest;
use App\Http\Requests\Profile\DeleteAccountRequest;
use App\Http\Requests\Profile\UpdateProfileRequest;
use App\Http\Resources\UserResource;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use OpenApi\Attributes as OA;
class ProfileController extends Controller
{
use ApiResponse;
#[OA\Get(
path: '/profile',
description: 'Get the authenticated user profile',
summary: 'Show profile',
security: [['sanctum' => []]],
tags: ['Profile'],
responses: [
new OA\Response(
response: 200,
description: 'Profile 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: 'user', ref: '#/components/schemas/User'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
]
)]
public function show(): JsonResponse
{
return $this->success('اطلاعات پروفایل با موفقیت دریافت شد.', [
'user' => new UserResource(request()->user()),
]);
}
#[OA\Put(
path: '/profile',
description: 'Update the authenticated user profile',
summary: 'Update profile',
security: [['sanctum' => []]],
tags: ['Profile'],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'name', type: 'string', example: 'علی رضایی'),
new OA\Property(property: 'username', type: 'string', example: 'ali'),
new OA\Property(property: 'email', type: 'string', format: 'email', example: 'ali@example.com'),
new OA\Property(property: 'mobile', type: 'string', example: '09123456789'),
new OA\Property(property: 'password', type: 'string', format: 'password', example: 'password123'),
]
)
),
responses: [
new OA\Response(
response: 200,
description: 'Profile 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: 'user', ref: '#/components/schemas/User'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
new OA\Response(response: 422, description: 'Validation error'),
]
)]
public function update(UpdateProfileRequest $request): JsonResponse
{
$user = $request->user();
$data = $request->safe()->except('avatar');
if ($request->hasFile('avatar')) {
if ($user->avatar_path) {
Storage::disk('public')->delete($user->avatar_path);
}
$data['avatar_path'] = $request->file('avatar')->store('avatars', 'public');
}
$user->update($data);
return $this->success('پروفایل با موفقیت به‌روزرسانی شد.', [
'user' => new UserResource($user->fresh()),
]);
}
#[OA\Put(
path: '/profile/password',
description: 'Change the authenticated user password',
summary: 'Change password',
security: [['sanctum' => []]],
tags: ['Profile'],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
required: ['current_password', 'password', 'password_confirmation'],
properties: [
new OA\Property(property: 'current_password', type: 'string', format: 'password'),
new OA\Property(property: 'password', type: 'string', format: 'password'),
new OA\Property(property: 'password_confirmation', type: 'string', format: 'password'),
]
)
),
responses: [
new OA\Response(
response: 200,
description: 'Password changed 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'),
new OA\Response(response: 422, description: 'Validation error'),
]
)]
public function changePassword(ChangePasswordRequest $request): JsonResponse
{
$user = $request->user();
$user->update(['password' => $request->validated('password')]);
$currentTokenId = $user->currentAccessToken()?->id;
if ($currentTokenId) {
$user->tokens()->where('id', '!=', $currentTokenId)->delete();
}
return $this->success('رمز عبور با موفقیت تغییر کرد.');
}
#[OA\Delete(
path: '/profile',
description: 'Delete the authenticated user account. Requires the current password for confirmation.',
summary: 'Delete account',
security: [['sanctum' => []]],
tags: ['Profile'],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
required: ['password'],
properties: [
new OA\Property(property: 'password', type: 'string', format: 'password', example: 'password123'),
]
)
),
responses: [
new OA\Response(
response: 200,
description: 'Account deleted 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 destroy(DeleteAccountRequest $request): JsonResponse
{
$user = $request->user();
if ($user->avatar_path) {
Storage::disk('public')->delete($user->avatar_path);
}
$user->tokens()->delete();
$user->delete();
return $this->success('حساب کاربری با موفقیت حذف شد.');
}
}