Commit b6aa44a2 authored by Tim Visée's avatar Tim Visée
Browse files

Merge branch '358-send-first-balance-update-after-a-week' into 'master'

Resolve "Send first balance update after a week"

Closes #358

See merge request !191
parents 171286d4 587dfff6
Pipeline #192142721 passed with stage
in 5 minutes and 7 seconds
......@@ -30,6 +30,11 @@ class SendBalanceUpdates implements ShouldQueue {
*/
const UPDATE_INTERVAL = 60 * 60 * 24 * 30;
/**
* Time in seconds for the first updat eemail to be send to each user.
*/
const UPDATE_FIRST_DELAY = 60 * 60 * 24 * 7;
/**
* Allowed update interval play in seconds.
*
......@@ -80,17 +85,24 @@ class SendBalanceUpdates implements ShouldQueue {
// Send an update to each listed user
$users->each(function($user) {
DB::transaction(function() use($user) {
// Update email history time
$history = EmailHistory::updateOrCreate([
'user_id' => $user->id,
'type' => EmailHistory::TYPE_BALANCE_UPDATE,
], [
'last_at' => now(),
]);
// Find the email history entry
$email_history = EmailHistory::user($user)
->type(EmailHistory::TYPE_BALANCE_UPDATE)
->first();
// Do not send the first time, if create/update time are the same
if($history->created_at == $history->updated_at)
// Create new entry if non existant
if($email_history == null) {
$email_history = new EmailHistory();
$email_history->user_id = $user->id;
$email_history->type = EmailHistory::TYPE_BALANCE_UPDATE;
$email_history->last_at = now()->subSeconds(Self::UPDATE_INTERVAL)->addSeconds(Self::UPDATE_FIRST_DELAY);
$email_history->save();
return;
}
// Update last time in existing entity
$email_history->last_at = now();
$email_history->save();
// Schedule a job to send the balance update for the user
SendBalanceUpdate::dispatch($user->id);
......
......@@ -2,6 +2,7 @@
namespace App\Models;
use App\Models\Users;
use App\Utils\EmailRecipient;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
......@@ -38,6 +39,19 @@ class EmailHistory extends Model {
*/
const TYPE_BALANCE_UPDATE = 1;
/**
* A scope for selecting a specific user.
*
* @param User|int $user_id The user.
*/
public function scopeUser($query, $user_id) {
if($user_id instanceof User)
$user_id = $user_id->id;
if($user_id == null)
throw new \Exception("User cannot be null");
return $query->where('user_id', $user_id);
}
/**
* A scope for selecting a specific email type.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment