Skip to content

Commit

Permalink
brushing up the code of TaskCommentController store
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-xz committed Aug 25, 2024
1 parent 8dd93fb commit e4b9ef1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/Http/Controllers/TaskCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ public function store(Request $request, Task $task)
$comment->save();
$comment->recipients()->sync($recipientsId);

// Sending notifications
// Saving notifications to database
$label = Label::where('name', 'new response')->first();
foreach ([$task->author?->id, $task->assignedTo?->id, ...$recipientsId] as $userId) {
$ifAlredyNotified = TaskNotification::where('task_id', $task->id)
->where('user_id', $userId)
->exists();
if (!$ifAlredyNotified && $request->user()?->id != $userId) {
$notification = new TaskNotification();
$label = Label::where('name', 'new response')->first();
$notification->task()->associate($task);
$notification->recipient()->associate($userId);
$notification->label()->associate($label);

$notification->save();
if ($ifAlredyNotified || $request->user()?->id == $userId) {
continue;
}

$notification = new TaskNotification();
$notification->task()->associate($task);
$notification->recipient()->associate($userId);
$notification->label()->associate($label);
$notification->comment()->associate($comment);

$notification->save();
}

flash(__('flash.commentStored'))->success();
Expand Down

0 comments on commit e4b9ef1

Please sign in to comment.