ldc-cocotte/app/Jobs/SendReminderMail.php

133 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Executor;
use App\Cqs\Actions\Orders\ClaimTodayOrderReminder;
use App\Cqs\Actions\Orders\ClaimTodayOrderReminderInput;
use App\Dto\OrderReminderDto;
use App\Dto\OrderReminderLineDto;
use App\Models\Employee;
use App\Models\Order;
use App\Notifications\OrderTodayReminderNotification;
use Carbon\CarbonImmutable;
use Domain\Enums\OrderStatus;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Date;
use Money\MoneyFormatter;
class SendReminderMail implements ShouldBeUnique, ShouldQueue
{
use Queueable;
public int $uniqueFor = 3600;
private const int CHUNK_SIZE = 100;
public function __construct(
private readonly MoneyFormatter $moneyFormatter
) {
}
public function uniqueId(): string
{
return Date::now()->format('Y-m-d');
}
public function handle(Executor $executor, ClaimTodayOrderReminder $claimAction): void
{
$today = Date::now()->format('Y-m-d');
Order::query()
->with(['lines', 'shop'])
->whereDate('pickup_day', $today)
->where('status', OrderStatus::VALIDATED)
->whereNull('reminded_at')
->chunkById(
self::CHUNK_SIZE,
function (Collection $orders) use ($executor, $claimAction): void {
/** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\Order> $orders */
$this->processChunk($orders, $executor, $claimAction);
},
'id'
);
}
/**
* @param \Illuminate\Database\Eloquent\Collection<int, \App\Models\Order> $orders
*/
private function processChunk(
Collection $orders,
Executor $executor,
ClaimTodayOrderReminder $claimAction,
): void {
$matricules = $orders
->map(fn (Order $order): string => $order->employee->matricule->value)
->unique()
->values();
$employees = Employee::query()
->with('customer')
->whereIn('matricule', $matricules)
->get()
->keyBy(fn (Employee $employee): string => $employee->matricule->value);
foreach ($orders as $order) {
$employee = $employees->get($order->employee->matricule->value);
if (! $employee instanceof Employee) {
continue;
}
$claimed = $executor->execute(
$claimAction,
new ClaimTodayOrderReminderInput($order->getId(), CarbonImmutable::now()),
);
if (! $claimed) {
continue;
}
$this->notifyEmployee(
$employee,
$this->toDto($order, $employee)
);
}
}
private function notifyEmployee(Employee $employee, OrderReminderDto $dto): void
{
$employee->notify(new OrderTodayReminderNotification($dto));
}
private function toDto(Order $order, Employee $employee): OrderReminderDto
{
$linesDto = [];
foreach ($order->lines as $line) {
$linesDto[] = new OrderReminderLineDto(
name: $line->product->name ?? '',
quantity: $line->quantity,
priceKgTtc: $this->moneyFormatter->format($line->product->priceKgTtc),
totalTtc: $this->moneyFormatter->format($line->total_ttc),
);
}
return new OrderReminderDto(
firstname: $employee->prenom,
lastname: $employee->nom,
reference: (string) $order->reference,
total: $this->moneyFormatter->format($order->total),
pickupDayFormatted: $order->pickup_day ? $order->pickup_day->translatedFormat('l j F Y') : '',
lines: $linesDto,
shopName: $order->shop ? $order->shop->name : '',
shopAddress: $order->shop ? $order->shop->address : '',
shopPostcode: $order->shop ? $order->shop->postcode : '',
shopCity: $order->shop ? $order->shop->city : ''
);
}
}