diff --git a/app/Http/Controllers/Bo/AnswerController.php b/app/Http/Controllers/Bo/AnswerController.php index e93a74f..2972b6f 100644 --- a/app/Http/Controllers/Bo/AnswerController.php +++ b/app/Http/Controllers/Bo/AnswerController.php @@ -47,7 +47,7 @@ class AnswerController extends Controller $this->sortQuery($query); $searchFields = trans('Réponse'); - $answerModels = $this->paginate($this->getPagination()); + $answerModels = $query->paginate($this->getPagination()); return view('bo.pages.answers.index', compact('answerModels', 'questionModel', 'searchFields')); } diff --git a/app/Http/Controllers/Bo/ClaimController.php b/app/Http/Controllers/Bo/ClaimController.php index a6586bc..1fcde8a 100644 --- a/app/Http/Controllers/Bo/ClaimController.php +++ b/app/Http/Controllers/Bo/ClaimController.php @@ -16,6 +16,7 @@ use App\Models\Claim; use App\Models\ClaimMailTemplate; use App\Models\ClaimMessage; use App\Models\ClaimSubject; +use App\Models\CustomerInfo; use App\Traits\Controllers\UpdateModelPublished; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -92,12 +93,12 @@ class ClaimController extends Controller */ public function edit(Claim $claim): \Illuminate\Contracts\View\View { - $customerClaimModels = !is_null($claim->customer) ? $this->paginate(Claim::query() + $customerClaimModels = !is_null($claim->customer) ? Claim::query() ->whereNotNull('customer_id') ->whereHas('brand', fn($q) => $q->whereKey($claim->brand->getKey())) ->whereHas('customer', fn($q) => $q->whereKey($claim->customer->getKey())) ->whereNot('id', $claim->getKey()) - ->whereNot('status', ClaimStatus::treated)) : collect(); + ->whereNot('status', ClaimStatus::treated)->paginate($this->jsonItemPerPagination) : collect(); $brandModels = CollectionHelper::paginate( Brand::getUserAssociatedBrands(), $this->jsonItemPerPagination, @@ -111,6 +112,7 @@ class ClaimController extends Controller ClaimMailTemplate::getAllModelsFromBrand($claim->brand->getKey()), $this->jsonItemPerPagination, ); + $customerModel = CustomerInfo::query()->where('hashcode', $claim->hashcode)->first(); return view('bo.pages.claims.edit', [ 'claimModel' => $claim->load(['brand', 'claimSubject']), @@ -119,6 +121,7 @@ class ClaimController extends Controller 'claimMessageModels' => $claimMessageModels, 'claimSubjectModels' => $claimSubjectModels, 'claimTemplateModels' => $claimTemplateModels, + 'customerModel' => $customerModel, ]); } @@ -168,7 +171,7 @@ class ClaimController extends Controller "%s %s %s %s
%s", $userModel->first_name, $userModel->last_name, - trans('a modifié le :field en', ['field' => trans('validation.custom.response_type')]), + trans('a modifié :field en', ['field' => trans('validation.custom.response_type')]), $claim->response_type->label(), clean($request->validated()['content']), ); diff --git a/app/Http/Requests/Api/V4/SendClaimRequest.php b/app/Http/Requests/Api/V4/SendClaimRequest.php index ff9f649..a6c232f 100644 --- a/app/Http/Requests/Api/V4/SendClaimRequest.php +++ b/app/Http/Requests/Api/V4/SendClaimRequest.php @@ -82,7 +82,7 @@ class SendClaimRequest extends BaseApiRequest 'message' => 'required|string|min:3', 'brand_id' => 'required|integer|exists:brands,id', 'subject_id' => 'required|integer|exists:claim_subjects,id', - 'customer_id' => 'sometimes|nullable|integer|exists:customers,id', + 'hashcode' => 'sometimes|nullable|string|size:36', ]; } } diff --git a/app/Http/Requests/Bo/ClaimSubjects/ClaimSubjectRequest.php b/app/Http/Requests/Bo/ClaimSubjects/ClaimSubjectRequest.php index 0f7cde8..6ef52c7 100644 --- a/app/Http/Requests/Bo/ClaimSubjects/ClaimSubjectRequest.php +++ b/app/Http/Requests/Bo/ClaimSubjects/ClaimSubjectRequest.php @@ -38,7 +38,7 @@ class ClaimSubjectRequest extends FormRequest { return [ 'brand_id' => trans('models.classes.brands'), - 'label' => trans('validation.attributes.label'), + 'label' => trans('validation.custom.label'), ]; } } diff --git a/app/Listeners/AddClaimMessageSystem.php b/app/Listeners/AddClaimMessageSystem.php index 7e4570f..119148b 100644 --- a/app/Listeners/AddClaimMessageSystem.php +++ b/app/Listeners/AddClaimMessageSystem.php @@ -68,7 +68,7 @@ class AddClaimMessageSystem { $enumLabel = $event->claim->{$event->field}->label(); $enumColor = $event->claim->{$event->field}->color(); - $baseMessage = trans(':field est passé à', ['field' => $event->label]); + $baseMessage = trans('Modification :field en', ['field' => $event->label]); // If the user is available, include user details in the message. if ($event->user) { @@ -76,7 +76,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é :field en', ['field' => $event->label]), $enumColor, $enumLabel, ); @@ -101,7 +101,7 @@ class AddClaimMessageSystem { $field = $event->claim->{$event->field}; $label = Str::of($event->label)->lower()->singular(); - $baseMessage = trans(':field est passé à', ['field' => $label]); + $baseMessage = trans('Modification :field en', ['field' => $label]); $fieldLabel = $field->label ?? $field->name; // If the user is available, include user details in the message. @@ -110,7 +110,7 @@ class AddClaimMessageSystem "%s %s %s %s", $event->user->first_name, $event->user->last_name, - trans('a modifié le :field en', ['field' => $label]), + trans('a modifié :field en', ['field' => $label]), $fieldLabel, ); } diff --git a/app/Models/Claim.php b/app/Models/Claim.php index 5b23fcb..1a23fda 100644 --- a/app/Models/Claim.php +++ b/app/Models/Claim.php @@ -26,7 +26,7 @@ use Illuminate\Support\Str; * @property \App\Enums\Claims\ClaimType $type Claim type (ClaimType enum). * @property \App\Models\Brand $brand_id Claim associated Brand. * @property \App\Models\ClaimSubject $subject_id Claim associated Subject. - * @property \App\Models\Customer|null $customer_id Claim associated Customer. + * @property string|null $hashcode Claim associated Customer hashcode. * @property-read \Illuminate\Support\Carbon $created_at Claim created date. * @property-read \Illuminate\Support\Carbon $updated_at Claim updated date. */ @@ -54,7 +54,7 @@ class Claim extends Model 'type', 'brand_id', 'subject_id', - 'customer_id', + 'hashcode', ]; /** @@ -153,16 +153,6 @@ class Claim extends Model return $this->belongsTo(ClaimSubject::class, 'subject_id'); } - /** - * Customer subject belongs to relation. - * - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo - { - return $this->belongsTo(Customer::class); - } - /** * ClaimMessage has many relation. * diff --git a/app/Policies/ClaimPolicy.php b/app/Policies/ClaimPolicy.php index 0d4c23a..a6ea601 100644 --- a/app/Policies/ClaimPolicy.php +++ b/app/Policies/ClaimPolicy.php @@ -3,6 +3,7 @@ namespace App\Policies; use App\Enums\Users\RoleEnum; +use App\Models\Claim; use App\Models\User; use Illuminate\Auth\Access\HandlesAuthorization; use App\Policies\Rules\UserStaticRules as USR; @@ -12,16 +13,6 @@ class ClaimPolicy { use HandlesAuthorization; - /** - * View a Claim. - * - * @return boolean - */ - public function view(): bool - { - return USR::everyOne(); - } - /** * View any a Claim. * @@ -35,21 +26,25 @@ class ClaimPolicy /** * Preview a Claim. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function preview(): bool + public function preview(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); } /** * Update a Claim. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function update(): bool + public function update(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); } /** @@ -66,40 +61,65 @@ class ClaimPolicy /** * Determine whether the user can update the associated subject of the model. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function updateSubject(): bool + public function updateSubject(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); } /** * Update a Claim. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function updateType(): bool + public function updateType(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); } /** * Update a Claim. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function updateStatus(): bool + public function updateStatus(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); } /** * Update a Claim. * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim * @return boolean */ - public function updateResponseType(): bool + public function updateResponseType(User $authUser, Claim $claim): bool { - return USR::everyOne(); + return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim); + } + + /** + * Check if the user an access a specific resource. + * + * @param \App\Models\User $authUser + * @param \App\Models\Claim $claim + * @return boolean + */ + private function checkOnlyMinAdminOrInUserBrands(User $authUser, Claim $claim): bool + { + $userBrandModelKeys = $authUser->brands->modelKeys(); + $claimModelKey = $claim->brand->getKey(); + return UserStaticRules::atLeastRole($authUser, RoleEnum::admin) || ( + UserStaticRules::atLeastRole($authUser, RoleEnum::marketing) + && \in_array($claimModelKey, $userBrandModelKeys) + ); } } diff --git a/app/Policies/UserPolicy.php b/app/Policies/UserPolicy.php index 7b68a5f..9cdf972 100644 --- a/app/Policies/UserPolicy.php +++ b/app/Policies/UserPolicy.php @@ -126,4 +126,17 @@ class UserPolicy UserStaticRules::atLeastRole($authUser, RoleEnum::conceptor) and UserStaticRules::atLeastRole($authUser, $user->role); } + + /** + * Determine whether the user can update associated brands to the model. + * + * @param \App\Models\User $authUser + * @param \App\Models\User $user + * @return booleanean + */ + public function changeBrands(User $authUser, User $user): bool + { + return UserStaticRules::atLeastRole($authUser, RoleEnum::conceptor) && + $user->role->equals(RoleEnum::marketing); + } } diff --git a/composer.json b/composer.json index 3c04fea..4d1d30c 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,11 @@ { "name": "ldc/ldccrm", - "version": "4.1.6", + "version": "4.1.7", "type": "project", "description": "LDC CRM", "license": "MIT", "support": { - "email": "equipe.web@ldc.fr" + "email": "digital@ldc.fr" }, "keywords": [ "framework", diff --git a/database/migrations/2025_10_06_142426_alter_claim_customer_association.php b/database/migrations/2025_10_06_142426_alter_claim_customer_association.php new file mode 100644 index 0000000..bd07b47 --- /dev/null +++ b/database/migrations/2025_10_06_142426_alter_claim_customer_association.php @@ -0,0 +1,36 @@ +dropForeign('claims_customer_id_foreign'); + $table->dropColumn('customer_id'); + $table->string('hashcode')->nullable()->unique()->after('id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('claims', function (Blueprint $table) { + $table->dropColumn('hashcode'); + $table->foreignId('customer_id')->nullable()->comment('Claim associated Customer.') + ->constrained('customers')->onUpdate('cascade')->onDelete('set null'); + }); + } +}; diff --git a/resources/ts/bo/components/ClaimActionsComponent.vue b/resources/ts/bo/components/ClaimActionsComponent.vue index e11e71f..07a22c6 100644 --- a/resources/ts/bo/components/ClaimActionsComponent.vue +++ b/resources/ts/bo/components/ClaimActionsComponent.vue @@ -5,7 +5,7 @@ > - +