- 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
279 lines
12 KiB
PHP
279 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Enums\TemplateGenerationSource;
|
|
use App\Enums\TemplateGenerationStatus;
|
|
use App\Enums\TemplateKeyType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\GenerateAiPresentationRequest;
|
|
use App\Http\Requests\Api\GenerateTemplateRequest;
|
|
use App\Http\Resources\TemplateGenerationResource;
|
|
use App\Jobs\GenerateTemplateJob;
|
|
use App\Models\Template;
|
|
use App\Models\TemplateGeneration;
|
|
use App\Support\Jalali;
|
|
use App\Traits\ApiResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use OpenApi\Attributes as OA;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class TemplateGenerationController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
#[OA\Post(
|
|
path: '/templates/{slug}/generate',
|
|
description: 'Start generating a filled presentation from a template',
|
|
summary: 'Generate presentation',
|
|
security: [['sanctum' => []]],
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'slug', description: 'Template slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
|
|
],
|
|
responses: [
|
|
new OA\Response(
|
|
response: 202,
|
|
description: 'Generation started',
|
|
content: new OA\JsonContent(
|
|
properties: [
|
|
new OA\Property(property: 'message', type: 'string', example: 'درخواست تولید پاورپوینت ثبت شد.'),
|
|
new OA\Property(
|
|
property: 'data',
|
|
properties: [
|
|
new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'),
|
|
],
|
|
type: 'object'
|
|
),
|
|
]
|
|
)
|
|
),
|
|
new OA\Response(response: 401, description: 'Unauthenticated'),
|
|
new OA\Response(response: 422, description: 'Validation error'),
|
|
]
|
|
)]
|
|
public function store(GenerateTemplateRequest $request, Template $template): JsonResponse
|
|
{
|
|
$uuid = (string) Str::uuid();
|
|
$values = $request->validated('values', []);
|
|
|
|
$storedValues = [];
|
|
|
|
foreach ($template->keys as $key) {
|
|
if (! array_key_exists($key->key, $values)) {
|
|
continue;
|
|
}
|
|
|
|
if ($key->type === TemplateKeyType::Image) {
|
|
$storedValues[$key->key] = $request->file('values.'.$key->key)
|
|
->store('templates/generations/inputs/'.$uuid, 'local');
|
|
|
|
continue;
|
|
}
|
|
|
|
$storedValues[$key->key] = $values[$key->key];
|
|
}
|
|
|
|
$generation = TemplateGeneration::query()->create([
|
|
'template_id' => $template->id,
|
|
'uuid' => $uuid,
|
|
'title' => $this->buildGenerationTitle($template, $request),
|
|
'status' => TemplateGenerationStatus::Pending,
|
|
'values' => $storedValues,
|
|
'requested_by' => $request->user()->id,
|
|
]);
|
|
|
|
GenerateTemplateJob::dispatch($generation);
|
|
|
|
return $this->success('درخواست تولید پاورپوینت ثبت شد.', [
|
|
'generation' => new TemplateGenerationResource($generation),
|
|
], 202);
|
|
}
|
|
|
|
#[OA\Post(
|
|
path: '/templates/{slug}/generate-from-text',
|
|
description: 'Start generating a presentation from free-form text using AI',
|
|
summary: 'Generate presentation from text (AI)',
|
|
security: [['sanctum' => []]],
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'slug', description: 'Template slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
|
|
],
|
|
requestBody: new OA\RequestBody(
|
|
required: true,
|
|
content: new OA\JsonContent(
|
|
required: ['title', 'language', 'slide_count', 'text'],
|
|
properties: [
|
|
new OA\Property(property: 'title', type: 'string', example: 'گزارش فروش سال ۱۴۰۴'),
|
|
new OA\Property(property: 'language', type: 'string', example: 'fa'),
|
|
new OA\Property(property: 'slide_count', type: 'integer', example: 10),
|
|
new OA\Property(property: 'text', type: 'string', example: 'متن، عناوین، بولتها و قوانین موردنظر...'),
|
|
]
|
|
)
|
|
),
|
|
responses: [
|
|
new OA\Response(
|
|
response: 202,
|
|
description: 'Generation started',
|
|
content: new OA\JsonContent(
|
|
properties: [
|
|
new OA\Property(property: 'message', type: 'string', example: 'درخواست تولید پاورپوینت ثبت شد.'),
|
|
new OA\Property(
|
|
property: 'data',
|
|
properties: [
|
|
new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'),
|
|
],
|
|
type: 'object'
|
|
),
|
|
]
|
|
)
|
|
),
|
|
new OA\Response(response: 401, description: 'Unauthenticated'),
|
|
new OA\Response(response: 422, description: 'Validation error'),
|
|
]
|
|
)]
|
|
public function storeFromText(GenerateAiPresentationRequest $request, Template $template): JsonResponse
|
|
{
|
|
$generation = TemplateGeneration::query()->create([
|
|
'template_id' => $template->id,
|
|
'uuid' => (string) Str::uuid(),
|
|
'source' => TemplateGenerationSource::AiText,
|
|
'title' => $request->validated('title'),
|
|
'language' => $request->validated('language'),
|
|
'slide_count' => $request->validated('slide_count'),
|
|
'source_text' => $request->validated('text'),
|
|
'status' => TemplateGenerationStatus::Pending,
|
|
'requested_by' => $request->user()->id,
|
|
]);
|
|
|
|
GenerateTemplateJob::dispatch($generation);
|
|
|
|
return $this->success('درخواست تولید پاورپوینت ثبت شد.', [
|
|
'generation' => new TemplateGenerationResource($generation),
|
|
], 202);
|
|
}
|
|
|
|
#[OA\Get(
|
|
path: '/generations',
|
|
description: 'List the authenticated user presentation generations (both template-based and AI-generated)',
|
|
summary: 'List my presentations',
|
|
security: [['sanctum' => []]],
|
|
tags: ['Templates'],
|
|
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: 'Generations 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: 'generations',
|
|
type: 'array',
|
|
items: new OA\Items(ref: '#/components/schemas/TemplateGeneration')
|
|
),
|
|
],
|
|
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);
|
|
|
|
$generations = TemplateGeneration::query()
|
|
->with('template')
|
|
->where('requested_by', $request->user()->id)
|
|
->latest()
|
|
->paginate($perPage);
|
|
|
|
return $this->success('پاورپوینتهای شما با موفقیت دریافت شد.', [
|
|
'generations' => TemplateGenerationResource::collection($generations)->response()->getData(true),
|
|
]);
|
|
}
|
|
|
|
#[OA\Get(
|
|
path: '/generations/{uuid}',
|
|
description: 'Get the status of a presentation generation request',
|
|
summary: 'Show generation status',
|
|
security: [['sanctum' => []]],
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
|
|
],
|
|
responses: [
|
|
new OA\Response(
|
|
response: 200,
|
|
description: 'Generation status 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: 'generation', ref: '#/components/schemas/TemplateGeneration'),
|
|
],
|
|
type: 'object'
|
|
),
|
|
]
|
|
)
|
|
),
|
|
new OA\Response(response: 401, description: 'Unauthenticated'),
|
|
new OA\Response(response: 403, description: 'Forbidden'),
|
|
new OA\Response(response: 404, description: 'Not found'),
|
|
]
|
|
)]
|
|
public function show(TemplateGeneration $generation): JsonResponse
|
|
{
|
|
abort_if($generation->requested_by !== request()->user()->id, 403);
|
|
|
|
return $this->success('وضعیت تولید با موفقیت دریافت شد.', [
|
|
'generation' => new TemplateGenerationResource($generation),
|
|
]);
|
|
}
|
|
|
|
#[OA\Get(
|
|
path: '/generations/{uuid}/download',
|
|
description: 'Download the generated presentation file',
|
|
summary: 'Download generated presentation',
|
|
security: [['sanctum' => []]],
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
|
|
],
|
|
responses: [
|
|
new OA\Response(response: 200, description: 'File download'),
|
|
new OA\Response(response: 401, description: 'Unauthenticated'),
|
|
new OA\Response(response: 403, description: 'Forbidden'),
|
|
new OA\Response(response: 404, description: 'Not found or not ready'),
|
|
]
|
|
)]
|
|
public function download(TemplateGeneration $generation): StreamedResponse
|
|
{
|
|
abort_if($generation->requested_by !== request()->user()->id, Response::HTTP_FORBIDDEN);
|
|
abort_if($generation->status !== TemplateGenerationStatus::Completed || ! $generation->output_path, Response::HTTP_NOT_FOUND);
|
|
|
|
return Storage::disk('local')->download($generation->output_path, $generation->downloadFilename().'.pptx');
|
|
}
|
|
|
|
private function buildGenerationTitle(Template $template, Request $request): string
|
|
{
|
|
$label = $request->user()?->name ?: 'کاربر';
|
|
|
|
return sprintf('%s - %s - %s', $template->title, $label, Jalali::date(now()));
|
|
}
|
|
}
|