From c3abef62a7b80e5fda132b178fb783a003b2067d Mon Sep 17 00:00:00 2001 From: Michal Feher Date: Wed, 20 Aug 2025 12:21:25 +0200 Subject: [PATCH] no message --- .../SendEditedSubTaskNotification.php | 2 + .../SendNewSubTaskEmailNotification.php | 65 +++++++++++++++---- ...add_notification_sent_flag_to_subtasks.php | 31 +++++++++ 3 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 be/database/migrations/2025_08_19_114307_add_notification_sent_flag_to_subtasks.php diff --git a/be/app/Listeners/SendEditedSubTaskNotification.php b/be/app/Listeners/SendEditedSubTaskNotification.php index 1cdbe57..05715a3 100644 --- a/be/app/Listeners/SendEditedSubTaskNotification.php +++ b/be/app/Listeners/SendEditedSubTaskNotification.php @@ -46,11 +46,13 @@ class SendEditedSubTaskNotification implements ShouldQueue unset($editedSubTaskArray['updated_at']); unset($editedSubTaskArray['timesheets']); unset($editedSubTaskArray['timeblocks']); + unset($editedSubTaskArray['notification_sent']); unset($originalSubTaskArray['created_at']); unset($originalSubTaskArray['updated_at']); unset($originalSubTaskArray['timesheets']); unset($originalSubTaskArray['timeblocks']); + unset($originalSubTaskArray['notification_sent']); // get the difference between $subtask and $originalSubtask $modelDiff = array_diff_assoc($editedSubTaskArray, $originalSubTaskArray); diff --git a/be/app/Listeners/SendNewSubTaskEmailNotification.php b/be/app/Listeners/SendNewSubTaskEmailNotification.php index 5de7549..f2fc44c 100644 --- a/be/app/Listeners/SendNewSubTaskEmailNotification.php +++ b/be/app/Listeners/SendNewSubTaskEmailNotification.php @@ -31,18 +31,57 @@ class SendNewSubTaskEmailNotification implements ShouldQueue */ public function handle(SubTaskCreated $event) { - // ked je v subtasku viacej userov, tak poslem 1 email notifikaciu so vsetkymi usermi, ktori su v master tasku -// if (count($event->subtask->mastertask->subtasks) > 1) { -// $emails = $event->subtask->mastertask->all_users_in_master_task->pluck('email')->unique()->toArray(); -// -// if (empty($emails)) { -// return; -// } -// -// Mail::to($emails)->send(new NewTask($event->subtask)); -// } else { -// Mail::to($event->subtask->user->email)->send(new NewTask($event->subtask)); -// } - Mail::to($event->subtask->user->email)->send(new NewTask($event->subtask)); + // flag na urovni subtasku - notification_sent + // ak existuje este nejaky subtask v ramci master tasku, co ma prazdnu notification_sent, tak mu nastavim 0 a skippujem posielanie emailu + // az ked neexistuje uz iny subtask, co ma flag prazdny, tak poslem email a nastavim notification_sent na 1 + // v pripade, ze sa do existujuceho master tasku pridaju nove subtasky, tak opakujem to iste. akurat pri posielani pri notification_sent nastavim poslem iba na tych userov, co boli medzi poslednymi subtaskami, ktore mali notification_sent prazdny + + if (count($event->subtask->mastertask->subtasks) > 1) { + $masterTaskSubtasks = $event->subtask->mastertask->subtasks; + + // get last subtask + $lastSubtask = $masterTaskSubtasks->last(); + + if ($lastSubtask->id > $event->subtask->id) { + // skip sending email if this is not the last subtask + return; + } + + $lastSubtaskWithNotificationSent = $masterTaskSubtasks->where('notification_sent', 1)->last(); + + if ( ! $lastSubtaskWithNotificationSent) { + // if there is no subtask with notification_sent = 1, set it to the first subtask + $firstSubtaskWithNotificationNotSent = $masterTaskSubtasks->first(); + } else { + $firstSubtaskWithNotificationNotSent = $masterTaskSubtasks->where('notification_sent', 0)->where('id', + '>', $lastSubtaskWithNotificationSent->id)->first(); + } + + // get all the subtasks between the last subtask with notification_sent = 1 and the current subtask + $subtasksToNotify = $masterTaskSubtasks->where('notification_sent', 0)->where('id', '>=', + $firstSubtaskWithNotificationNotSent->id); + + // if there are no subtasks to notify, skip sending email + if ($subtasksToNotify->isEmpty()) { + return; + } + + // get unique emails from the subtasks to notify + $emails = $subtasksToNotify->pluck('user.email')->unique()->toArray(); + + if (empty($emails)) { + return; + } + + Mail::to($emails)->send(new NewTask($event->subtask)); + + // set notification_sent to 1 for all the subtasks that were notified + $subtasksToNotify->each(function ($subtask) { + $subtask->notification_sent = 1; + $subtask->save(); + }); + } else { + Mail::to($event->subtask->user->email)->send(new NewTask($event->subtask)); + } } } diff --git a/be/database/migrations/2025_08_19_114307_add_notification_sent_flag_to_subtasks.php b/be/database/migrations/2025_08_19_114307_add_notification_sent_flag_to_subtasks.php new file mode 100644 index 0000000..82fd0e7 --- /dev/null +++ b/be/database/migrations/2025_08_19_114307_add_notification_sent_flag_to_subtasks.php @@ -0,0 +1,31 @@ +tinyInteger('notification_sent')->after('is_finished')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('subtasks', function (Blueprint $table) { + // + }); + } +};