Merge branch 'cache' into 'develop'
fix: 🐛 update saving claim file
See merge request ldc-web/projets/crm!37
This commit is contained in:
commit
06694f0a48
10 changed files with 159 additions and 15 deletions
|
|
@ -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.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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
|
|||
"<b>%s %s</b> %s <b>%s</b>",
|
||||
$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,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ldc/ldccrm",
|
||||
"version": "4.1.0",
|
||||
"version": "4.1.1",
|
||||
"type": "project",
|
||||
"description": "LDC CRM",
|
||||
"license": "MIT",
|
||||
|
|
|
|||
|
|
@ -60,12 +60,12 @@
|
|||
for="brand"
|
||||
class="col-form-label"
|
||||
>
|
||||
<b>{{ trans.methods.__('Modifier la :model', { model: trans.methods.__("models.classes.claimsubjects") }) }} *</b>
|
||||
<b>{{ trans.methods.__('Modifier le :model', { model: claimsubjectLabel }) }} *</b>
|
||||
</label>
|
||||
<SearchBelongsToDropdown
|
||||
id="subject"
|
||||
name="subject_id"
|
||||
:label="trans.methods.__('Modifier la :model', { model: trans.methods.__('models.classes.claimsubjects') })"
|
||||
:label="trans.methods.__('Modifier le :model', { model: claimsubjectLabel })"
|
||||
:model="claimModel"
|
||||
targetclass="App\Models\ClaimSubject"
|
||||
:modellistpaginate="claimSubjectModels"
|
||||
|
|
@ -76,7 +76,7 @@
|
|||
:multiple="false"
|
||||
:required="true"
|
||||
:disabled="loading || isTreatedClaim"
|
||||
:placeholder="trans.methods.__('Rechercher une :model', { model: trans.methods.__('models.classes.claimsubjects') })"
|
||||
:placeholder="trans.methods.__('Rechercher une :model', { model: claimsubjectLabel })"
|
||||
@selected-value-changed="setSelectedSubject"
|
||||
/>
|
||||
<small class="text-danger m-0">
|
||||
|
|
@ -85,7 +85,7 @@
|
|||
</td>
|
||||
<template v-else>
|
||||
<td class="align-middle fw-bold">
|
||||
{{ trans.methods.__("models.classes.claimsubjects") }}
|
||||
{{ claimsubjectLabel }}
|
||||
</td>
|
||||
<td class="align-middle">
|
||||
{{ selectParameters.subject?.label }}
|
||||
|
|
@ -301,7 +301,8 @@ const selectParameters = reactive<{
|
|||
|
||||
// * COMPUTED
|
||||
const isUsedWithProps = computed<boolean>(() => attrs.json === undefined);
|
||||
const brandLabel = computed(() => trans.methods.__("models.classes.brands").toString().replace(/s$/, ""));
|
||||
const brandLabel = computed(() => trans.methods.__("models.classes.brands").toString().toLowerCase().replace(/s$/, ""));
|
||||
const claimsubjectLabel = computed(() => trans.methods.__("models.classes.claimsubjects").toLowerCase().toString().replace(/s$/, ""));
|
||||
const isTreatedClaim = computed(() => Object.values(enumParameters.claimStatus).find(e => e.name === "treated")?.value == selectParameters.enums["status"]);
|
||||
|
||||
// * LIFECYCLE
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@
|
|||
@can('preview', $claimModel)
|
||||
{{-- PREVIEW TRIGGER MODAL --}}
|
||||
<button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal"
|
||||
data-bs-target="#previewModal{{ $claimModel->getKey() }}"
|
||||
data-bs-tooltip="tooltip" title="{{ str(__('crud.actions.duplicate'))->ucfirst() }}">
|
||||
data-bs-target="#previewModal{{ $claimModel->getKey() }}" data-bs-tooltip="tooltip"
|
||||
title="{{ __('Afficher cette :model', ['model' => Str::singular(__('models.classes.claims'))]) }}">
|
||||
<i class="fa-solid fa-eye"></i>
|
||||
</button>
|
||||
@endcan
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
71
tests/Client/CrmClientClaimTest.php
Normal file
71
tests/Client/CrmClientClaimTest.php
Normal 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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue