From d238d53b26a1f90bb9cb7234dcac6b78573dc2b6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Mar 2021 10:40:18 +1100 Subject: [PATCH] Delete PDF helper --- app/Http/Middleware/SetEmailDb.php | 8 +++++--- app/Jobs/Entity/CreateEntityPdf.php | 2 ++ app/Jobs/Util/UnlinkFile.php | 2 ++ app/Libraries/MultiDB.php | 5 +++-- app/Services/Credit/CreditService.php | 10 +++++++++- app/Services/Quote/QuoteService.php | 8 ++++++++ app/Services/Recurring/RecurringService.php | 8 ++++++++ 7 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/Http/Middleware/SetEmailDb.php b/app/Http/Middleware/SetEmailDb.php index 2ae4e7d4890b..8a977d0f30a8 100644 --- a/app/Http/Middleware/SetEmailDb.php +++ b/app/Http/Middleware/SetEmailDb.php @@ -33,10 +33,12 @@ class SetEmailDb ]; if ($request->input('email') && config('ninja.db.multi_db_enabled')) { - nlog("trying to find db"); - if (! MultiDB::userFindAndSetDb($request->input('email'))) { + + + if (! MultiDB::userFindAndSetDb($request->input('email'))) return response()->json($error, 400); - } + + } // else { // return response()->json($error, 403); diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index e2467e254397..5f368e5d64fa 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -92,6 +92,8 @@ class CreateEntityPdf implements ShouldQueue App::forgetInstance('translator'); Lang::replace(Ninja::transformTranslations($this->entity->client->getMergedSettings())); + $this->entity->service()->deletePdf(); + if (config('ninja.phantomjs_pdf_generation')) { return (new Phantom)->generate($this->invitation); } diff --git a/app/Jobs/Util/UnlinkFile.php b/app/Jobs/Util/UnlinkFile.php index a7f5f02cb4de..70461add3b70 100644 --- a/app/Jobs/Util/UnlinkFile.php +++ b/app/Jobs/Util/UnlinkFile.php @@ -39,6 +39,8 @@ class UnlinkFile implements ShouldQueue */ public function handle() { + // nlog("deleting"); + // nlog($this->file_path); Storage::disk($this->disk)->delete($this->file_path); } } diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 3240283af898..dd5237f35ec9 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -187,9 +187,10 @@ class MultiDB //multi-db active foreach (self::$dbs as $db) { - if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail + + if (User::on($db)->where(['email' => $email])->count() >= 1) return true; - } + } return false; diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index 85ab8fddc64c..8912c41e3561 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -11,6 +11,7 @@ namespace App\Services\Credit; +use App\Jobs\Util\UnlinkFile; use App\Models\Credit; use App\Utils\Traits\MakesHash; @@ -134,7 +135,14 @@ class CreditService return $this; } - + + public function deletePdf() + { + UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath() . $this->credit->number.'.pdf'); + + return $this; + } + /** * Saves the credit. * @return Credit object diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index fbf53e623a37..01488348d12c 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -12,6 +12,7 @@ namespace App\Services\Quote; use App\Events\Quote\QuoteWasApproved; +use App\Jobs\Util\UnlinkFile; use App\Models\Invoice; use App\Models\Quote; use App\Repositories\QuoteRepository; @@ -189,6 +190,13 @@ class QuoteService return $this; } + public function deletePdf() + { + UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath() . $this->quote->number.'.pdf'); + + return $this; + } + /** * Saves the quote. * @return Quote|null diff --git a/app/Services/Recurring/RecurringService.php b/app/Services/Recurring/RecurringService.php index 9942efcc69e5..00811fa404ff 100644 --- a/app/Services/Recurring/RecurringService.php +++ b/app/Services/Recurring/RecurringService.php @@ -11,6 +11,7 @@ namespace App\Services\Recurring; +use App\Jobs\Util\UnlinkFile; use App\Models\RecurringInvoice; use App\Services\Recurring\GetInvoicePdf; use Illuminate\Support\Carbon; @@ -84,6 +85,13 @@ class RecurringService return (new GetInvoicePdf($this->recurring_entity, $contact))->run(); } + public function deletePdf() + { + UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath() . $this->recurring_entity->number.'.pdf'); + + return $this; + } + public function save() { $this->recurring_entity->save();