2026-03-11 23:33:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Client;
|
|
|
|
|
|
|
|
|
|
use App\Enums\ActorType;
|
2026-03-12 18:25:32 +00:00
|
|
|
use App\Enums\DeclarationStatus;
|
2026-03-11 23:33:10 +00:00
|
|
|
use App\Enums\MessageType;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2026-03-12 18:25:32 +00:00
|
|
|
use App\Mail\DeclarationTextMessageMail;
|
2026-03-11 23:33:10 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
|
|
|
|
|
class UploadController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Show the client upload page.
|
|
|
|
|
*/
|
|
|
|
|
public function show(Request $request, string $token): Response
|
|
|
|
|
{
|
2026-03-12 18:25:32 +00:00
|
|
|
$invitation = $request->attributes->get('declaration_invitation');
|
|
|
|
|
$declaration = $invitation->declaration;
|
2026-03-11 23:33:10 +00:00
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
$declaration->load(['client']);
|
|
|
|
|
$documents = $declaration->getMedia('documents')->map(fn ($m) => [
|
2026-03-11 23:33:10 +00:00
|
|
|
'id' => $m->id,
|
|
|
|
|
'name' => $m->name,
|
|
|
|
|
'file_name' => $m->file_name,
|
|
|
|
|
'size' => $m->human_readable_size,
|
|
|
|
|
'created_at' => $m->created_at->format('d/m/Y H:i'),
|
|
|
|
|
])->values()->all();
|
|
|
|
|
|
|
|
|
|
return Inertia::render('client/Upload', [
|
2026-03-12 18:25:32 +00:00
|
|
|
'declaration' => [
|
|
|
|
|
'id' => $declaration->id,
|
|
|
|
|
'title' => $declaration->title,
|
|
|
|
|
'client_name' => $declaration->client->company_name,
|
2026-03-11 23:33:10 +00:00
|
|
|
],
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'documents' => $documents,
|
|
|
|
|
'uploadUrl' => route('client.upload.store', ['token' => $token]),
|
|
|
|
|
'csrfToken' => csrf_token(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store uploaded files.
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request, string $token): RedirectResponse
|
|
|
|
|
{
|
2026-03-12 18:25:32 +00:00
|
|
|
$invitation = $request->attributes->get('declaration_invitation');
|
|
|
|
|
$declaration = $invitation->declaration;
|
2026-03-11 23:33:10 +00:00
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'files' => ['required', 'array', 'min:1'],
|
|
|
|
|
'files.*' => ['file', 'max:10240'],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
$message = $declaration->messages()->create([
|
2026-03-11 23:33:10 +00:00
|
|
|
'type' => MessageType::Text,
|
|
|
|
|
'body' => 'Documents déposés par le client.',
|
|
|
|
|
'sent_by_type' => ActorType::Client,
|
2026-03-12 18:25:32 +00:00
|
|
|
'sent_by_id' => $declaration->client_id,
|
2026-03-11 23:33:10 +00:00
|
|
|
'metadata' => ['invitation_id' => $invitation->id],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
foreach ($request->file('files') as $file) {
|
2026-03-12 18:25:32 +00:00
|
|
|
$declaration->addMedia($file)
|
2026-03-11 23:33:10 +00:00
|
|
|
->withCustomProperties([
|
|
|
|
|
'message_id' => $message->id,
|
|
|
|
|
'uploaded_by_type' => ActorType::Client,
|
2026-03-12 18:25:32 +00:00
|
|
|
'uploaded_by_id' => $declaration->client_id,
|
2026-03-11 23:33:10 +00:00
|
|
|
])
|
|
|
|
|
->toMediaCollection('documents');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
// Only transition to en_cours if the current status allows it
|
|
|
|
|
$allowed = DeclarationStatus::allowedTransitions()[$declaration->status->value] ?? [];
|
|
|
|
|
if (in_array(DeclarationStatus::EnCours, $allowed)) {
|
|
|
|
|
$declaration->update(['status' => DeclarationStatus::EnCours]);
|
|
|
|
|
}
|
2026-03-11 23:33:10 +00:00
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
$recipient = $declaration->assignee ?? $declaration->creator;
|
2026-03-11 23:33:10 +00:00
|
|
|
if ($recipient?->email) {
|
|
|
|
|
Mail::to($recipient->email)->send(
|
2026-03-12 18:25:32 +00:00
|
|
|
new DeclarationTextMessageMail($declaration, 'Le client a déposé des documents.', null)
|
2026-03-11 23:33:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return back()->with('flash', ['type' => 'success', 'message' => 'Documents envoyés avec succès.']);
|
|
|
|
|
}
|
|
|
|
|
}
|