diff --git a/app/Http/Controllers/EventsController.php b/app/Http/Controllers/EventsController.php index e337b5a..2422f20 100644 --- a/app/Http/Controllers/EventsController.php +++ b/app/Http/Controllers/EventsController.php @@ -2,7 +2,9 @@ namespace App\Http\Controllers; +use App\Mail\EventParticipationCancelledMail; use App\Models\Event; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Notification; use App\Models\Task; use App\Notifications\EventDeleted; @@ -94,6 +96,18 @@ class EventsController extends Controller if ($participants->isNotEmpty()) { Notification::send($participants, new EventDeleted($event)); + + foreach ($participants as $user) { + if ($user->email_notifications) { + Mail::to($user->email)->send(new EventParticipationCancelledMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'eventName' => $event->name, + 'start' => $event->start, + 'end' => $event->end, + ])); + } + } } } diff --git a/app/Http/Controllers/TasksController.php b/app/Http/Controllers/TasksController.php index f1ac7fd..8c37e6a 100644 --- a/app/Http/Controllers/TasksController.php +++ b/app/Http/Controllers/TasksController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; +use App\Mail\TaskDateUpdateMail; +use App\Mail\TaskParticipationCancelledMail; +use App\Mail\VolunteerAssignToTaskMail; +use App\Mail\VolunteerUnassignToTaskMail; use App\Notifications\TaskDateUpdated; use App\Notifications\TaskDeleted; use App\Notifications\VolunteerAssignedToTask; use App\Notifications\VolunteerUnassignedFromTask; use App\Models\Event; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Notification; use App\Models\Task; use App\Models\User; @@ -91,8 +96,19 @@ class TasksController extends Controller $task->users()->attach($id); if (Carbon::now()->lessThanOrEqualTo($task->end)) { + $event = Event::findOrFail($task->events_id); $user->notify(new VolunteerAssignedToTask($task, $event)); - } + + if ($user->email_notifications) { + Mail::to($user->email)->send(new VolunteerAssignToTaskMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'taskName' => $task->name, + 'start' => $task->start, + 'end' => $task->end, + 'eventName' => $event->name, + ])); + } } return response()->json(['message' => 'Task assigned'], 200); @@ -128,6 +144,19 @@ class TasksController extends Controller if (Carbon::now()->lessThanOrEqualTo($task->end) && $task->users->isNotEmpty()) { Notification::send($task->users, new TaskDeleted($task, $event)); + + foreach ($task->users as $user) { + if ($user->email_notifications) { + Mail::to($user->email)->send(new TaskParticipationCancelledMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'taskName' => $task->name, + 'start' => $event->start, + 'end' => $task->end, + 'eventName' => $event->name, + ])); + } + } } $task->delete(); @@ -162,6 +191,21 @@ class TasksController extends Controller newStart: FormatDate::formatDate($request['start']), newEnd: FormatDate::formatDate($request['end']), )); + + foreach ($task->users as $user) { + if ($user->email_notifications) { + Mail::to($user->email)->send(new TaskDateUpdateMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'taskName' => $task->name, + 'oldStart' => FormatDate::formatDate($task->start), + 'oldEnd' => FormatDate::formatDate($task->end), + 'newStart' => $request['start'], + 'newEnd' => $request['end'], + 'eventName' => $event->name, + ])); + } + } } $task->update([ @@ -219,6 +263,17 @@ class TasksController extends Controller if (Carbon::now()->lessThanOrEqualTo($task->end)) { $event = Event::findOrFail($task->events_id); $user->notify(new VolunteerUnassignedFromTask($task, $event)); + + if ($user->email_notifications) { + Mail::to($user->email)->send(new VolunteerUnassignToTaskMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'taskName' => $task->name, + 'start' => $task->start, + 'end' => $task->end, + 'eventName' => $event->name, + ])); + } } return response()->json(['message' => 'User unassigned successfully'], 200); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 42e39c8..eb9a46a 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Mail\RegisterMail; +use App\Mail\UpdateRoleMail; use App\Notifications\VolunteerRoleUpdated; use App\Mail\CreateUserByAdminMail; use App\Mail\DeactivateAccountMail; @@ -56,6 +58,17 @@ class UserController extends Controller $admins = User::whereIn('role', [1, 2])->get(); Notification::sendNow($admins, new UserRegistered($user)); + foreach ($admins as $admin) { + if ($admin->email_notifications) { + Mail::to($admin->email)->send(new RegisterMail([ + 'adminName' => $admin->name, + 'adminLastname' => $admin->lastname, + 'userName' => $user->name, + 'userLastname' => $user->lastname, + ])); + } + } + return response()->json(['message' => 'User created successfully']); } @@ -244,6 +257,14 @@ class UserController extends Controller $user->notify(new VolunteerRoleUpdated($role)); + if ($user->email_notifications) { + Mail::to($user->email)->send(new UpdateRoleMail([ + 'name' => $user->name, + 'lastname' => $user->lastname, + 'role' => $role, + ])); + } + return response()->json(['message' => 'User role added successfully']); } catch(\Exception $e) { Log::info($e->getMessage()); diff --git a/app/Notifications/EventDeleted.php b/app/Notifications/EventDeleted.php index 587ae28..84e3147 100644 --- a/app/Notifications/EventDeleted.php +++ b/app/Notifications/EventDeleted.php @@ -25,10 +25,6 @@ class EventDeleted extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -44,15 +40,4 @@ class EventDeleted extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new EventParticipationCancelledMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'eventName' => $this->event->name, - 'start' => $this->event->start, - 'end' => $this->event->end, - ])); - } } diff --git a/app/Notifications/TaskDateUpdated.php b/app/Notifications/TaskDateUpdated.php index 4e52690..54ab043 100644 --- a/app/Notifications/TaskDateUpdated.php +++ b/app/Notifications/TaskDateUpdated.php @@ -33,10 +33,6 @@ class TaskDateUpdated extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -57,18 +53,4 @@ class TaskDateUpdated extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new TaskDateUpdateMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'taskName' => $this->task->name, - 'oldStart' => $this->oldStart, - 'oldEnd' => $this->oldEnd, - 'newStart' => $this->newStart, - 'newEnd' => $this->newEnd, - 'eventName' => $this->event->name, - ])); - } } diff --git a/app/Notifications/TaskDeleted.php b/app/Notifications/TaskDeleted.php index 775d88e..146e8e2 100644 --- a/app/Notifications/TaskDeleted.php +++ b/app/Notifications/TaskDeleted.php @@ -29,10 +29,6 @@ class TaskDeleted extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -49,16 +45,4 @@ class TaskDeleted extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new TaskParticipationCancelledMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'taskName' => $this->task->name, - 'start' => $this->event->start, - 'end' => $this->task->end, - 'eventName' => $this->event->name, - ])); - } } diff --git a/app/Notifications/UserRegistered.php b/app/Notifications/UserRegistered.php index f26d558..88f801f 100644 --- a/app/Notifications/UserRegistered.php +++ b/app/Notifications/UserRegistered.php @@ -26,10 +26,6 @@ class UserRegistered extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -45,16 +41,4 @@ class UserRegistered extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): MailMessage - { - Mail::to($notifiable->email)->send(new RegisterMail([ - 'adminName' => $notifiable->name, - 'adminLastname'=> $notifiable->lastname, - 'userName' => $this->user->name, - 'userLastname' => $this->user->lastname, - ])); - - return (new MailMessage); - } } diff --git a/app/Notifications/VolunteerAssignedToTask.php b/app/Notifications/VolunteerAssignedToTask.php index 42f94b9..0fb9c6b 100644 --- a/app/Notifications/VolunteerAssignedToTask.php +++ b/app/Notifications/VolunteerAssignedToTask.php @@ -29,10 +29,6 @@ class VolunteerAssignedToTask extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -49,16 +45,4 @@ class VolunteerAssignedToTask extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new VolunteerAssignToTaskMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'taskName' => $this->task->name, - 'start' => $this->task->start, - 'end' => $this->task->end, - 'eventName' => $this->event->name, - ])); - } } diff --git a/app/Notifications/VolunteerRoleUpdated.php b/app/Notifications/VolunteerRoleUpdated.php index 060c181..3b80d7d 100644 --- a/app/Notifications/VolunteerRoleUpdated.php +++ b/app/Notifications/VolunteerRoleUpdated.php @@ -24,10 +24,6 @@ class VolunteerRoleUpdated extends Notification implements ShouldBroadcast $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -43,13 +39,4 @@ class VolunteerRoleUpdated extends Notification implements ShouldBroadcast { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new UpdateRoleMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'role' => $this->role, - ])); - } } diff --git a/app/Notifications/VolunteerUnassignedFromTask.php b/app/Notifications/VolunteerUnassignedFromTask.php index 71d0220..6ef215e 100644 --- a/app/Notifications/VolunteerUnassignedFromTask.php +++ b/app/Notifications/VolunteerUnassignedFromTask.php @@ -29,10 +29,6 @@ class VolunteerUnassignedFromTask extends Notification implements ShouldBroadcas $channels[] = 'broadcast'; } - if ($notifiable->email_notifications) { - $channels[] = 'mail'; - } - return $channels; } @@ -49,16 +45,4 @@ class VolunteerUnassignedFromTask extends Notification implements ShouldBroadcas { return new BroadcastMessage($this->toDatabase($notifiable)); } - - public function toMail($notifiable): void - { - Mail::to($notifiable->email)->send(new VolunteerUnassignToTaskMail([ - 'name' => $notifiable->name, - 'lastname' => $notifiable->lastname, - 'taskName' => $this->task->name, - 'start' => $this->task->start, - 'end' => $this->task->end, - 'eventName' => $this->event->name, - ])); - } }