fix: 🐛 standardize ID extraction using modelKeys() instead of pluck('id')

This commit is contained in:
Alexis Gousseau 2025-08-05 09:23:54 +02:00
parent 927ae02240
commit fff623144a
No known key found for this signature in database
GPG key ID: 4A614CB3DBBA2406
18 changed files with 43 additions and 37 deletions

View file

@ -175,7 +175,6 @@ class UserCreate extends Command
[
Str::ucFirst(RoleEnum::conceptor->label()),
Str::ucFirst(RoleEnum::admin->label()),
Str::ucFirst(RoleEnum::visitor->label()),
Str::ucFirst(RoleEnum::marketing->label())
],
Str::ucFirst(RoleEnum::marketing->label()),
@ -186,9 +185,6 @@ class UserCreate extends Command
if ($tmp === Str::ucFirst(RoleEnum::admin->label())) {
$tmp = RoleEnum::admin;
}
if ($tmp === Str::ucFirst(RoleEnum::visitor->label())) {
$tmp = RoleEnum::visitor;
}
if ($tmp === Str::ucFirst(RoleEnum::marketing->label())) {
$tmp = RoleEnum::marketing;
}

View file

@ -52,7 +52,7 @@ class ClaimController extends Controller
fn($q) => $q->whereHas('brand', fn($q) => $q->where('id', $request->brand))
)->when(
$userModel->role->equals(RoleEnum::marketing),
fn($q) => $q->whereHas('brand', fn($q) => $q->whereIn('id', $userModel->brands->pluck('id')))
fn($q) => $q->whereHas('brand', fn($q) => $q->whereIn('id', $userModel->brands->modelKeys()))
);
/** @var string $search Search field */
@ -62,12 +62,14 @@ class ClaimController extends Controller
$claimModels,
$search,
null,
'reference',
'first_name',
'last_name',
'email',
);
}
$searchFields = \implode(', ', [
trans('validation.custom.reference'),
trans('validation.attributes.first_name'),
trans('validation.attributes.last_name'),
trans('validation.attributes.email'),

View file

@ -40,7 +40,7 @@ class ClaimMailTemplateController extends Controller
fn($q) => $q->whereHas('brand', fn($q) => $q->where('id', $request->brand))
)->when(
$userModel->role->equals(RoleEnum::marketing),
fn($q) => $q->whereHas('brand', fn($q) => $q->whereIn('id', $userModel->brands->pluck('id')))
fn($q) => $q->whereHas('brand', fn($q) => $q->whereIn('id', $userModel->brands->modelKeys()))
);
if ($request->search) {

View file

@ -212,7 +212,7 @@ class QuestionController extends Controller
->selectRaw('count(*) as total, answer_id')
->from('model_answer')
->where('model_type', '=', Customer::class)
->whereIn('answer_id', $question->answers->pluck('id'))
->whereIn('answer_id', $question->answers->modelKeys())
->groupBy('answer_id')
->get()
->keyBy('answer_id');

View file

@ -160,7 +160,7 @@ class StatsController extends Controller
$answersByDays = ModelAnswer::query()
->whereDate('created_at', '>=', $start)
->whereDate('created_at', '<=', $end)
->whereIn('answer_id', $answers->pluck('id'))
->whereIn('answer_id', $answers->modelKeys())
->get()->map(function ($custAnswer) {
return [
'answer_id' => $custAnswer->answer_id,

View file

@ -36,14 +36,14 @@ class Answer extends Model
*/
protected static function booted(): void
{
static::created(function () {
self::resetCache();
static::created(function (self $model) {
self::resetCache($model);
});
static::updated(function () {
self::resetCache();
static::updated(function (self $model) {
self::resetCache($model);
});
static::deleted(function () {
self::resetCache();
static::deleted(function (self $model) {
self::resetCache($model);
});
}
@ -65,11 +65,13 @@ class Answer extends Model
/**
* Reset all cache elements related to this model.
*
* @param self $model
* @return void
*/
private static function resetCache(): void
private static function resetCache(self $model): void
{
Cache::forget('answers');
$questionModelKey = $model->question->getKey();
Cache::forget("question-{$questionModelKey}-answers");
self::getAnswers();
}

View file

@ -120,7 +120,7 @@ class Brand extends Model
$brandModels = self::getBrands();
if ($userModel->role->equals(RoleEnum::marketing)) {
$brandIds = $userModel->brands->pluck('id')->all();
$brandIds = $userModel->brands->modelKeys();
$brandModels = $brandModels->filter(function ($brand) use ($brandIds) {
return in_array($brand->getKey(), $brandIds);

View file

@ -51,14 +51,14 @@ class Question extends Model
*/
protected static function booted(): void
{
static::created(function () {
self::resetCache();
static::created(function (self $model) {
self::resetCache($model->getKey());
});
static::updated(function () {
self::resetCache();
static::updated(function (self $model) {
self::resetCache($model->getKey());
});
static::deleted(function () {
self::resetCache();
static::deleted(function (self $model) {
self::resetCache($model->getKey());
});
}
@ -80,11 +80,13 @@ class Question extends Model
/**
* Reset all cache elements related to this model.
*
* @param integer $modelKey
* @return void
*/
private static function resetCache(): void
private static function resetCache(int $modelKey): void
{
Cache::forget('questions');
Cache::forget("question-{$modelKey}-answers");
self::getQuestions();
}

View file

@ -99,7 +99,7 @@ class ClaimMailTemplatePolicy
*/
private function checkOnlyMinAdminOrInUserBrands(User $authUser, ClaimMailTemplate $claim_mail_template): bool
{
$userBrandModelKeys = $authUser->brands->pluck('id')->toArray();
$userBrandModelKeys = $authUser->brands->modelKeys();
$claimMailTemplateModelKey = $claim_mail_template->brand->getKey();
return UserStaticRules::atLeastRole($authUser, RoleEnum::admin) || (
UserStaticRules::atLeastRole($authUser, RoleEnum::marketing)

View file

@ -60,7 +60,7 @@ class ClaimPolicy
*/
public function updateBrand(User $authUser): bool
{
return UserStaticRules::atLeastRole($authUser, RoleEnum::conceptor);
return UserStaticRules::atLeastRole($authUser, RoleEnum::admin);
}
/**

View file

@ -38,9 +38,11 @@ class ClaimFactory extends Factory
'response_type' => ClaimResponseType::none,
'type' => collect(ClaimType::toArray())->random()->value,
'status' => collect(ClaimStatus::toArray())->random()->value,
'brand_id' => $this->faker->randomElement(Brand::pluck('id')),
'subject_id' => $this->faker->randomElement(ClaimSubject::pluck('id')),
'customer_id' => $this->faker->boolean(50) ? $this->faker->randomElement(Customer::pluck('id')) : null,
'brand_id' => $this->faker->randomElement(Brand::query()->get()->modelKeys()),
'subject_id' => $this->faker->randomElement(ClaimSubject::query()->get()->modelKeys()),
'customer_id' => $this->faker->boolean(50)
? $this->faker->randomElement(Customer::query()->get()->modelKeys())
: null,
];
}
}

View file

@ -25,7 +25,7 @@ class ClaimMailTemplateFactory extends Factory
return [
'label' => $this->faker->word(),
'content' => $this->faker->sentence(10),
'brand_id' => $this->faker->randomElement(Brand::pluck('id')),
'brand_id' => $this->faker->randomElement(Brand::query()->get()->modelKeys()),
];
}
}

View file

@ -27,8 +27,10 @@ class ClaimMessageFactory extends Factory
return [
'content' => $this->faker->sentence(10),
'type' => collect(ClaimMessageType::toArray())->random()->value,
'claim_id' => $this->faker->randomElement(Claim::pluck('id')),
'user_id' => $this->faker->boolean(50) ? $this->faker->randomElement(User::pluck('id')) : null,
'claim_id' => $this->faker->randomElement(Claim::query()->get()->modelKeys()),
'user_id' => $this->faker->boolean(50)
? $this->faker->randomElement(User::query()->get()->modelKeys())
: null,
];
}
}

View file

@ -24,7 +24,7 @@ class ClaimSubjectFactory extends Factory
{
return [
'label' => $this->faker->word(),
'brand_id' => $this->faker->randomElement(Brand::pluck('id')),
'brand_id' => $this->faker->randomElement(Brand::query()->get()->modelKeys()),
];
}
}

View file

@ -26,7 +26,7 @@ class SingleOptinFactory extends Factory
return [
'hashcode' => Str::uuid(),
'email' => $this->faker->email(),
'brand_id' => $this->faker->randomElement(Brand::pluck('id')),
'brand_id' => $this->faker->randomElement(Brand::query()->get()->modelKeys()),
];
}
}

View file

@ -41,7 +41,7 @@ class CustomerSeeder extends Seeder
}
);
}
$answerModels = Answer::inRandomOrder()->take(rand(1, 3))->pluck('id');
$answerModels = Answer::inRandomOrder()->take(rand(1, 3))->get()->modelKeys();
$customer->answers()->attach($answerModels);
});
}

View file

@ -16,7 +16,7 @@ class SingleOptinSeeder extends Seeder
public function run(): void
{
SingleOptin::factory(20)->create()->each(function (SingleOptin $singleOptin) {
$answerModels = Answer::inRandomOrder()->take(rand(1, 3))->pluck('id');
$answerModels = Answer::inRandomOrder()->take(rand(1, 3))->get()->modelKeys();
$singleOptin->answers()->attach($answerModels);
});
}

View file

@ -16,7 +16,7 @@ class UserSeeder extends Seeder
public function run(): void
{
User::factory(20)->create()->each(function ($user) {
$brands = Brand::inRandomOrder()->take(rand(1, 3))->pluck('id');
$brands = Brand::inRandomOrder()->take(rand(1, 3))->get()->modelKeys();
$user->brands()->attach($brands);
});
}