You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Listeners;
  3. use App\Models\User;
  4. use App\Models\Business;
  5. use Illuminate\Support\Arr;
  6. <<<<<<< Updated upstream
  7. use App\Channels\FcmChannel;
  8. use App\Events\BusinessUpdate;
  9. use App\Events\BusinessUserCreate;
  10. use App\Notifications\DBNotification;
  11. use App\Notifications\FcmNotification;
  12. use App\Notifications\MailNotification;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. =======
  15. use App\Events\BusinessUpdate;
  16. use App\Events\BusinessUserCreate;
  17. use App\Notifications\DBNotification;
  18. use App\Notifications\MailNotification;
  19. use Illuminate\Queue\InteractsWithQueue;
  20. >>>>>>> Stashed changes
  21. use Illuminate\Contracts\Queue\ShouldQueue;
  22. use Illuminate\Support\Facades\Notification;
  23. class BusinessUpdateListener
  24. {
  25. public function handle(BusinessUpdate $event)
  26. {
  27. $payload = $event->message;
  28. <<<<<<< Updated upstream
  29. $business = Business::findOrFail($payload->business)->load('owners');
  30. $this->checkWalletIsRunningLow($business);
  31. $this->checkWalletIsEmptyOrNegative($business);
  32. $this->accountHasBeenSuspended($business);
  33. }
  34. public function checkWalletIsRunningLow(Business $business): void
  35. {
  36. $moving_average_days = 7;
  37. $predict_ahead_hours = 24;
  38. $hours = $moving_average_days * 24;
  39. $business->load([
  40. 'cost' => fn ($query) => $query->where('created_at', '>=', now('Asia/Tehran')->subHours($hours)),
  41. ]);
  42. // The wallet is running out
  43. // Calculate the average consumption of the N past days
  44. $average_cost = $business->cost->sum('cost') / $hours;
  45. // Average hourly consumption multiplied by the next 24 hours
  46. // If the account does not charge as much as in the next 24 hours, notified.
  47. $message = ['body' => __('notification.business.wallet_almost_empty')];
  48. if (($business->wallet / $predict_ahead_hours) < $average_cost) {
  49. Notification::send($business->owners, new MailNotification($message));
  50. Notification::send($business->owners, new DBNotification($message));
  51. }
  52. }
  53. public function checkWalletIsEmptyOrNegative(Business $business): void
  54. {
  55. $message = ['body' => __('notification.business.wallet_empty')];
  56. if ($business->wallet <= 0) {
  57. Notification::send($business->owners, new MailNotification($message));
  58. Notification::send($business->owners, new DBNotification($message));
  59. }
  60. }
  61. public function accountHasBeenSuspended(Business $business): void
  62. {
  63. if ($business->wallet > 0) {
  64. return;
  65. }
  66. $recent_payments_number = 10;
  67. $negativity_threshold = 20;
  68. $business->load([
  69. 'transactions' => fn ($query) => $query->where('succeeded', '=', true)
  70. ->orderBy('created_at')
  71. ->take($recent_payments_number),
  72. ]);
  73. // Your account has been blocked.
  74. // What is the average of the last 10 payments?
  75. $average_payment = $business->transactions->average('amount');
  76. $threshold = $average_payment / 100 * $negativity_threshold;
  77. $message = ['body' => __('notification.business.suspended')];
  78. if ($business->wallet < $threshold) {
  79. Notification::send($business->owners, new MailNotification($message));
  80. Notification::send($business->owners, new DBNotification($message));
  81. =======
  82. $wallet = $payload?->data?->diff?->wallet;
  83. $owners = Business::findOrFail($payload->business)->owners;
  84. $message = ['body' => 'Test'];
  85. if ($wallet < 0) {
  86. Notification::send($owners, new MailNotification($message));
  87. Notification::send($owners, new DBNotification($message));
  88. >>>>>>> Stashed changes
  89. }
  90. }
  91. }