Skip to content

Commit

Permalink
hotfix/stale-service-emails-not-sending (#243)
Browse files Browse the repository at this point in the history
* Emails only sent on 15th of month now

* Removed global admins from stale service emails
  • Loading branch information
matthew-inamdar authored Jun 17, 2019
1 parent 0226ebb commit ec8693b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
8 changes: 7 additions & 1 deletion app/Console/Commands/Ck/Notify/StaleServicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use App\Models\Service;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\DB;

class StaleServicesCommand extends Command
{
Expand Down Expand Up @@ -42,7 +44,11 @@ protected function handleServices6To12MonthsStale(): void
Service::query()
->with([
'users' => function (BelongsToMany $query): void {
$query->where('user_roles.role_id', '=', Role::serviceAdmin()->id);
$query
->where('user_roles.role_id', '=', Role::serviceAdmin()->id)
->whereDoesntHave('userRoles', function (Builder $query): void {
$query->where('user_roles.role_id', '=', Role::globalAdmin()->id);
});
},
])
->where('status', '=', Service::STATUS_ACTIVE)
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
$schedule->command(Commands\Ck\Notify\StaleServicesCommand::class)
->dailyAt('09:00');
->monthlyOn(15, '09:00');

$schedule->command(Commands\Ck\Notify\UnactionedReferralsCommand::class)
->dailyAt('09:00');
Expand Down
42 changes: 32 additions & 10 deletions tests/Unit/Console/Commands/Ck/Notify/StaleServicesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@

class StaleServicesCommandTest extends TestCase
{
/*
* 6 to 12 months.
*/
public function test_6_to_12_months_emails_not_sent_after_5_months()
{
Queue::fake();

factory(Service::class)->create([
$service = factory(Service::class)->create([
'last_modified_at' => now()->subMonths(5),
]);

factory(User::class)->create()->makeSuperAdmin();
factory(User::class)->create()->makeServiceAdmin($service);

Artisan::call(StaleServicesCommand::class);

Expand All @@ -32,11 +35,11 @@ public function test_6_to_12_months_emails_not_sent_after_13_months()
{
Queue::fake();

factory(Service::class)->create([
$service = factory(Service::class)->create([
'last_modified_at' => now()->subMonths(13),
]);

factory(User::class)->create()->makeSuperAdmin();
factory(User::class)->create()->makeServiceAdmin($service);

Artisan::call(StaleServicesCommand::class);

Expand All @@ -47,11 +50,11 @@ public function test_6_to_12_months_emails_sent_after_6_months()
{
Queue::fake();

factory(Service::class)->create([
$service = factory(Service::class)->create([
'last_modified_at' => now()->subMonths(6),
]);

factory(User::class)->create()->makeSuperAdmin();
factory(User::class)->create()->makeServiceAdmin($service);

Artisan::call(StaleServicesCommand::class);

Expand All @@ -68,11 +71,11 @@ public function test_6_to_12_months_emails_sent_after_12_months()
{
Queue::fake();

factory(Service::class)->create([
$service = factory(Service::class)->create([
'last_modified_at' => now()->subMonths(12),
]);

factory(User::class)->create()->makeSuperAdmin();
factory(User::class)->create()->makeServiceAdmin($service);

Artisan::call(StaleServicesCommand::class);

Expand All @@ -89,11 +92,11 @@ public function test_6_to_12_months_emails_sent_after_9_months()
{
Queue::fake();

factory(Service::class)->create([
$service = factory(Service::class)->create([
'last_modified_at' => now()->subMonths(9),
]);

factory(User::class)->create()->makeSuperAdmin();
factory(User::class)->create()->makeServiceAdmin($service);

Artisan::call(StaleServicesCommand::class);

Expand Down Expand Up @@ -121,6 +124,25 @@ public function test_6_to_12_months_emails_not_sent_to_service_workers()
Queue::assertNotPushed(NotifyServiceAdminEmail::class);
}

public function test_6_to_12_months_emails_not_sent_to_global_admins()
{
Queue::fake();

factory(Service::class)->create([
'last_modified_at' => now()->subMonths(9),
]);

factory(User::class)->create()->makeGlobalAdmin();

Artisan::call(StaleServicesCommand::class);

Queue::assertNotPushed(NotifyServiceAdminEmail::class);
}

/*
* After 12 months.
*/

public function test_after_12_months_emails_not_sent_after_11_months()
{
Queue::fake();
Expand Down

0 comments on commit ec8693b

Please sign in to comment.