From b2ca65ad4c47bdbb96e117545069814a8079305d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna-Sara=20S=C3=A9lea?= Date: Tue, 27 May 2025 08:38:49 +0200 Subject: [PATCH] Fix with mail and delete of reservation --- app/Console/Commands/SendReminderEmail.php | 46 ++++++++++++++ .../Resources/ReserveditemResource.php | 10 +++ app/Mail/ReservationDeletedUser.php | 58 +++++++++++++++++ app/Mail/ReturnDateReminder.php | 63 +++++++++++++++++++ ...5_add_notofied_to_reserved_items_table.php | 28 +++++++++ .../emails/user/reservationdeleted.blade.php | 12 ++++ .../emails/user/returndatereminder.blade.php | 13 ++++ routes/console.php | 2 + 8 files changed, 232 insertions(+) create mode 100644 app/Console/Commands/SendReminderEmail.php create mode 100644 app/Mail/ReservationDeletedUser.php create mode 100644 app/Mail/ReturnDateReminder.php create mode 100644 database/migrations/2025_05_27_045625_add_notofied_to_reserved_items_table.php create mode 100644 resources/views/emails/user/reservationdeleted.blade.php create mode 100644 resources/views/emails/user/returndatereminder.blade.php diff --git a/app/Console/Commands/SendReminderEmail.php b/app/Console/Commands/SendReminderEmail.php new file mode 100644 index 0000000..d46a6d0 --- /dev/null +++ b/app/Console/Commands/SendReminderEmail.php @@ -0,0 +1,46 @@ +add(5, 'days')->toDateTimeString()) + ->where('return_date', '>', Carbon::now()->toDateTimeString()) + ->where('notified', 0) + ->get(); + + if ($reservations) { + foreach ($reservations as $reservation) { + Mail::to($reservation['email']) + ->send(new ReturnDateReminder($reservation)); + $reservation['notified'] = 1; + $reservation->save(); + } + } + } +} diff --git a/app/Filament/Resources/ReserveditemResource.php b/app/Filament/Resources/ReserveditemResource.php index 79fa1e9..c54f819 100644 --- a/app/Filament/Resources/ReserveditemResource.php +++ b/app/Filament/Resources/ReserveditemResource.php @@ -25,6 +25,8 @@ use Filament\Tables\Columns\IconColumn; use Filament\Tables\Actions\Action; use Filament\Tables\Columns\Layout\Stack; use Filament\Support\Enums\FontWeight; +use Illuminate\Support\Facades\Mail; +use App\Mail\ReservationDeletedUser; class ReserveditemResource extends Resource @@ -124,6 +126,14 @@ class ReserveditemResource extends Resource ->button() ->icon('heroicon-m-pencil-square') ->iconPosition(IconPosition::After), + Tables\Actions\DeleteAction::make() + ->action(function (array $data, Reserveditem $record): void { + $record->delete(); + Item::where('id', $record->item_id)->update(['reserved' => false]); + Mail::to($record['email']) + ->send(new ReservationDeletedUser($record)); + }) + ]) ->bulkActions([ // Tables\Actions\BulkActionGroup::make([ diff --git a/app/Mail/ReservationDeletedUser.php b/app/Mail/ReservationDeletedUser.php new file mode 100644 index 0000000..fdf9236 --- /dev/null +++ b/app/Mail/ReservationDeletedUser.php @@ -0,0 +1,58 @@ + $this->reservation->username, + 'reservationDesc' => Item::where('id', $this->reservation->item_id)->pluck('desc')->first(), + 'reservationEmail' => $this->reservation->email, + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Mail/ReturnDateReminder.php b/app/Mail/ReturnDateReminder.php new file mode 100644 index 0000000..639154f --- /dev/null +++ b/app/Mail/ReturnDateReminder.php @@ -0,0 +1,63 @@ +reservation->return_date); + return new Content( + view: 'emails.user.returndatereminder', + with: [ + 'reservationName' => $this->reservation->username, + 'reservationDesc' => Item::where('id', $this->reservation->item_id)->pluck('desc')->first(), + 'reservationEmail' => $this->reservation->email, + 'reservationReturnDate' => $date->format('Y-m-d'), + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/database/migrations/2025_05_27_045625_add_notofied_to_reserved_items_table.php b/database/migrations/2025_05_27_045625_add_notofied_to_reserved_items_table.php new file mode 100644 index 0000000..0a99ae6 --- /dev/null +++ b/database/migrations/2025_05_27_045625_add_notofied_to_reserved_items_table.php @@ -0,0 +1,28 @@ +boolean('notified')->default(0)->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('reserveditems', function (Blueprint $table) { + $table->dropColumn('notified'); + }); + } +}; diff --git a/resources/views/emails/user/reservationdeleted.blade.php b/resources/views/emails/user/reservationdeleted.blade.php new file mode 100644 index 0000000..9c203d5 --- /dev/null +++ b/resources/views/emails/user/reservationdeleted.blade.php @@ -0,0 +1,12 @@ + + + + Reservation borttagen + + +

Reservation borttagen

+

Din reserveration för {{ $reservationDesc }} har blivit borttagen

+

Med vänliga hälsningar

+

vBytes

+ + \ No newline at end of file diff --git a/resources/views/emails/user/returndatereminder.blade.php b/resources/views/emails/user/returndatereminder.blade.php new file mode 100644 index 0000000..ec9cf19 --- /dev/null +++ b/resources/views/emails/user/returndatereminder.blade.php @@ -0,0 +1,13 @@ + + + + Påminnelse om återlämning + + +

Påminnelse om återlämning

+

Det är snart dags att lämna tillbaka {{ $reservationDesc }}

+

Du ska återlämna varan {{ $reservationReturnDate }}

+

Med vänliga hälsningar

+

vBytes

+ + \ No newline at end of file diff --git a/routes/console.php b/routes/console.php index eff2ed2..98203a5 100644 --- a/routes/console.php +++ b/routes/console.php @@ -6,3 +6,5 @@ use Illuminate\Support\Facades\Artisan; Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote')->hourly(); + +Schedule::command('send-reminder-email:cron')->daily();