1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Constants\TranslationCode; |
6
|
|
|
use App\Models\UserTask; |
7
|
|
|
use App\Services\LogService; |
8
|
|
|
use App\Services\NotificationService; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use Illuminate\Console\Command; |
11
|
|
|
use Illuminate\Support\Facades\DB; |
12
|
|
|
use Illuminate\Support\Facades\Log; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TaskNotificationsCommand |
17
|
|
|
* |
18
|
|
|
* Send notifications to assigned user when an uncompleted task has deadline today. |
19
|
|
|
* Send notifications to users added the task when the task is uncompleted and deadline passed by a day. |
20
|
|
|
* Should be running once a day. |
21
|
|
|
* |
22
|
|
|
* @package App\Console\Commands |
23
|
|
|
*/ |
24
|
|
|
class TaskNotificationsCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** @var string */ |
27
|
|
|
protected $signature = 'send:taskNotifications'; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $description = 'Send notifications when a task is expiring or has expired.'; |
31
|
|
|
|
32
|
|
|
/** @var NotificationService */ |
33
|
|
|
protected $notificationService; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* TaskNotificationsCommand constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
|
42
|
|
|
$this->notificationService = new NotificationService(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Command handle |
47
|
|
|
*/ |
48
|
|
|
public function handle() |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Command [send:taskNotifications] started.'); |
52
|
|
|
|
53
|
|
|
DB::beginTransaction(); |
54
|
|
|
|
55
|
|
|
$this->checkExpiringTasks(); |
56
|
|
|
|
57
|
|
|
$this->checkExpiredTasks(); |
58
|
|
|
|
59
|
|
|
DB::commit(); |
60
|
|
|
|
61
|
|
|
$this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Command [send:taskNotifications] ended.'); |
62
|
|
|
} catch (Throwable $t) { |
63
|
|
|
Log::error(LogService::getThrowableTraceAsString($t)); |
64
|
|
|
|
65
|
|
|
$this->error($t->getMessage()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Identify uncompleted tasks that have deadline today and send notifications. |
71
|
|
|
*/ |
72
|
|
|
private function checkExpiringTasks() |
73
|
|
|
{ |
74
|
|
|
$expiringUserTasks = UserTask::where('status', UserTask::STATUS_ASSIGNED) |
75
|
|
|
->where('deadline', Carbon::now()->format('Y-m-d')) |
76
|
|
|
->get(); |
77
|
|
|
|
78
|
|
|
$this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Found ' . $expiringUserTasks->count() . ' expiring tasks.'); |
79
|
|
|
|
80
|
|
|
foreach ($expiringUserTasks as $expiringUserTask) { |
81
|
|
|
$userNotification = $this->notificationService->addNotification( |
82
|
|
|
$expiringUserTask->assigned_user_id, |
83
|
|
|
TranslationCode::NOTIFICATION_TASK_EXPIRING, |
84
|
|
|
'userTask', |
85
|
|
|
$expiringUserTask->id |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->notificationService->sendNotification($userNotification); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Identify uncompleted tasks that have deadline passed by a day and send notifications. |
94
|
|
|
*/ |
95
|
|
|
private function checkExpiredTasks() |
96
|
|
|
{ |
97
|
|
|
$expiredUserTasks = UserTask::where('status', UserTask::STATUS_ASSIGNED) |
98
|
|
|
->where('deadline', Carbon::now()->subDay()->format('Y-m-d')) |
99
|
|
|
->get(); |
100
|
|
|
|
101
|
|
|
$this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Found ' . $expiredUserTasks->count() . ' expired tasks.'); |
102
|
|
|
|
103
|
|
|
foreach ($expiredUserTasks as $expiredUserTask) { |
104
|
|
|
$userNotification = $this->notificationService->addNotification( |
105
|
|
|
$expiredUserTask->assigned_user_id, |
106
|
|
|
TranslationCode::NOTIFICATION_TASK_EXPIRED, |
107
|
|
|
'userTask', |
108
|
|
|
$expiredUserTask->id |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->notificationService->sendNotification($userNotification); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|