complete story 8.4 with qa tests
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Consultation;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Attachment;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class BookingApprovedMail extends Mailable implements ShouldQueue
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public Consultation $consultation,
|
||||
public string $icsContent,
|
||||
public ?string $paymentInstructions = null
|
||||
) {
|
||||
$this->locale = $consultation->user->preferred_language ?? 'ar';
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
$locale = $this->consultation->user->preferred_language ?? 'ar';
|
||||
|
||||
return new Envelope(
|
||||
subject: $locale === 'ar'
|
||||
? 'تمت الموافقة على استشارتك'
|
||||
: 'Your Consultation Has Been Approved',
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
$locale = $this->consultation->user->preferred_language ?? 'ar';
|
||||
|
||||
return new Content(
|
||||
markdown: 'emails.booking.approved.'.$locale,
|
||||
with: [
|
||||
'consultation' => $this->consultation,
|
||||
'user' => $this->consultation->user,
|
||||
'paymentInstructions' => $this->paymentInstructions,
|
||||
'formattedDate' => $this->getFormattedDate($locale),
|
||||
'formattedTime' => $this->getFormattedTime(),
|
||||
'duration' => $this->consultation->duration ?? 45,
|
||||
'consultationType' => $this->getConsultationTypeLabel($locale),
|
||||
'isPaid' => $this->consultation->consultation_type?->value === 'paid',
|
||||
'paymentAmount' => $this->consultation->payment_amount,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function attachments(): array
|
||||
{
|
||||
return [
|
||||
Attachment::fromData(fn () => $this->icsContent, 'consultation.ics')
|
||||
->withMime('text/calendar'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFormattedDate(string $locale): string
|
||||
{
|
||||
$date = $this->consultation->booking_date;
|
||||
|
||||
return $locale === 'ar'
|
||||
? $date->format('d/m/Y')
|
||||
: $date->format('m/d/Y');
|
||||
}
|
||||
|
||||
public function getFormattedTime(): string
|
||||
{
|
||||
$time = $this->consultation->booking_time;
|
||||
|
||||
return Carbon::parse($time)->format('h:i A');
|
||||
}
|
||||
|
||||
public function getConsultationTypeLabel(string $locale): string
|
||||
{
|
||||
$type = $this->consultation->consultation_type?->value ?? 'free';
|
||||
|
||||
if ($locale === 'ar') {
|
||||
return $type === 'paid' ? 'مدفوعة' : 'مجانية';
|
||||
}
|
||||
|
||||
return $type === 'paid' ? 'Paid' : 'Free';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Enums\ConsultationStatus;
|
||||
use App\Enums\ConsultationType;
|
||||
use App\Mail\BookingApprovedMail;
|
||||
use App\Models\Consultation;
|
||||
use App\Services\CalendarService;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class ConsultationObserver
|
||||
{
|
||||
public function __construct(
|
||||
protected CalendarService $calendarService
|
||||
) {}
|
||||
|
||||
public function updated(Consultation $consultation): void
|
||||
{
|
||||
if ($consultation->wasChanged('status') && $consultation->status === ConsultationStatus::Approved) {
|
||||
$this->sendApprovalEmail($consultation);
|
||||
}
|
||||
}
|
||||
|
||||
protected function sendApprovalEmail(Consultation $consultation): void
|
||||
{
|
||||
$consultation->loadMissing('user');
|
||||
|
||||
$icsContent = $this->calendarService->generateIcs($consultation);
|
||||
|
||||
$paymentInstructions = null;
|
||||
if ($consultation->consultation_type === ConsultationType::Paid) {
|
||||
$paymentInstructions = $this->getPaymentInstructions($consultation);
|
||||
}
|
||||
|
||||
Mail::to($consultation->user)
|
||||
->queue(new BookingApprovedMail($consultation, $icsContent, $paymentInstructions));
|
||||
}
|
||||
|
||||
protected function getPaymentInstructions(Consultation $consultation): string
|
||||
{
|
||||
$locale = $consultation->user->preferred_language ?? 'ar';
|
||||
$amount = number_format($consultation->payment_amount ?? 0, 2);
|
||||
|
||||
if ($locale === 'ar') {
|
||||
return "يرجى دفع مبلغ {$amount} شيكل قبل موعد الاستشارة.";
|
||||
}
|
||||
|
||||
return "Please pay {$amount} ILS before your consultation.";
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Listeners\LogFailedLoginAttempt;
|
||||
use App\Models\Consultation;
|
||||
use App\Observers\ConsultationObserver;
|
||||
use Illuminate\Auth\Events\Failed;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@@ -23,5 +25,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
Event::listen(Failed::class, LogFailedLoginAttempt::class);
|
||||
|
||||
Consultation::observe(ConsultationObserver::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user