diff --git a/app/Http/Controllers/Api/V4/ClaimController.php b/app/Http/Controllers/Api/V4/ClaimController.php index 01945e2..1742216 100644 --- a/app/Http/Controllers/Api/V4/ClaimController.php +++ b/app/Http/Controllers/Api/V4/ClaimController.php @@ -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.'); } diff --git a/app/Http/Requests/Api/V4/SendClaimRequest.php b/app/Http/Requests/Api/V4/SendClaimRequest.php index 08af906..ff9f649 100644 --- a/app/Http/Requests/Api/V4/SendClaimRequest.php +++ b/app/Http/Requests/Api/V4/SendClaimRequest.php @@ -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', diff --git a/app/Listeners/AddClaimMessageSystem.php b/app/Listeners/AddClaimMessageSystem.php index 086568f..7e4570f 100644 --- a/app/Listeners/AddClaimMessageSystem.php +++ b/app/Listeners/AddClaimMessageSystem.php @@ -5,6 +5,7 @@ namespace App\Listeners; use App\Enums\Claims\ClaimMessageType; use App\Events\ClaimModelChanged; use App\Models\ClaimMessage; +use Illuminate\Support\Str; class AddClaimMessageSystem { @@ -30,7 +31,7 @@ class AddClaimMessageSystem * @param \App\Events\ClaimModelChanged $event * @return \App\Models\ClaimMessage */ - private function createClaimMessage(ClaimModelChanged $event): ClaimMessage + private function createClaimMessage(ClaimModelChanged $event): \App\Models\ClaimMessage { return new ClaimMessage([ 'claim_id' => $event->claim->getKey(), @@ -99,7 +100,8 @@ class AddClaimMessageSystem private function generateDefaultFieldMessageContent(ClaimModelChanged $event): string { $field = $event->claim->{$event->field}; - $baseMessage = trans(':field est passé à', ['field' => $event->label]); + $label = Str::of($event->label)->lower()->singular(); + $baseMessage = trans(':field est passé à', ['field' => $label]); $fieldLabel = $field->label ?? $field->name; // If the user is available, include user details in the message. @@ -108,7 +110,7 @@ class AddClaimMessageSystem "%s %s %s %s", $event->user->first_name, $event->user->last_name, - trans('a modifié le :field en', ['field' => $event->label]), + trans('a modifié le :field en', ['field' => $label]), $fieldLabel, ); } diff --git a/app/Models/ClaimMailTemplate.php b/app/Models/ClaimMailTemplate.php index 8393ead..c461d3b 100644 --- a/app/Models/ClaimMailTemplate.php +++ b/app/Models/ClaimMailTemplate.php @@ -29,6 +29,24 @@ class ClaimMailTemplate extends Model 'brand_id', ]; + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted(): void + { + static::created(function () { + self::resetCache(); + }); + static::updated(function () { + self::resetCache(); + }); + static::deleted(function () { + self::resetCache(); + }); + } + // * METHODS /** diff --git a/app/Models/ClaimSubject.php b/app/Models/ClaimSubject.php index 6b38d68..70e2f89 100644 --- a/app/Models/ClaimSubject.php +++ b/app/Models/ClaimSubject.php @@ -27,6 +27,24 @@ class ClaimSubject extends Model 'brand_id', ]; + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted(): void + { + static::created(function () { + self::resetCache(); + }); + static::updated(function () { + self::resetCache(); + }); + static::deleted(function () { + self::resetCache(); + }); + } + // * METHODS /** diff --git a/composer.json b/composer.json index 7868b85..34b0378 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "ldc/ldccrm", - "version": "4.1.0", + "version": "4.1.1", "type": "project", "description": "LDC CRM", "license": "MIT", diff --git a/resources/ts/bo/components/ClaimActionsComponent.vue b/resources/ts/bo/components/ClaimActionsComponent.vue index 2f732c1..e11e71f 100644 --- a/resources/ts/bo/components/ClaimActionsComponent.vue +++ b/resources/ts/bo/components/ClaimActionsComponent.vue @@ -60,12 +60,12 @@ for="brand" class="col-form-label" > - {{ trans.methods.__('Modifier la :model', { model: trans.methods.__("models.classes.claimsubjects") }) }} * + {{ trans.methods.__('Modifier le :model', { model: claimsubjectLabel }) }} * @@ -85,7 +85,7 @@