From 43b917d8ec3ffc854e5db49ea1c065507d382774 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Mon, 23 Jan 2023 17:24:57 +0530 Subject: [PATCH 1/4] Fix #5981 * Add deleted observer on the contact model --- app/Models/Contact/Contact.php | 17 +++++++++++++++++ tests/Feature/MeTest.php | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index 48c6f9687d3..d924681fa9a 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -32,6 +32,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Validation\ValidationException; use Illuminate\Contracts\Filesystem\Filesystem; +use App\Services\Contact\Contact\DeleteMeContact; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -167,6 +168,22 @@ class Contact extends Model */ protected $nameOrder = 'firstname_lastname'; + /** + * Boot all the event observers for the model. + */ + protected static function booted() + { + static::deleted(function ($contact) { + if ($contact->isMe() === true) + { + app(DeleteMeContact::class)->execute([ + 'account_id' => $contact->id, + 'user_id' => auth()->user()->id, + ]); + } + }); + } + /** * Get the user associated with the contact. * diff --git a/tests/Feature/MeTest.php b/tests/Feature/MeTest.php index 7eeb0189305..0ef88150853 100644 --- a/tests/Feature/MeTest.php +++ b/tests/Feature/MeTest.php @@ -84,4 +84,24 @@ public function it_deletes_me() 'me_contact_id' => null, ]); } + + /** @test */ + public function it_deletes_me_contact() + { + $user = $this->signin(); + $contact = factory(Contact::class)->create([ + 'account_id' => $user->account_id, + ]); + $user->me_contact_id = $contact->id; + $user->save(); + + $response = $this->json('DELETE', '/contacts/' . $contact->id); + + $response->assertStatus(200); + + $this->assertDatabaseHas('users', [ + 'id' => $user->id, + 'me_contact_id' => null, + ]); + } } From abb9badd96c2b28c53197e2ed87c45f44855f58f Mon Sep 17 00:00:00 2001 From: Vivek Date: Mon, 23 Jan 2023 18:38:21 +0530 Subject: [PATCH 2/4] Fix style CI comments --- app/Models/Contact/Contact.php | 3 ++- tests/Feature/MeTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index d924681fa9a..b5a5bb6a549 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -173,7 +173,8 @@ class Contact extends Model */ protected static function booted() { - static::deleted(function ($contact) { + static::deleted(function ($contact) + { if ($contact->isMe() === true) { app(DeleteMeContact::class)->execute([ diff --git a/tests/Feature/MeTest.php b/tests/Feature/MeTest.php index 0ef88150853..d8f2daac888 100644 --- a/tests/Feature/MeTest.php +++ b/tests/Feature/MeTest.php @@ -95,7 +95,7 @@ public function it_deletes_me_contact() $user->me_contact_id = $contact->id; $user->save(); - $response = $this->json('DELETE', '/contacts/' . $contact->id); + $response = $this->json('DELETE', '/contacts/'.$contact->id); $response->assertStatus(200); From 528aaefea09fda9f4aa3bca13b12b6f4db65fd0e Mon Sep 17 00:00:00 2001 From: Vivek Date: Mon, 23 Jan 2023 18:47:38 +0530 Subject: [PATCH 3/4] Fix style CI --- app/Models/Contact/Contact.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index b5a5bb6a549..a75e8206b0f 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -173,10 +173,8 @@ class Contact extends Model */ protected static function booted() { - static::deleted(function ($contact) - { - if ($contact->isMe() === true) - { + static::deleted(function ($contact) { + if ($contact->isMe() === true) { app(DeleteMeContact::class)->execute([ 'account_id' => $contact->id, 'user_id' => auth()->user()->id, From ed308895c3d9b91bd720de48154a4ff87dadbf3b Mon Sep 17 00:00:00 2001 From: Vivek Date: Wed, 1 Mar 2023 08:25:48 +0530 Subject: [PATCH 4/4] Apply suggestions from code review --- app/Models/Contact/Contact.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index a75e8206b0f..488fd2915ec 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -173,7 +173,8 @@ class Contact extends Model */ protected static function booted() { - static::deleted(function ($contact) { + static::deleted(function ($contact) + { if ($contact->isMe() === true) { app(DeleteMeContact::class)->execute([ 'account_id' => $contact->id,