From 54fc78a88ba7118e0b997d5afd72c5b12f757847 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 27 Dec 2019 11:28:36 +1100 Subject: [PATCH] Refactor jobs to be MultiDB aware (#3174) --- app/Console/Commands/CreateTestData.php | 6 +- app/Events/Payment/PaymentWasCreated.php | 5 +- app/Http/Controllers/ClientController.php | 39 +++------- app/Http/Controllers/InvoiceController.php | 8 +-- app/Http/Controllers/PaymentController.php | 2 +- app/Jobs/Client/StoreClient.php | 59 --------------- app/Jobs/Client/UpdateClient.php | 56 --------------- app/Jobs/Invitation/MarkOpened.php | 2 + app/Jobs/Invoice/ApplyClientPayment.php | 9 ++- app/Jobs/Invoice/ApplyInvoiceNumber.php | 10 ++- app/Jobs/Invoice/ApplyInvoicePayment.php | 10 ++- app/Jobs/Invoice/ApplyPaymentToInvoice.php | 11 ++- app/Jobs/Invoice/CreateInvoiceInvitations.php | 12 +++- app/Jobs/Invoice/CreateInvoicePdf.php | 8 ++- app/Jobs/Invoice/EmailInvoice.php | 9 ++- app/Jobs/Invoice/InvoiceActions.php | 69 ------------------ app/Jobs/Invoice/MarkInvoicePaid.php | 11 ++- app/Jobs/Invoice/ReverseInvoicePayment.php | 9 ++- app/Jobs/Invoice/StoreInvoice.php | 11 ++- app/Jobs/Invoice/UpdateInvoicePayment.php | 9 ++- app/Jobs/Payment/PaymentNotification.php | 8 ++- app/Jobs/Quote/ApplyQuoteNumber.php | 11 ++- app/Jobs/Quote/CreateQuoteInvitations.php | 11 ++- app/Jobs/Util/UploadFile.php | 8 ++- .../Invoice/UpdateInvoiceActivity.php | 2 + app/Models/Invoice.php | 4 +- .../PayPalExpressPaymentDriver.php | 2 +- app/PaymentDrivers/StripePaymentDriver.php | 4 +- app/Repositories/InvoiceRepository.php | 6 +- app/Repositories/PaymentRepository.php | 6 +- app/Repositories/QuoteRepository.php | 4 +- database/seeds/RandomDataSeeder.php | 2 +- phpunit.xml | 2 +- tests/Feature/ClientTest.php | 72 +++++++++---------- tests/Feature/InvoiceEmailTest.php | 2 +- tests/Integration/MarkInvoicePaidTest.php | 2 +- tests/Integration/UpdateCompanyLedgerTest.php | 2 +- 37 files changed, 201 insertions(+), 302 deletions(-) delete mode 100644 app/Jobs/Client/StoreClient.php delete mode 100644 app/Jobs/Client/UpdateClient.php delete mode 100644 app/Jobs/Invoice/InvoiceActions.php diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 57452a1ca8f8..416c72fdab9a 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -320,7 +320,7 @@ class CreateTestData extends Command $this->invoice_repo->markSent($invoice); - CreateInvoiceInvitations::dispatch($invoice); + CreateInvoiceInvitations::dispatch($invoice, $invoice->company); if(rand(0, 1)) { @@ -337,7 +337,7 @@ class CreateTestData extends Command event(new PaymentWasCreated($payment)); - UpdateInvoicePayment::dispatchNow($payment); + UpdateInvoicePayment::dispatchNow($payment, $payment->company); } } @@ -380,7 +380,7 @@ class CreateTestData extends Command $quote->save(); - CreateQuoteInvitations::dispatch($quote); + CreateQuoteInvitations::dispatch($quote, $quote->company); } diff --git a/app/Events/Payment/PaymentWasCreated.php b/app/Events/Payment/PaymentWasCreated.php index e1d8366defdf..e5496802d16c 100644 --- a/app/Events/Payment/PaymentWasCreated.php +++ b/app/Events/Payment/PaymentWasCreated.php @@ -11,6 +11,7 @@ namespace App\Events\Payment; +use App\Models\Company; use App\Models\Payment; use Illuminate\Queue\SerializesModels; @@ -26,13 +27,15 @@ class PaymentWasCreated */ public $payment; + public $company; /** * Create a new event instance. * * @param Payment $payment */ - public function __construct(Payment $payment) + public function __construct(Payment $payment, Company $company) { $this->payment = $payment; + $this->company = $company; } } diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 3a3829f21b2f..681a77697d43 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -505,36 +505,19 @@ class ClientController extends BaseController * ), * ) */ - public function bulk(BulkClientRequest $request) + public function bulk() { - $action = $request->action; - $ids = []; - - if ($request->action !== self::$STORE_METHOD) { - - $ids = $request->ids; - - $clients = Client::withTrashed()->find($this->transformKeys($ids)); - - $clients->each(function ($client, $key) use ($request, $action) { - - if (auth()->user()->can($request->action, $client)) - $this->client_repo->{$action}($client); - - }); - - } - - if($request->action == self::$STORE_METHOD) { - - /** Small hunks of data (originally 100) */ - $chunks = array_chunk($request->clients, self::$CHUNK_SIZE); - - foreach($chunks as $data) { - dispatch(new ProcessBulk($data, $this->client_repo, self::$STORE_METHOD))->onQueue(self::$DEFAULT_QUEUE); - } - } + $action = request()->input('action'); + + $ids = request()->input('ids'); + $clients = Client::withTrashed()->find($this->transformKeys($ids)); + + $clients->each(function ($client, $key) use($action){ + if(auth()->user()->can('edit', $client)) + $this->client_repo->{$action}($client); + }); + return $this->listResponse(Client::withTrashed()->whereIn('id', $this->transformKeys($ids))); } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 35eae3b4866a..f661d2f69318 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -214,7 +214,7 @@ class InvoiceController extends BaseController $invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id)); - $invoice = StoreInvoice::dispatchNow($invoice, $request->all()); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI + $invoice = StoreInvoice::dispatchNow($invoice, $request->all(), $invoice->company); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI event(new InvoiceWasCreated($invoice)); @@ -396,7 +396,7 @@ class InvoiceController extends BaseController $invoice = $this->invoice_repo->save($request->all(), $invoice); - event(new InvoiceWasUpdated($invoice)); + event(new InvoiceWasUpdated($invoice, $invoice->company)); return $this->itemResponse($invoice); @@ -634,7 +634,7 @@ class InvoiceController extends BaseController if($invoice->balance <= 0 || $invoice->status_id == Invoice::STATUS_PAID) return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400); - $invoice = MarkInvoicePaid::dispatchNow($invoice); + $invoice = MarkInvoicePaid::dispatchNow($invoice, $invoice->company); if(!$bulk) return $this->itemResponse($invoice); @@ -661,7 +661,7 @@ class InvoiceController extends BaseController return $this->listResponse($invoice); break; case 'email': - EmailInvoice::dispatch($invoice); + EmailInvoice::dispatch($invoice, $invoice->company); if(!$bulk) return response()->json(['message'=>'email sent'],200); break; diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 4531bc6635fc..c89527ea0103 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -484,7 +484,7 @@ class PaymentController extends BaseController public function destroy(DestroyPaymentRequest $request, Payment $payment) { - ReverseInvoicePayment::dispatchNow($payment); + ReverseInvoicePayment::dispatchNow($payment, $payment->company); $payment->is_deleted = true; $payment->save(); diff --git a/app/Jobs/Client/StoreClient.php b/app/Jobs/Client/StoreClient.php deleted file mode 100644 index d47165a248d8..000000000000 --- a/app/Jobs/Client/StoreClient.php +++ /dev/null @@ -1,59 +0,0 @@ -data = $data; - - $this->client = $client; - } - - /** - * Execute the job. - * - * @param ClientRepository $client_repo - * @param ClientContactRepository $client_contact_repo - * @return Client|null - */ - public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) : ?Client { - - $client = $client_repo->save($this->data, $this->client); - - $contacts = $client_contact_repo->save($data['contacts']), $client); - - return $client; - } -} diff --git a/app/Jobs/Client/UpdateClient.php b/app/Jobs/Client/UpdateClient.php deleted file mode 100644 index 958fea3f49de..000000000000 --- a/app/Jobs/Client/UpdateClient.php +++ /dev/null @@ -1,56 +0,0 @@ -data = $data; - $this->client = $client; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) :?Client - { - $client = $client_repo->save($this->data, $this->client); - - $contacts = $client_contact_repo->save($data['contacts']), $client); - - return $client->fresh(); - } -} diff --git a/app/Jobs/Invitation/MarkOpened.php b/app/Jobs/Invitation/MarkOpened.php index 239a40d8d6bf..92ef9301dd1d 100644 --- a/app/Jobs/Invitation/MarkOpened.php +++ b/app/Jobs/Invitation/MarkOpened.php @@ -23,6 +23,8 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Mail; +//todo - ensure we are MultiDB Aware in dispatched jobs + class MarkOpened implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter; diff --git a/app/Jobs/Invoice/ApplyClientPayment.php b/app/Jobs/Invoice/ApplyClientPayment.php index 29969d49949c..586aa843c491 100644 --- a/app/Jobs/Invoice/ApplyClientPayment.php +++ b/app/Jobs/Invoice/ApplyClientPayment.php @@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyPaymentToInvoice; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Repositories\InvoiceRepository; @@ -32,15 +34,18 @@ class ApplyClientPayment implements ShouldQueue public $payment; + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Payment $payment) + public function __construct(Payment $payment, Company $company) { $this->payment = $payment; + $this->company = $company; } @@ -53,6 +58,8 @@ class ApplyClientPayment implements ShouldQueue public function handle() { + MultiDB::setDB($this->company->db); + $client = $this->payment->client; $client->credit_balance += $this->payment->amount; $client->save(); diff --git a/app/Jobs/Invoice/ApplyInvoiceNumber.php b/app/Jobs/Invoice/ApplyInvoiceNumber.php index b8554f4c3d0c..c58d9e782100 100644 --- a/app/Jobs/Invoice/ApplyInvoiceNumber.php +++ b/app/Jobs/Invoice/ApplyInvoiceNumber.php @@ -11,6 +11,7 @@ namespace App\Jobs\Invoice; +use App\Libraries\MultiDB; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentTerm; @@ -32,17 +33,20 @@ class ApplyInvoiceNumber implements ShouldQueue public $settings; + private $company; /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice, $settings) + public function __construct(Invoice $invoice, $settings, $company) { $this->invoice = $invoice; $this->settings = $settings; + + $this->company = $company; } /** @@ -53,6 +57,10 @@ class ApplyInvoiceNumber implements ShouldQueue */ public function handle() { + + MultiDB::setDB($this->company->db); + + //return early if($this->invoice->number != '') return $this->invoice; diff --git a/app/Jobs/Invoice/ApplyInvoicePayment.php b/app/Jobs/Invoice/ApplyInvoicePayment.php index ead0c383babd..8e63a140c8f7 100644 --- a/app/Jobs/Invoice/ApplyInvoicePayment.php +++ b/app/Jobs/Invoice/ApplyInvoicePayment.php @@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyPaymentToInvoice; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Repositories\InvoiceRepository; @@ -35,17 +37,21 @@ class ApplyInvoicePayment implements ShouldQueue public $payment; public $amount; + + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice, Payment $payment, float $amount) + public function __construct(Invoice $invoice, Payment $payment, float $amount, Company $company) { $this->invoice = $invoice; $this->payment = $payment; $this->amount = $amount; + $this->company = $company; } @@ -58,6 +64,8 @@ class ApplyInvoicePayment implements ShouldQueue public function handle() { + MultiDB::setDB($this->company->db); + UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->amount*-1)); UpdateClientBalance::dispatchNow($this->payment->client, $this->amount*-1); UpdateClientPaidToDate::dispatchNow($this->payment->client, $this->amount); diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php index f4133ac5f702..03a77dc1ce22 100644 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ b/app/Jobs/Invoice/ApplyPaymentToInvoice.php @@ -13,6 +13,8 @@ namespace App\Jobs\Invoice; use App\Events\Invoice\InvoiceWasPaid; use App\Jobs\Invoice\ApplyInvoiceNumber; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentTerm; @@ -33,18 +35,21 @@ class ApplyPaymentToInvoice implements ShouldQueue public $payment; + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Payment $payment, Invoice $invoice) + public function __construct(Payment $payment, Invoice $invoice, Company $company) { $this->invoice = $invoice; $this->payment = $payment; + $this->company = $company; } /** @@ -56,6 +61,8 @@ class ApplyPaymentToInvoice implements ShouldQueue public function handle() { + MultiDB::setDB($this->company->db); + /* The amount we are adjusting the invoice by*/ $adjustment = $this->payment->amount * -1; @@ -113,7 +120,7 @@ class ApplyPaymentToInvoice implements ShouldQueue $this->invoice->save(); - $this->invoice = ApplyInvoiceNumber::dispatchNow($this->invoice, $invoice->client->getMergedSettings()); + $this->invoice = ApplyInvoiceNumber::dispatchNow($this->invoice, $invoice->client->getMergedSettings(), $this->invoice->company); return $this->invoice; } diff --git a/app/Jobs/Invoice/CreateInvoiceInvitations.php b/app/Jobs/Invoice/CreateInvoiceInvitations.php index 007c2f51dc6e..518966273967 100644 --- a/app/Jobs/Invoice/CreateInvoiceInvitations.php +++ b/app/Jobs/Invoice/CreateInvoiceInvitations.php @@ -12,6 +12,8 @@ namespace App\Jobs\Invoice; use App\Factory\InvoiceInvitationFactory; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\InvoiceInvitation; use Illuminate\Bus\Queueable; @@ -27,21 +29,25 @@ class CreateInvoiceInvitations implements ShouldQueue private $invoice; + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice) + public function __construct(Invoice $invoice, Company $company) { $this->invoice = $invoice; - + $this->company = $company; + } public function handle() { - + MultiDB::setDB($this->company->db); + $contacts = $this->invoice->client->contacts; $contacts->each(function ($contact) { diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 6ea98b0dba2e..1c0ef31519c0 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -11,12 +11,14 @@ namespace App\Jobs\Invoice; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentTerm; use App\Repositories\InvoiceRepository; -use App\Utils\Traits\NumberFormatter; use App\Utils\Traits\MakesInvoiceHtml; +use App\Utils\Traits\NumberFormatter; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -40,16 +42,18 @@ class CreateInvoicePdf implements ShouldQueue * * @return void */ - public function __construct(Invoice $invoice) + public function __construct(Invoice $invoice, Company $company) { $this->invoice = $invoice; + $this->company = $company; } public function handle() { + MultiDB::setDB($this->company->db); $this->invoice->load('client'); $path = 'public/' . $this->invoice->client->client_hash . '/invoices/'; diff --git a/app/Jobs/Invoice/EmailInvoice.php b/app/Jobs/Invoice/EmailInvoice.php index 23c97b2f477c..fede4eeede5c 100644 --- a/app/Jobs/Invoice/EmailInvoice.php +++ b/app/Jobs/Invoice/EmailInvoice.php @@ -15,6 +15,7 @@ use App\Events\Invoice\InvoiceWasEmailed; use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Libraries\MultiDB; use App\Mail\TemplateEmail; +use App\Models\Company; use App\Models\Invoice; use App\Models\SystemLog; use Illuminate\Bus\Queueable; @@ -32,16 +33,20 @@ class EmailInvoice implements ShouldQueue public $message_array = []; + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice) + public function __construct(Invoice $invoice, Company $company) { $this->invoice = $invoice; + $this->company = $company; + } /** @@ -53,7 +58,7 @@ class EmailInvoice implements ShouldQueue public function handle() { /*Jobs are not multi-db aware, need to set! */ - MultiDB::setDB($this->invoice->company->db); + MultiDB::setDB($this->company->db); //todo - change runtime config of mail driver if necessary diff --git a/app/Jobs/Invoice/InvoiceActions.php b/app/Jobs/Invoice/InvoiceActions.php deleted file mode 100644 index 3a8c20f867ce..000000000000 --- a/app/Jobs/Invoice/InvoiceActions.php +++ /dev/null @@ -1,69 +0,0 @@ -invoice = $invoice; - - $this->data = $data; - - } - - /** - * Execute the job. - * - * - * @return void - */ - public function handle() - { - - switch($this->data['action']) - { - //fire actions here - } - - } -} diff --git a/app/Jobs/Invoice/MarkInvoicePaid.php b/app/Jobs/Invoice/MarkInvoicePaid.php index 70db6138b485..f9f7a8858b2d 100644 --- a/app/Jobs/Invoice/MarkInvoicePaid.php +++ b/app/Jobs/Invoice/MarkInvoicePaid.php @@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyPaymentToInvoice; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Repositories\InvoiceRepository; @@ -32,15 +34,17 @@ class MarkInvoicePaid implements ShouldQueue public $invoice; + private $company; /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice) + public function __construct(Invoice $invoice, Company $company) { $this->invoice = $invoice; + $this->company = $company; } @@ -52,6 +56,9 @@ class MarkInvoicePaid implements ShouldQueue */ public function handle() { + MultiDB::setDB($this->company->db); + + /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); @@ -69,7 +76,7 @@ class MarkInvoicePaid implements ShouldQueue $this->invoice->updateBalance($payment->amount*-1); /* Update Invoice balance */ - event(new PaymentWasCreated($payment)); + event(new PaymentWasCreated($payment, $payment->company)); UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1)); UpdateClientBalance::dispatchNow($payment->client, $payment->amount*-1); diff --git a/app/Jobs/Invoice/ReverseInvoicePayment.php b/app/Jobs/Invoice/ReverseInvoicePayment.php index a21d985f1de5..3a67569517d6 100644 --- a/app/Jobs/Invoice/ReverseInvoicePayment.php +++ b/app/Jobs/Invoice/ReverseInvoicePayment.php @@ -16,6 +16,8 @@ use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Util\SystemLogger; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Models\Payment; use App\Models\SystemLog; @@ -31,14 +33,18 @@ class ReverseInvoicePayment implements ShouldQueue use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $payment; + + private $company; + /** * Create the event listener. * * @return void */ - public function __construct(Payment $payment) + public function __construct(Payment $payment, Company $company) { $this->payment = $payment; + $this->company = $company; } /** @@ -49,6 +55,7 @@ class ReverseInvoicePayment implements ShouldQueue */ public function handle() { + MultiDB::setDB($this->company->db); $invoices = $this->payment->invoices()->get(); $client = $this->payment->client; diff --git a/app/Jobs/Invoice/StoreInvoice.php b/app/Jobs/Invoice/StoreInvoice.php index 6454e20424ed..41f2e9588df8 100644 --- a/app/Jobs/Invoice/StoreInvoice.php +++ b/app/Jobs/Invoice/StoreInvoice.php @@ -13,6 +13,8 @@ namespace App\Jobs\Invoice; use App\Jobs\Invoice\InvoiceNotification; use App\Jobs\Payment\PaymentNotification; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Invoice; use App\Repositories\InvoiceRepository; use Illuminate\Bus\Queueable; @@ -29,18 +31,20 @@ class StoreInvoice implements ShouldQueue protected $data; + private $company; /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice, array $data) + public function __construct(Invoice $invoice, array $data, Company $company) { $this->invoice = $invoice; $this->data = $data; + $this->company = $company; } /** @@ -61,6 +65,7 @@ class StoreInvoice implements ShouldQueue */ public function handle(InvoiceRepository $invoice_repo) : ?Invoice { + MultiDB::setDB($this->company->db); $payment = false; @@ -81,7 +86,7 @@ class StoreInvoice implements ShouldQueue $this->invoice = $invoice_repo->markSent($this->invoice); //fire invoice job (the job performs the filtering logic of the email recipients... if any.) - InvoiceNotification::dispatch($invoice); + InvoiceNotification::dispatch($invoice, $invoice->company); } @@ -100,7 +105,7 @@ class StoreInvoice implements ShouldQueue if($payment) { //fire payment notifications here - PaymentNotification::dispatch($payment); + PaymentNotification::dispatch($payment, $payment->company); } diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Jobs/Invoice/UpdateInvoicePayment.php index de64bc658d7e..8b062dd54093 100644 --- a/app/Jobs/Invoice/UpdateInvoicePayment.php +++ b/app/Jobs/Invoice/UpdateInvoicePayment.php @@ -16,6 +16,8 @@ use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Util\SystemLogger; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Payment; use App\Models\SystemLog; use App\Utils\Traits\SystemLogTrait; @@ -30,14 +32,18 @@ class UpdateInvoicePayment implements ShouldQueue use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $payment; + + private $company; + /** * Create the event listener. * * @return void */ - public function __construct(Payment $payment) + public function __construct(Payment $payment, Company $company) { $this->payment = $payment; + $this->company = $company; } /** @@ -48,6 +54,7 @@ class UpdateInvoicePayment implements ShouldQueue */ public function handle() { + MultiDB::setDB($this->company->db); $invoices = $this->payment->invoices()->get(); diff --git a/app/Jobs/Payment/PaymentNotification.php b/app/Jobs/Payment/PaymentNotification.php index b13610fb05ed..84552eb2b3fe 100644 --- a/app/Jobs/Payment/PaymentNotification.php +++ b/app/Jobs/Payment/PaymentNotification.php @@ -11,6 +11,8 @@ namespace App\Jobs\Payment; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Payment; use App\Repositories\InvoiceRepository; use Illuminate\Bus\Queueable; @@ -25,16 +27,19 @@ class PaymentNotification implements ShouldQueue public $payment; + private $company; /** * Create a new job instance. * * @return void */ - public function __construct(Payment $payment) + public function __construct(Payment $payment, Company $company) { $this->payment = $payment; + $this->company = $company; + } /** @@ -45,6 +50,7 @@ class PaymentNotification implements ShouldQueue */ public function handle() { + MultiDB::setDB($this->company->db); //notification for the payment. // diff --git a/app/Jobs/Quote/ApplyQuoteNumber.php b/app/Jobs/Quote/ApplyQuoteNumber.php index cdd8e9ac3996..a12b6b2c6e5e 100644 --- a/app/Jobs/Quote/ApplyQuoteNumber.php +++ b/app/Jobs/Quote/ApplyQuoteNumber.php @@ -11,9 +11,11 @@ namespace App\Jobs\Quote; -use App\Models\Quote; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Payment; use App\Models\PaymentTerm; +use App\Models\Quote; use App\Repositories\QuoteRepository; use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\NumberFormatter; @@ -32,17 +34,20 @@ class ApplyQuoteNumber implements ShouldQueue private $settings; + private $company; /** * Create a new job instance. * * @return void */ - public function __construct(Quote $quote, $settings) + public function __construct(Quote $quote, $settings, Company $company) { $this->quote = $quote; $this->settings = $settings; + + $this->company = $company; } /** @@ -53,6 +58,8 @@ class ApplyQuoteNumber implements ShouldQueue */ public function handle() { + MultiDB::setDB($this->company->db); + //return early if($this->quote->number != '') return $this->quote; diff --git a/app/Jobs/Quote/CreateQuoteInvitations.php b/app/Jobs/Quote/CreateQuoteInvitations.php index e8870ab62d2d..8767a8413779 100644 --- a/app/Jobs/Quote/CreateQuoteInvitations.php +++ b/app/Jobs/Quote/CreateQuoteInvitations.php @@ -12,6 +12,8 @@ namespace App\Jobs\Quote; use App\Factory\QuoteInvitationFactory; +use App\Libraries\MultiDB; +use App\Models\Company; use App\Models\Quote; use App\Models\QuoteInvitation; use Illuminate\Bus\Queueable; @@ -27,21 +29,26 @@ class CreateQuoteInvitations implements ShouldQueue private $quote; + private $company; + /** * Create a new job instance. * * @return void */ - public function __construct(Quote $quote) + public function __construct(Quote $quote, Company $company) { $this->quote = $quote; + $this->company = $company; + } public function handle() { - + MultiDB::setDB($this->company->db); + $contacts = $this->quote->client->contacts; $contacts->each(function ($contact) { diff --git a/app/Jobs/Util/UploadFile.php b/app/Jobs/Util/UploadFile.php index 0c5fea99cb95..c1a5d4a96c8f 100644 --- a/app/Jobs/Util/UploadFile.php +++ b/app/Jobs/Util/UploadFile.php @@ -11,16 +11,17 @@ namespace App\Jobs\Util; +use App\Libraries\MultiDB; use App\Models\Document; use App\Utils\Traits\MakesHash; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Storage; -use Intervention\Image\ImageManager; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Http\Request; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Storage; +use Intervention\Image\ImageManager; class UploadFile implements ShouldQueue { @@ -57,6 +58,7 @@ class UploadFile implements ShouldQueue */ public function handle() : ?Document { + MultiDB::setDB($this->company->db); $path = $this->encodePrimaryKey($this->company->id); diff --git a/app/Listeners/Invoice/UpdateInvoiceActivity.php b/app/Listeners/Invoice/UpdateInvoiceActivity.php index da9c74ec1ea2..5e198fe71600 100644 --- a/app/Listeners/Invoice/UpdateInvoiceActivity.php +++ b/app/Listeners/Invoice/UpdateInvoiceActivity.php @@ -11,6 +11,7 @@ namespace App\Listeners\Invoice; +use App\Libraries\MultiDB; use App\Models\Activity; use App\Models\ClientContact; use App\Models\InvoiceInvitation; @@ -41,6 +42,7 @@ class UpdateInvoiceActivity implements ShouldQueue */ public function handle($event) { + MultiDB::setDB($event->company->db); $fields = new \stdClass; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 82ec4dae9a0e..4aca341c1c37 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -329,7 +329,7 @@ class Invoice extends BaseModel $storage_path = 'public/' . $this->client->client_hash . '/invoices/'. $this->number . '.pdf'; if(!Storage::exists($storage_path)) { - event(new InvoiceWasUpdated($this)); + event(new InvoiceWasUpdated($this, $this->company)); } return $public_path; @@ -340,7 +340,7 @@ class Invoice extends BaseModel $storage_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->number . '.pdf'; if(!Storage::exists($storage_path)) { - CreateInvoicePdf::dispatchNow($this); + CreateInvoicePdf::dispatchNow($this, $this->company); } return $storage_path; diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 3ad46f1335a3..a31782e8b234 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -166,7 +166,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver event(new PaymentWasCreated($payment)); - UpdateInvoicePayment::dispatchNow($payment); + UpdateInvoicePayment::dispatchNow($payment, $payment->company); return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index fdd2b94e2002..4457a9059dfd 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -363,9 +363,9 @@ class StripePaymentDriver extends BasePaymentDriver /* Link invoices to payment*/ $this->attachInvoices($payment, $hashed_ids); - event(new PaymentWasCreated($payment)); + event(new PaymentWasCreated($payment, $payment->company)); - UpdateInvoicePayment::dispatchNow($payment); + UpdateInvoicePayment::dispatchNow($payment, $payment->company); SystemLogger::dispatch([ 'server_response' => $payment_intent, diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index bd98f09e0067..da3a61a95aec 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -113,7 +113,7 @@ class InvoiceRepository extends BaseRepository /* If no invitations have been created, this is our fail safe to maintain state*/ if($invoice->invitations->count() == 0) - CreateInvoiceInvitations::dispatchNow($invoice); + CreateInvoiceInvitations::dispatchNow($invoice, $invoice->company); $invoice = $invoice->calc()->getInvoice(); @@ -125,7 +125,7 @@ class InvoiceRepository extends BaseRepository if($finished_amount != $starting_amount) UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount)); - $invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings()); + $invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company); if($invoice->company->update_products !== false) UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice); @@ -150,7 +150,7 @@ class InvoiceRepository extends BaseRepository * When marked as sent it becomes a ledgerable item. * */ - $invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings()); + $invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company); UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 804c3e8c15a0..1db89490bbd9 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -51,17 +51,17 @@ class PaymentRepository extends BaseRepository $invoice = Invoice::whereId($paid_invoice['id'])->company()->first(); if($invoice) - ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount']); + ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company); } } else { //paid is made, but not to any invoice, therefore we are applying the payment to the clients credit - ApplyClientPayment::dispatchNow($payment); + ApplyClientPayment::dispatchNow($payment, $payment->company); } - event(new PaymentWasCreated($payment)); + event(new PaymentWasCreated($payment, $payment->company)); //UpdateInvoicePayment::dispatchNow($payment); diff --git a/app/Repositories/QuoteRepository.php b/app/Repositories/QuoteRepository.php index a4650819d779..3cf5eb9e855c 100644 --- a/app/Repositories/QuoteRepository.php +++ b/app/Repositories/QuoteRepository.php @@ -93,7 +93,7 @@ class QuoteRepository extends BaseRepository /* If no invitations have been created, this is our fail safe to maintain state*/ if($quote->invitations->count() == 0) - CreateQuoteInvitations::dispatchNow($quote); + CreateQuoteInvitations::dispatchNow($quote, $quote->company); $quote = $quote->calc()->getInvoice(); @@ -101,7 +101,7 @@ class QuoteRepository extends BaseRepository $finished_amount = $quote->amount; - $quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings()); + $quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company); return $quote->fresh(); } diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 3a47b5b6a18e..52ad0b8d5692 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -182,7 +182,7 @@ class RandomDataSeeder extends Seeder event(new PaymentWasCreated($payment)); - UpdateInvoicePayment::dispatchNow($payment); + UpdateInvoicePayment::dispatchNow($payment, $payment->company); } }); diff --git a/phpunit.xml b/phpunit.xml index e0efb4ff5a55..8c949a4ab873 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="true"> ./tests/Unit diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 1b2a88af0d81..7ca483f04c74 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -254,50 +254,50 @@ class ClientTest extends TestCase } /** @test */ - public function testMassivelyCreatingClients() - { - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; + // public function testMassivelyCreatingClients() + // { + // $data = [ + // 'first_name' => $this->faker->firstName, + // 'last_name' => $this->faker->lastName, + // 'name' => $this->faker->company, + // 'email' => $this->faker->unique()->safeEmail, + // 'password' => 'ALongAndBrilliantPassword123', + // '_token' => csrf_token(), + // 'privacy_policy' => 1, + // 'terms_of_service' => 1 + // ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); + // $response = $this->withHeaders([ + // 'X-API-SECRET' => config('ninja.api_secret'), + // ])->post('/api/v1/signup?include=account', $data); - $response->assertStatus(200); + // $response->assertStatus(200); - $acc = $response->json(); + // $acc = $response->json(); - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); + // $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - $token = $account->default_company->tokens->first()->token; + // $token = $account->default_company->tokens->first()->token; - $body = [ - 'action' => 'create', - 'clients' => [ - ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'], - ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'], - ], - ]; + // $body = [ + // 'action' => 'create', + // 'clients' => [ + // ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'], + // ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'], + // ], + // ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, - ])->post(route('clients.bulk'), $body); + // $response = $this->withHeaders([ + // 'X-API-SECRET' => config('ninja.api_secret'), + // 'X-API-TOKEN' => $token, + // ])->post(route('clients.bulk'), $body); - $response->assertStatus(200); + // $response->assertStatus(200); - $first_record = Client::where('website', 'my-awesome-website-1.com')->first(); - $second_record = Client::where('website', 'my-awesome-website-2.com')->first(); + // $first_record = Client::where('website', 'my-awesome-website-1.com')->first(); + // $second_record = Client::where('website', 'my-awesome-website-2.com')->first(); - $this->assertNotNull($first_record); - $this->assertNotNull($second_record); - } + // $this->assertNotNull($first_record); + // $this->assertNotNull($second_record); + // } } diff --git a/tests/Feature/InvoiceEmailTest.php b/tests/Feature/InvoiceEmailTest.php index ac4b362e748e..c8640b517c73 100644 --- a/tests/Feature/InvoiceEmailTest.php +++ b/tests/Feature/InvoiceEmailTest.php @@ -63,7 +63,7 @@ class InvoiceEmailTest extends TestCase $invitations = InvoiceInvitation::whereInvoiceId($this->invoice->id)->get(); - $invitations->each(function($invitation) use($message_array, $template_styles) { + $invitations->each(function($invitation) use($message_array, $template_style) { $contact = $invitation->contact; diff --git a/tests/Integration/MarkInvoicePaidTest.php b/tests/Integration/MarkInvoicePaidTest.php index bbe7bc5018f3..39affd46139e 100644 --- a/tests/Integration/MarkInvoicePaidTest.php +++ b/tests/Integration/MarkInvoicePaidTest.php @@ -37,7 +37,7 @@ class MarkInvoicePaidTest extends TestCase public function testMarkInvoicePaidInvoice() { - MarkInvoicePaid::dispatchNow($this->invoice); + MarkInvoicePaid::dispatchNow($this->invoice, $this->company); $invoice = Invoice::find($this->invoice->id); diff --git a/tests/Integration/UpdateCompanyLedgerTest.php b/tests/Integration/UpdateCompanyLedgerTest.php index 8130f1983a3d..98f114e21c47 100644 --- a/tests/Integration/UpdateCompanyLedgerTest.php +++ b/tests/Integration/UpdateCompanyLedgerTest.php @@ -37,7 +37,7 @@ class UpdateCompanyLedgerTest extends TestCase public function testPaymentIsPresentInLedger() { - $invoice = MarkInvoicePaid::dispatchNow($this->invoice); + $invoice = MarkInvoicePaid::dispatchNow($this->invoice, $this->company); $ledger = CompanyLedger::whereClientId($invoice->client_id)