Changeset View
Changeset View
Standalone View
Standalone View
src/app/Jobs/WalletCheck.php
Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | class WalletCheck implements ShouldQueue | ||||
protected function initialReminder() | protected function initialReminder() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_initial')) { | if ($this->wallet->getSetting('balance_warning_initial')) { | ||||
return; | return; | ||||
} | } | ||||
// TODO: Should we check if the account is already suspended? | // TODO: Should we check if the account is already suspended? | ||||
$this->sendMail(\App\Mail\NegativeBalance::class); | $label = "Notification sent for"; | ||||
$this->sendMail(\App\Mail\NegativeBalance::class, false, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_initial', $now); | $this->wallet->setSetting('balance_warning_initial', $now); | ||||
\Log::info("[WalletCheck] Notification sent for {$this->wallet->owner->email}"); | |||||
} | } | ||||
/** | /** | ||||
* Send the second reminder | * Send the second reminder | ||||
*/ | */ | ||||
protected function secondReminder() | protected function secondReminder() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_reminder')) { | if ($this->wallet->getSetting('balance_warning_reminder')) { | ||||
return; | return; | ||||
} | } | ||||
// TODO: Should we check if the account is already suspended? | // TODO: Should we check if the account is already suspended? | ||||
$this->sendMail(\App\Mail\NegativeBalanceReminder::class); | $label = "Reminder sent for"; | ||||
$this->sendMail(\App\Mail\NegativeBalanceReminder::class, false, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_reminder', $now); | $this->wallet->setSetting('balance_warning_reminder', $now); | ||||
\Log::info("[WalletCheck] Reminder sent for {$this->wallet->owner->email}"); | |||||
} | } | ||||
/** | /** | ||||
* Suspend the account (and send the warning) | * Suspend the account (and send the warning) | ||||
*/ | */ | ||||
protected function suspendAccount() | protected function suspendAccount() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_suspended')) { | if ($this->wallet->getSetting('balance_warning_suspended')) { | ||||
return; | return; | ||||
} | } | ||||
// Sanity check, already deleted | // Sanity check, already deleted | ||||
if (!$this->wallet->owner) { | if (!$this->wallet->owner) { | ||||
return; | return; | ||||
} | } | ||||
\Log::info("[WalletCheck] Suspend account {$this->wallet->owner->email}"); | |||||
// Suspend the account | // Suspend the account | ||||
$this->wallet->owner->suspend(); | $this->wallet->owner->suspend(); | ||||
foreach ($this->wallet->entitlements as $entitlement) { | foreach ($this->wallet->entitlements as $entitlement) { | ||||
if ( | if ( | ||||
$entitlement->entitleable_type == \App\Domain::class | $entitlement->entitleable_type == \App\Domain::class | ||||
|| $entitlement->entitleable_type == \App\User::class | || $entitlement->entitleable_type == \App\User::class | ||||
) { | ) { | ||||
$entitlement->entitleable->suspend(); | $entitlement->entitleable->suspend(); | ||||
} | } | ||||
} | } | ||||
$this->sendMail(\App\Mail\NegativeBalanceSuspended::class); | $label = "Account suspended"; | ||||
$this->sendMail(\App\Mail\NegativeBalanceSuspended::class, false, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_suspended', $now); | $this->wallet->setSetting('balance_warning_suspended', $now); | ||||
} | } | ||||
/** | /** | ||||
* Send the last warning before delete | * Send the last warning before delete | ||||
*/ | */ | ||||
protected function warnBeforeDelete() | protected function warnBeforeDelete() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_before_delete')) { | if ($this->wallet->getSetting('balance_warning_before_delete')) { | ||||
return; | return; | ||||
} | } | ||||
// Sanity check, already deleted | // Sanity check, already deleted | ||||
if (!$this->wallet->owner) { | if (!$this->wallet->owner) { | ||||
return; | return; | ||||
} | } | ||||
$this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true); | $label = "Last warning sent for"; | ||||
$this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_before_delete', $now); | $this->wallet->setSetting('balance_warning_before_delete', $now); | ||||
\Log::info("[WalletCheck] Last warning sent for {$this->wallet->owner->email}"); | |||||
} | } | ||||
/** | /** | ||||
* Delete the account | * Delete the account | ||||
*/ | */ | ||||
protected function deleteAccount() | protected function deleteAccount() | ||||
{ | { | ||||
// TODO: This will not work when we actually allow multiple-wallets per account | // TODO: This will not work when we actually allow multiple-wallets per account | ||||
// but in this case we anyway have to change the whole thing | // but in this case we anyway have to change the whole thing | ||||
// and calculate summarized balance from all wallets. | // and calculate summarized balance from all wallets. | ||||
// The dirty work will be done by UserObserver | // The dirty work will be done by UserObserver | ||||
if ($this->wallet->owner) { | if ($this->wallet->owner) { | ||||
\Log::info("[WalletCheck] Delete account {$this->wallet->owner->email}"); | $email = $this->wallet->owner->email; | ||||
$this->wallet->owner->delete(); | $this->wallet->owner->delete(); | ||||
\Log::info( | |||||
sprintf( | |||||
"[WalletCheck] Account deleted %s (%s)", | |||||
$this->wallet->id, | |||||
) | |||||
); | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Send the email | * Send the email | ||||
* | * | ||||
* @param string $class Mailable class name | * @param string $class Mailable class name | ||||
* @param bool $with_external Use users's external email | * @param bool $with_external Use users's external email | ||||
* @param ?string $log_label Log label | |||||
*/ | */ | ||||
protected function sendMail($class, $with_external = false): void | protected function sendMail($class, $with_external = false, $log_label = null): void | ||||
{ | { | ||||
// TODO: Send the email to all wallet controllers? | // TODO: Send the email to all wallet controllers? | ||||
$mail = new $class($this->wallet, $this->wallet->owner); | $mail = new $class($this->wallet, $this->wallet->owner); | ||||
list($to, $cc) = \App\Mail\Helper::userEmails($this->wallet->owner, $with_external); | list($to, $cc) = \App\Mail\Helper::userEmails($this->wallet->owner, $with_external); | ||||
if (!empty($to) || !empty($cc)) { | if (!empty($to) || !empty($cc)) { | ||||
try { | try { | ||||
Mail::to($to)->cc($cc)->send($mail); | Mail::to($to)->cc($cc)->send($mail); | ||||
if ($log_label) { | |||||
$msg = sprintf( | |||||
"[WalletCheck] %s %s (%s)", | |||||
$log_label, | |||||
$this->wallet->id, | |||||
empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), | |||||
); | |||||
\Log::info($msg); | |||||
} | |||||
} catch (\Exception $e) { | } catch (\Exception $e) { | ||||
$msg = sprintf( | $msg = sprintf( | ||||
"[WalletCheck] Failed to send mail for wallet %s (%s): %s", | "[WalletCheck] Failed to send mail for %s (%s): %s", | ||||
$this->wallet->id, | $this->wallet->id, | ||||
json_encode(array_merge([$to], $cc)), | empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), | ||||
$e->getMessage() | $e->getMessage() | ||||
); | ); | ||||
\Log::error($msg); | \Log::error($msg); | ||||
throw $e; | throw $e; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |