Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to process old reminders #1529

Merged
merged 3 commits into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/Console/Commands/Reminder/ProcessOldReminders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Console\Commands\Reminder;

use Illuminate\Console\Command;
use App\Models\Contact\Reminder;

/**
* Because of an old bug, some reminders have never been sent and sit on the
* database, with very old `next_expected` date. This command
* - delete one time reminders
* - calculate the next expected date of the reminder based on its frequency.
*/
class ProcessOldReminders extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'process:old_reminders';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Schedule the next expected date for reminders which have never been sent';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$reminders = Reminder::where('next_expected_date', '<', now()->subDays(1))
->get();

foreach ($reminders as $reminder) {
// Skip the reminder if the contact has been deleted (and for some
// reasons, the reminder hasn't)
if (! $reminder->contact) {
$reminder->delete();
continue;
}

if ($reminder->frequency_type == 'one_time') {
$reminder->delete();
}

if ($reminder->frequency_type != 'one_time') {
$reminder->calculateNextExpectedDate('UTC');
$reminder->save();
}
}
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\SetupTest',
'App\Console\Commands\Update',
'App\Console\Commands\MigrateDatabaseCollation',
'App\Console\Commands\Reminder\ProcessOldReminders',
];

/**
Expand All @@ -41,6 +42,7 @@ protected function schedule(Schedule $schedule)
$schedule->command('send:reminders')->hourly();
$schedule->command('send:stay_in_touch')->hourly();
$schedule->command('monica:calculatestatistics')->daily();
$schedule->command('process:old_reminders')->daily();
$schedule->command('monica:ping')->daily();
}
}