76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Notification\Services;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Integration\Services\MailService;
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Domains\Ticket\Models\Ticket;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class NotificationMailService
|
|
{
|
|
public function __construct(
|
|
private readonly MailService $mailService,
|
|
) {}
|
|
|
|
public function sendWelcome(int $userId, string $tenantCode): void
|
|
{
|
|
$tenant = Tenant::query()->where('codigo', $tenantCode)->firstOrFail();
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
$this->mailService
|
|
->forTenant($tenantCode)
|
|
->send(
|
|
$user->email,
|
|
"Bienvenido a {$tenant->nombre}",
|
|
view('mail.notifications.welcome', compact('tenant', 'user'))->render(),
|
|
);
|
|
}
|
|
|
|
public function sendPurchasePaid(int $purchaseId): void
|
|
{
|
|
$purchase = Purchase::query()
|
|
->with(['tenant', 'user', 'items'])
|
|
->findOrFail($purchaseId);
|
|
|
|
$this->mailService
|
|
->forTenant($purchase->tenant_codigo)
|
|
->send(
|
|
$this->recipientFor($purchase),
|
|
"Pago confirmado - Compra #{$purchase->getKey()}",
|
|
view('mail.notifications.purchase-paid', compact('purchase'))->render(),
|
|
);
|
|
}
|
|
|
|
/** @param array<int, int> $ticketIds */
|
|
public function sendTicketsAvailable(int $purchaseId, array $ticketIds): void
|
|
{
|
|
$purchase = Purchase::query()->with(['tenant', 'user'])->findOrFail($purchaseId);
|
|
/** @var Collection<int, Ticket> $tickets */
|
|
$tickets = Ticket::query()
|
|
->where('tenant_code', $purchase->tenant_codigo)
|
|
->where('user_id', $purchase->user_id)
|
|
->whereKey($ticketIds)
|
|
->get();
|
|
|
|
if ($tickets->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$this->mailService
|
|
->forTenant($purchase->tenant_codigo)
|
|
->send(
|
|
$this->recipientFor($purchase),
|
|
'Tus tickets ya están disponibles',
|
|
view('mail.notifications.tickets-available', compact('purchase', 'tickets'))->render(),
|
|
);
|
|
}
|
|
|
|
private function recipientFor(Purchase $purchase): string
|
|
{
|
|
return (string) ($purchase->email ?: $purchase->user?->email);
|
|
}
|
|
}
|