From 0f2ed3fe975b69761265201da06d203929db99f2 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 7 Aug 2021 19:55:18 +1000 Subject: [PATCH] Email Quotas for hosted --- app/Jobs/Mail/NinjaMailerJob.php | 3 +- app/Models/Account.php | 19 ++++++++++++ ...2524_add_email_quota_to_accounts_table.php | 30 ------------------- 3 files changed, 21 insertions(+), 31 deletions(-) delete mode 100644 database/migrations/2021_08_07_092524_add_email_quota_to_accounts_table.php diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index ce94381dc4bd..21cb877b2590 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -228,7 +228,8 @@ class NinjaMailerJob implements ShouldQueue return true; /* On the hosted platform, if the user is over the email quotas, we do not send the email. */ - //if(Ninja::isHosted()) + if(Ninja::isHosted() && $this->company->account->emailQuotaExceeded()) + return true; return false; diff --git a/app/Models/Account.php b/app/Models/Account.php index 5e2ea213798a..05b0cb7264f3 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -18,6 +18,7 @@ use Carbon\Carbon; use DateTime; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Laracasts\Presenter\PresentableTrait; +use Illuminate\Support\Facades\Cache; class Account extends BaseModel { @@ -341,4 +342,22 @@ class Account extends BaseModel } } + public function getDailyEmailLimit() + { + $limit = config('ninja.daily_email_limit'); + + $limit += Carbon::createFromTimestamp($this->created_at)->diffInMonths() * 100; + + return min($limit, 5000); + } + + + public function emailQuotaExceeded() :bool + { + if(is_null(Cache::get($this->key))) + return false; + + return Cache::get($this->key) > $this->getDailyEmailLimit(); + } + } diff --git a/database/migrations/2021_08_07_092524_add_email_quota_to_accounts_table.php b/database/migrations/2021_08_07_092524_add_email_quota_to_accounts_table.php deleted file mode 100644 index 3b78b3c4c957..000000000000 --- a/database/migrations/2021_08_07_092524_add_email_quota_to_accounts_table.php +++ /dev/null @@ -1,30 +0,0 @@ -unsignedInteger('email_quota')->nullable()->default(300); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - - } -}