Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 0e0d9a2

Browse files
Merge pull request #241 from RoyalBoroughKingston/develop
v1.19.0
2 parents 4041752 + e99d935 commit 0e0d9a2

File tree

117 files changed

+3956
-1648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+3956
-1648
lines changed

app/BatchUpload/BatchUploader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ protected function processServices(array $services, EloquentCollection $organisa
281281
'referral_button_text' => !$isNone ? 'Make referral' : null,
282282
'referral_email' => $isInternal ? $serviceArray['Referral Email'] : null,
283283
'referral_url' => $isExternal ? $serviceArray['Referral URL'] : null,
284+
'last_modified_at' => now(),
284285
]);
285286

286287
$service->_id = $serviceArray['ID*'];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Ck\AutoDelete;
4+
5+
use App\Models\ServiceRefreshToken;
6+
use Illuminate\Console\Command;
7+
8+
class ServiceRefreshTokensCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'ck:auto-delete:service-refresh-tokens';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Deletes any service refresh tokens that are due for deletion';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return mixed
28+
*/
29+
public function handle()
30+
{
31+
$months = ServiceRefreshToken::AUTO_DELETE_MONTHS;
32+
33+
$this->line("Deleting service refresh tokens created {$months} month(s) ago...");
34+
$count = ServiceRefreshToken::dueForDeletion()->delete();
35+
$this->info("Deleted {$count} service refresh token(s).");
36+
}
37+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Ck\Notify;
4+
5+
use App\Emails\ServiceUpdatePrompt\NotifyGlobalAdminEmail;
6+
use App\Emails\ServiceUpdatePrompt\NotifyServiceAdminEmail;
7+
use App\Models\Notification;
8+
use App\Models\Role;
9+
use App\Models\Service;
10+
use Exception;
11+
use Illuminate\Console\Command;
12+
use Illuminate\Database\Eloquent\Collection;
13+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14+
15+
class StaleServicesCommand extends Command
16+
{
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'ck:notify:stale-services';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Sends notifications out for stale services';
30+
31+
/**
32+
* Execute the console command.
33+
*/
34+
public function handle(): void
35+
{
36+
$this->handleServices6To12MonthsStale();
37+
$this->handleServicesOver12MonthsStale();
38+
}
39+
40+
protected function handleServices6To12MonthsStale(): void
41+
{
42+
Service::query()
43+
->with([
44+
'users' => function (BelongsToMany $query): void {
45+
$query->where('user_roles.role_id', '=', Role::serviceAdmin()->id);
46+
},
47+
])
48+
->where('status', '=', Service::STATUS_ACTIVE)
49+
->whereBetween(
50+
'last_modified_at',
51+
[now()->subMonths(12), now()->subMonths(6)]
52+
)
53+
->chunk(200, function (Collection $services): void {
54+
// Loop through each service in the current chunk.
55+
$services->each(function (Service $service): void {
56+
$this->sendUpdatePromptEmailToServiceAdmins($service);
57+
});
58+
});
59+
}
60+
61+
protected function handleServicesOver12MonthsStale(): void
62+
{
63+
Service::query()
64+
->with([
65+
'users' => function (BelongsToMany $query): void {
66+
$query->where('user_roles.role_id', '=', Role::serviceAdmin()->id);
67+
},
68+
'organisation',
69+
])
70+
->where('status', '=', Service::STATUS_ACTIVE)
71+
->whereBetween(
72+
'last_modified_at',
73+
[now()->subMonths(13)->addDay(), now()->subMonths(12)]
74+
)
75+
->chunk(200, function (Collection $services): void {
76+
// Loop through each service in the current chunk.
77+
$services->each(function (Service $service): void {
78+
$this->sendUpdatePromptEmailToGlobalAdmin($service);
79+
});
80+
});
81+
}
82+
83+
/**
84+
* @param \App\Models\Service $service
85+
*/
86+
protected function sendUpdatePromptEmailToServiceAdmins(Service $service): void
87+
{
88+
// Create a refresh token for the service.
89+
$refreshToken = $service->serviceRefreshTokens()->create();
90+
91+
/** @var \App\Models\User $user */
92+
foreach ($service->users as $user) {
93+
try {
94+
$user->sendEmail(new NotifyServiceAdminEmail($user->email, [
95+
'SERVICE_NAME' => $service->name,
96+
'SERVICE_URL' => backend_uri("/services/{$service->id}"),
97+
'SERVICE_STILL_UP_TO_DATE_URL' => backend_uri("/services/{$service->id}/refresh?token={$refreshToken->id}"),
98+
]));
99+
100+
// Output a success message.
101+
$this->info("Emails successfully sent for service [{$service->id}] to user [{$user->id}]");
102+
} catch (Exception $exception) {
103+
// Log the exception.
104+
logger()->error($exception);
105+
106+
// Output an error message.
107+
$this->error("Email failed sending for service [{$service->id}] to user [{$user->id}]");
108+
}
109+
}
110+
}
111+
112+
/**
113+
* @param \App\Models\Service $service
114+
*/
115+
protected function sendUpdatePromptEmailToGlobalAdmin(Service $service): void
116+
{
117+
// Create a refresh token for the service.
118+
$refreshToken = $service->serviceRefreshTokens()->create();
119+
120+
try {
121+
Notification::sendEmail(
122+
new NotifyGlobalAdminEmail(config('ck.global_admin.email'), [
123+
'SERVICE_NAME' => $service->name,
124+
'SERVICE_URL' => backend_uri("/services/{$service->id}"),
125+
'SERVICE_ADMIN_NAMES' => $service->users->implode(', ', 'full_name'),
126+
'SERVICE_STILL_UP_TO_DATE_URL' => backend_uri("/services/{$service->id}/refresh?token={$refreshToken->id}"),
127+
])
128+
);
129+
130+
// Output a success message.
131+
$this->info("Emails successfully sent to global admin for service [{$service->id}]");
132+
} catch (Exception $exception) {
133+
// Log the exception.
134+
logger()->error($exception);
135+
136+
// Output an error message.
137+
$this->error("Email failed sending to global admin for service [{$service->id}]");
138+
}
139+
}
140+
}

app/Console/Commands/Ck/Redis/ClearCommand.php

Lines changed: 0 additions & 93 deletions
This file was deleted.

app/Console/Kernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Kernel extends ConsoleKernel
2424
*/
2525
protected function schedule(Schedule $schedule)
2626
{
27+
$schedule->command(Commands\Ck\Notify\StaleServicesCommand::class)
28+
->dailyAt('09:00');
29+
2730
$schedule->command(Commands\Ck\Notify\UnactionedReferralsCommand::class)
2831
->dailyAt('09:00');
2932

@@ -41,6 +44,9 @@ protected function schedule(Schedule $schedule)
4144

4245
$schedule->command(Commands\Ck\AutoDelete\ReferralsCommand::class)
4346
->daily();
47+
48+
$schedule->command(Commands\Ck\AutoDelete\ServiceRefreshTokensCommand::class)
49+
->daily();
4450
}
4551

4652
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Emails\ServiceUpdatePrompt;
4+
5+
use App\Emails\Email;
6+
7+
class NotifyGlobalAdminEmail extends Email
8+
{
9+
/**
10+
* @return string
11+
*/
12+
protected function getTemplateId(): string
13+
{
14+
return config('ck.notifications_template_ids.service_update_prompt.notify_global_admin.email');
15+
}
16+
17+
/**
18+
* @return string|null
19+
*/
20+
protected function getReference(): ?string
21+
{
22+
return null;
23+
}
24+
25+
/**
26+
* @return string|null
27+
*/
28+
protected function getReplyTo(): ?string
29+
{
30+
return null;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getContent(): string
37+
{
38+
return 'Pending to be sent. Content will be filled once sent.';
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Emails\ServiceUpdatePrompt;
4+
5+
use App\Emails\Email;
6+
7+
class NotifyServiceAdminEmail extends Email
8+
{
9+
/**
10+
* @return string
11+
*/
12+
protected function getTemplateId(): string
13+
{
14+
return config('ck.notifications_template_ids.service_update_prompt.notify_service_admin.email');
15+
}
16+
17+
/**
18+
* @return string|null
19+
*/
20+
protected function getReference(): ?string
21+
{
22+
return null;
23+
}
24+
25+
/**
26+
* @return string|null
27+
*/
28+
protected function getReplyTo(): ?string
29+
{
30+
return null;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getContent(): string
37+
{
38+
return 'Pending to be sent. Content will be filled once sent.';
39+
}
40+
}

0 commit comments

Comments
 (0)