Merge branch 'issue-353-mails' into 'develop'

Issue 353 mails

See merge request ldc-web/projets/le-gaulois!383
This commit is contained in:
Jérémy 2026-09-28 11:13:54 +02:00
commit 36f350accd
4 changed files with 119 additions and 29 deletions

View file

@ -5,10 +5,15 @@ namespace App\Policies;
use App\Enums\Users\RoleEnum;
use App\Models\User;
use App\Policies\Rules\UserStaticRules;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
class UserPolicy
{
public function __construct(
private Application $application
) {}
/**
* Determine whether the user can view any models.
*/
@ -54,7 +59,7 @@ class UserPolicy
*/
public function duplicate(Model $authModel, User $user): bool
{
return $authModel->getRouteKey() !== $user->getRouteKey() and UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
}
/**
@ -62,11 +67,11 @@ class UserPolicy
*/
public function changePublished(Model $authModel, User $user): bool
{
return $authModel->getRouteKey() !== $user->getRouteKey() and UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
}
public function testMails(User $user): bool
{
return $user->role->value <= RoleEnum::admin->value;
return !$this->application->environment('production') && $user->role->value <= RoleEnum::admin->value;
}
}

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Models\Customer;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@ -9,12 +10,23 @@ use Illuminate\Database\Eloquent\Factories\Factory;
*/
class CustomerFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
protected $model = Customer::class;
#[\Override]
protected function withFaker()
{
return class_exists(\Faker\Generator::class) ? parent::withFaker() : null;
}
public function definition(): array
{
return $this->faker !== null
? $this->definitionWithFaker()
: $this->definitionStatic();
}
/** @return array<string, mixed> */
private function definitionWithFaker(): array
{
return [
'email' => fake()->unique()->email(),
@ -22,4 +34,14 @@ class CustomerFactory extends Factory
'username' => fake()->unique()->userName(),
];
}
/** @return array<string, mixed> */
private function definitionStatic(): array
{
return [
'email' => 'client.test@exemple.org',
'hashcode' => '00000000-0000-0000-0000-000000000000',
'username' => 'client_demo',
];
}
}

View file

@ -16,19 +16,26 @@ use Illuminate\Support\Str;
*/
class OrderFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Order::class;
#[\Override]
protected function withFaker()
{
return class_exists(\Faker\Generator::class) ? parent::withFaker() : null;
}
public function definition(): array
{
return $this->faker !== null
? $this->definitionWithFaker()
: $this->definitionStatic();
}
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
private function definitionWithFaker(): array
{
return [
'ref' => strtoupper(Str::random(10)),
@ -48,18 +55,41 @@ class OrderFactory extends Factory
}
/**
* Attache des articles vendus (SellItem) avec leur quantité dans la table pivot.
* @return array<string, mixed>
*/
private function definitionStatic(): array
{
return [
'ref' => 'CMD-TEST-12345',
'firstname' => 'Alexandre',
'lastname' => 'Dupont',
'address' => '123 Rue de la République',
'postcode' => '75001',
'phone' => '0102030405',
'email' => 'client.test@exemple.org',
'city' => 'Paris',
'civility' => ContactFormCivility::m,
'status' => OrderStatus::waitingCheck,
'payementType' => OrderType::check,
'confirmMailSent' => true,
'customer_id' => 1,
];
}
public function withSellItems(int $count = 3, int $quantityMin = 1, int $quantityMax = 5): static
{
return $this->afterCreating(function (Order $order) use ($count, $quantityMin, $quantityMax) {
$items = SellItem::factory()->count($count)->create();
foreach ($items as $item) {
$order->sellItems()->attach($item->id, [
'quantity' => fake()->numberBetween($quantityMin, $quantityMax),
]);
}
$pivotData = $items->mapWithKeys(function (SellItem $item) use ($quantityMin, $quantityMax) {
$quantity = $this->faker !== null
? fake()->numberBetween($quantityMin, $quantityMax)
: 1;
return [$item->id => ['quantity' => $quantity]];
})->all();
$order->sellItems()->attach($pivotData);
});
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Enums\Shop\SellItemsType;
use App\Models\SellItem;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@ -10,15 +11,29 @@ use Illuminate\Database\Eloquent\Factories\Factory;
*/
class SellItemFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
protected $model = SellItem::class;
#[\Override]
protected function withFaker()
{
return class_exists(\Faker\Generator::class) ? parent::withFaker() : null;
}
public function definition(): array
{
return $this->faker !== null
? $this->definitionWithFaker()
: $this->definitionStatic();
}
/**
* @return array<string, mixed>
*/
private function definitionWithFaker(): array
{
return [
'name' => fake()->words(rand(1, 2), true),
'name' => fake()->words(fake()->numberBetween(1, 2), true),
'picture' => '',
'picture_alt' => fake()->sentence(),
'picture_title' => fake()->sentence(),
@ -26,7 +41,25 @@ class SellItemFactory extends Factory
'arcadia_weight' => fake()->numberBetween(1, 100),
'arcadia_id' => fake()->unique()->numberBetween(100, 500),
'order' => 1,
'type' => \collect(SellItemsType::toArray())->random()->value,
'type' => fake()->randomElement(SellItemsType::cases()),
];
}
/**
* @return array<string, mixed>
*/
private function definitionStatic(): array
{
return [
'name' => 'Article Démo Test',
'picture' => '',
'picture_alt' => 'Image descriptive article démo',
'picture_title' => 'Titre image démo',
'price' => 29.99,
'arcadia_weight' => 500,
'arcadia_id' => 101,
'order' => 1,
'type' => SellItemsType::cases()[0] ?? 'standard',
];
}
}