fix: 🐛 update saving claim file

This commit is contained in:
Alexis Gousseau 2025-08-26 09:34:58 +02:00
parent 4a23d74917
commit eeb467d1f0
No known key found for this signature in database
GPG key ID: 4A614CB3DBBA2406
4 changed files with 109 additions and 4 deletions

View file

@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V4\SendClaimRequest;
use App\Models\Claim;
use App\Models\Service;
use Illuminate\Support\Arr;
class ClaimController extends Controller
{
@ -20,7 +21,11 @@ class ClaimController extends Controller
*/
public function store(SendClaimRequest $request, Service $service): \Illuminate\Http\JsonResponse
{
$claimModel = (new Claim())->fill($request->validated());
$claimModel = (new Claim())->fill(Arr::except($request->validated(), 'file'));
if ($request->hasFile('file')) {
$claimModel->file = $request->file('file')->store('claims/files');
}
if (!$claimModel->saveOrFail()) {
throw new CrmException('Api failed to store claim.');
}

View file

@ -53,7 +53,32 @@ class SendClaimRequest extends BaseApiRequest
'address' => 'sometimes|nullable|string|max:255',
'city' => 'sometimes|nullable|string|between:2,255',
'postcode' => ['sometimes', 'nullable', 'string', "postal_code:{$countryList}"],
'file' => 'sometimes|nullable|mimes:jpg,jpeg,png,pdf|max:5120',
'file' => [
'sometimes',
'nullable',
'string',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) {
// Base64 validation.
$decoded = base64_decode($value, true);
if ($decoded === false) {
return $fail('Le fichier doit être un encodage base64 valide.');
}
// Check the size (5 MB max = 5 * 1024 * 1024).
if (strlen($decoded) > 5 * 1024 * 1024) {
return $fail('Le fichier est trop volumineux (max 5 Mo).');
}
// Check the MIME type.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $decoded);
$allowed = ['image/jpeg', 'image/png', 'application/pdf'];
if (!in_array($mime, $allowed)) {
return $fail('Le fichier doit être une image JPG/PNG ou un PDF.');
}
}
],
'message' => 'required|string|min:3',
'brand_id' => 'required|integer|exists:brands,id',
'subject_id' => 'required|integer|exists:claim_subjects,id',

View file

@ -24,6 +24,7 @@ class ClaimTest extends ApiBase
{
parent::setUp();
$image = UploadedFile::fake()->image('photo.jpg');
$this->skeleton = [
'first_name' => 'Jean',
'last_name' => 'Bond',
@ -32,7 +33,7 @@ class ClaimTest extends ApiBase
'address' => '10 rue des totos',
'city' => 'Le Mans',
'postcode' => '72100',
'file' => UploadedFile::fake()->image('photo.jpg'),
'file' => base64_encode(file_get_contents($image->getRealPath())),
];
}
@ -132,10 +133,13 @@ class ClaimTest extends ApiBase
$subject = ClaimSubject::query()->firstOrNew();
$customer = Customer::query()->firstOrNew();
// Create fake .gif base64.
$base64Gif = 'R0lGODlhAQABAIAAAAUEBA==';
$response = $this->callApiWithToken($service, 'post', route('apiV4.claim.store'), \array_merge(
Arr::except($this->skeleton, ['address', 'city', 'postcode', 'file']),
[
'file' => \Illuminate\Http\UploadedFile::fake()->create('x.gif', 100, 'image/gif'),
'file' => $base64Gif,
'brand_id' => $brand->getKey(),
'subject_id' => $subject->getKey(),
'customer_id' => $customer->getKey(),

View file

@ -0,0 +1,71 @@
<?php
namespace Tests\Client;
use App\Models\Claim as ModelsClaim;
use LDC\LDCCRM\Models\Claim;
use Illuminate\Http\UploadedFile;
use LDC\LDCCRM\Exceptions\ValidationException;
class CrmClientClaimTest extends CrmClientTester
{
/**
* Create new claim.
*
* @return void
*/
public function testCreateClaim(): void
{
$image = UploadedFile::fake()->image('photo.jpg');
$this->assertTrue(Claim::setClaim(
'Jean',
'Bond',
'claim@hotmail.fr',
'Ceci est un message.',
1,
1,
'10 rue des totos',
'Le Mans',
'72100',
base64_encode(file_get_contents($image->getRealPath())),
));
$this->assertTrue(ModelsClaim::query()->where('email', 'claim@hotmail.fr')->exists());
}
/**
* Create new claim without optionnal fields.
*
* @return void
*/
public function testCreateClaimWithoutOptionnalFields(): void
{
$this->assertTrue(Claim::setClaim(
'Jean',
'Bond',
'claim@hotmail.fr',
'Ceci est un message.',
1,
1,
));
$this->assertTrue(ModelsClaim::query()->where('email', 'claim@hotmail.fr')->exists());
}
/**
* Create new claim should fail.
*
* @return void
*/
public function testCreateClaimShouldFail(): void
{
$this->expectException(ValidationException::class);
Claim::setClaim(
'Jean',
'Bond',
'claim@hotmail.fr',
'Ceci est un message.',
0,
0,
);
$this->assertSame(0, ModelsClaim::count());
}
}