From 33a9cad32a86419f43fa2bc57d7e7546a8abdc4f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 14 Jan 2023 15:28:35 +1100 Subject: [PATCH 1/3] Fixes for deleting invitations after a contact has been deleted --- app/Console/Commands/ReactBuilder.php | 2 +- app/Models/ClientContact.php | 5 +++++ app/Observers/ClientContactObserver.php | 7 ++++--- app/Providers/EventServiceProvider.php | 3 +++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/ReactBuilder.php b/app/Console/Commands/ReactBuilder.php index 15a2f6520f46..7d9b36843c73 100644 --- a/app/Console/Commands/ReactBuilder.php +++ b/app/Console/Commands/ReactBuilder.php @@ -56,7 +56,7 @@ class ReactBuilder extends Command $directoryIterator = new \RecursiveDirectoryIterator(public_path('react'), \RecursiveDirectoryIterator::SKIP_DOTS); foreach (new \RecursiveIteratorIterator($directoryIterator) as $file) { - if (str_contains($file->getFileName(), '.js')) { + if (str_contains($file->getFileName(), '.js') && !strpos($file->getFileName(), '.json')) { if (str_contains($file->getFileName(), 'index.')) { $includes .= ''."\n"; } else { diff --git a/app/Models/ClientContact.php b/app/Models/ClientContact.php index d6984ad7970f..23041b063104 100644 --- a/app/Models/ClientContact.php +++ b/app/Models/ClientContact.php @@ -181,6 +181,11 @@ class ClientContact extends Authenticatable implements HasLocalePreference return $this->hasMany(InvoiceInvitation::class); } + public function recurring_invoice_invitations() + { + return $this->hasMany(RecurringInvoiceInvitation::class); + } + public function quote_invitations() { return $this->hasMany(QuoteInvitation::class); diff --git a/app/Observers/ClientContactObserver.php b/app/Observers/ClientContactObserver.php index 92b6428b253d..398db1c8a2f5 100644 --- a/app/Observers/ClientContactObserver.php +++ b/app/Observers/ClientContactObserver.php @@ -48,6 +48,7 @@ class ClientContactObserver $clientContact->invoice_invitations()->delete(); $clientContact->quote_invitations()->delete(); $clientContact->credit_invitations()->delete(); + $clientContact->recurring_invoice_invitations()->delete(); } /** @@ -58,9 +59,9 @@ class ClientContactObserver */ public function restored(ClientContact $clientContact) { - $clientContact->invoice_invitations()->restore(); - $clientContact->quote_invitations()->restore(); - $clientContact->credit_invitations()->restore(); + // $clientContact->invoice_invitations()->restore(); + // $clientContact->quote_invitations()->restore(); + // $clientContact->credit_invitations()->restore(); } /** diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2e7b59ebc4cc..c176963823ee 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -225,6 +225,7 @@ use App\Listeners\User\UpdateUserLastLogin; use App\Listeners\User\UpdatedUserActivity; use App\Models\Account; use App\Models\Client; +use App\Models\ClientContact; use App\Models\Company; use App\Models\CompanyGateway; use App\Models\CompanyToken; @@ -241,6 +242,7 @@ use App\Models\Subscription; use App\Models\Task; use App\Models\User; use App\Observers\AccountObserver; +use App\Observers\ClientContactObserver; use App\Observers\ClientObserver; use App\Observers\CompanyGatewayObserver; use App\Observers\CompanyObserver; @@ -633,6 +635,7 @@ class EventServiceProvider extends ServiceProvider Account::observe(AccountObserver::class); Subscription::observe(SubscriptionObserver::class); Client::observe(ClientObserver::class); + ClientContact::observe(ClientContactObserver::class); Company::observe(CompanyObserver::class); CompanyGateway::observe(CompanyGatewayObserver::class); CompanyToken::observe(CompanyTokenObserver::class); From 6541bb2bfe64335e0e2c87864be008588125fa12 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 14 Jan 2023 15:39:17 +1100 Subject: [PATCH 2/3] Remove redundant test class --- tests/Feature/Scheduler/SchedulerTest.php | 148 ---------------------- 1 file changed, 148 deletions(-) delete mode 100644 tests/Feature/Scheduler/SchedulerTest.php diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php deleted file mode 100644 index 11561e0f5a49..000000000000 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ /dev/null @@ -1,148 +0,0 @@ -faker = \Faker\Factory::create(); - - Model::reguard(); - - $this->makeTestData(); - - $this->withoutMiddleware( - ThrottleRequests::class - ); - - // $this->withoutExceptionHandling(); - } - - public function testSchedulerCantBeCreatedWithWrongData() - { - $data = [ - 'repeat_every' => Scheduler::DAILY, - 'job' => Scheduler::CREATE_CLIENT_REPORT, - 'date_key' => '123', - 'report_keys' => ['test'], - 'date_range' => 'all', - // 'start_from' => '2022-01-01' - ]; - - $response = false; - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/task_scheduler/', $data); - - $response->assertSessionHasErrors(); - } - - public function testSchedulerCanBeUpdated() - { - $response = $this->createScheduler(); - - $arr = $response->json(); - $id = $arr['data']['id']; - - $scheduler = Scheduler::find($this->decodePrimaryKey($id)); - - $updateData = [ - 'start_from' => 1655934741, - ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/task_scheduler/'.$this->encodePrimaryKey($scheduler->id), $updateData); - - $responseData = $response->json(); - $this->assertEquals($updateData['start_from'], $responseData['data']['start_from']); - } - - public function testSchedulerCanBeSeen() - { - $response = $this->createScheduler(); - - $arr = $response->json(); - $id = $arr['data']['id']; - - $scheduler = Scheduler::find($this->decodePrimaryKey($id)); - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->get('/api/v1/task_scheduler/'.$this->encodePrimaryKey($scheduler->id)); - - $arr = $response->json(); - $this->assertEquals('create_client_report', $arr['data']['action_name']); - } - - public function testSchedulerJobCanBeUpdated() - { - $response = $this->createScheduler(); - - $arr = $response->json(); - $id = $arr['data']['id']; - - $scheduler = Scheduler::find($this->decodePrimaryKey($id)); - - $this->assertSame('create_client_report', $scheduler->action_name); - - $updateData = [ - 'job' => Scheduler::CREATE_CREDIT_REPORT, - 'date_range' => 'all', - 'report_keys' => ['test1'], - ]; - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/task_scheduler/'.$this->encodePrimaryKey($scheduler->id), $updateData); - - $updatedSchedulerJob = Scheduler::first()->action_name; - $arr = $response->json(); - - $this->assertSame('create_credit_report', $arr['data']['action_name']); - } - - public function createScheduler() - { - $data = [ - 'repeat_every' => Scheduler::DAILY, - 'job' => Scheduler::CREATE_CLIENT_REPORT, - 'date_key' => '123', - 'report_keys' => ['test'], - 'date_range' => 'all', - 'start_from' => '2022-01-01', - ]; - - return $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/task_scheduler/', $data); - } -} From 9de6ee1d2aef2574e485d1f93ff36a2da5a23238 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 14 Jan 2023 16:22:26 +1100 Subject: [PATCH 3/3] Change the order we check a payment deleted status --- app/Repositories/PaymentRepository.php | 3 ++- app/Services/Client/ClientService.php | 6 +++--- app/Services/Payment/DeletePayment.php | 23 +++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 7d6cad5386a5..bc1a0c10ab52 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -257,7 +257,8 @@ class PaymentRepository extends BaseRepository { $payment = $payment->service()->deletePayment(); - event(new PaymentWasDeleted($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + if($payment) + event(new PaymentWasDeleted($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); return $payment; diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 221c048ed0e5..e60fee96e2a0 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -37,7 +37,7 @@ class ClientService $this->client->balance += $amount; $this->client->save(); - }, 2); + }, 1); } catch (\Throwable $throwable) { nlog("DB ERROR " . $throwable->getMessage()); @@ -58,7 +58,7 @@ class ClientService $this->client->paid_to_date += $paid_to_date; $this->client->save(); - }, 2); + }, 1); } catch (\Throwable $throwable) { nlog("DB ERROR " . $throwable->getMessage()); @@ -79,7 +79,7 @@ class ClientService $this->client->paid_to_date += $amount; $this->client->save(); - }, 2); + }, 1); return $this; } diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index beab99fb323f..10db3699ee14 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -38,22 +38,21 @@ class DeletePayment \DB::connection(config('database.default'))->transaction(function () { - - if ($this->payment->is_deleted) { - return $this->payment; - } - $this->payment = Payment::withTrashed()->where('id', $this->payment->id)->lockForUpdate()->first(); - $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment - ->updateCreditables() //return the credits first - ->adjustInvoices() - ->deletePaymentables() - ->cleanupPayment() - ->save(); + if (!$this->payment->is_deleted) { + + $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment + ->updateCreditables() //return the credits first + ->adjustInvoices() + ->deletePaymentables() + ->cleanupPayment() + ->save(); + + } - }, 2); + }, 1); return $this->payment;