-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNotifyInactiveProjects.php
More file actions
133 lines (113 loc) · 4.93 KB
/
Copy pathNotifyInactiveProjects.php
File metadata and controls
133 lines (113 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace App\Console\Commands;
use App\Models\Project;
use App\Models\User;
use App\Notifications\ProjectInactivityReminderNotification;
use App\Notifications\ProjectInactivityReportToAdmins;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
class NotifyInactiveProjects extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nmrxiv:notify-inactive-projects {--months= : Number of months of inactivity (defaults to config inactivity.grace_months)} {--list : Only list inactive projects without notifying} {--report-admins : Email admins a report of inactive projects}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Notify project owners if the project has had no updates for N months (default 6).';
/**
* Execute the console command.
*/
public function handle(): int
{
$months = (int) ($this->option('months') ?? config('inactivity.grace_months', 6));
$threshold = Carbon::now()->subMonths($months);
return DB::transaction(function () use ($threshold) {
// Identify inactive projects and mark them inactive
$projects = Project::with('owner', 'users')
->where('is_public', false)
->where('is_deleted', false)
->where('is_archived', false)
->where('updated_at', '<', $threshold)
->get();
if ($this->option('list')) {
$this->table(['ID', 'Name', 'Owner Email', 'Updated At'], $projects->map(function ($p) {
return [$p->id, $p->name, optional($p->owner)->email, (string) $p->updated_at];
})->toArray());
$this->info('Total inactive projects: '.count($projects));
return self::SUCCESS;
}
// Mark these projects as inactive in DB (idempotent) without touching updated_at
if ($projects->count() > 0) {
$ids = $projects->pluck('id');
Project::withoutTimestamps(function () use ($ids) {
Project::whereIn('id', $ids)->update(['active' => false]);
});
}
// Aggregate by recipient so each user gets a single digest listing all of their inactive projects
$recipientProjects = [];
foreach ($projects as $project) {
foreach ($this->prepareSendList($project) as $recipient) {
$recipientProjects[$recipient->id]['user'] = $recipient;
$recipientProjects[$recipient->id]['projects'][] = $project;
}
}
// Send one email per recipient with their list of inactive projects
$sentCount = 0;
foreach ($recipientProjects as $entry) {
/** @var \App\Models\User $user */
$user = $entry['user'];
$list = collect($entry['projects'])->map(function ($p) {
return [
'id' => $p->id,
'name' => $p->name,
'updated_at' => (string) $p->updated_at,
'url' => url(config('app.url').'/dashboard/projects/'.$p->id),
];
})->values()->all();
Notification::send($user, new ProjectInactivityReminderNotification($list));
$sentCount++;
}
$this->info('Inactive project digests sent: '.$sentCount.' (covering '.count($projects).' projects)');
if ($this->option('report-admins') && $projects->count() > 0) {
$payload = $projects->map(function ($p) {
return [
'id' => $p->id,
'name' => $p->name,
'owner' => optional($p->owner)->email ?? 'N/A',
'updated_at' => (string) $p->updated_at,
];
})->values()->all();
Notification::send(User::role(['super-admin'])->get(), new ProjectInactivityReportToAdmins($payload));
$this->info('Admin report sent to super-admins.');
}
return self::SUCCESS;
});
}
/**
* Prepare recipients list (owner and creators).
*/
protected function prepareSendList(Project $project): array
{
$sendTo = [];
$add = function ($user) use (&$sendTo) {
if ($user && isset($user->id)) {
$sendTo[$user->id] = $user;
}
};
foreach ($project->allUsers() as $member) {
if ($member->projectMembership->role == 'creator' || $member->projectMembership->role == 'owner') {
$add($member);
}
}
$add($project->owner);
return array_values($sendTo);
}
}