diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index a1e1de7d11e..6005eccdcb7 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..d8f2daac888 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, + ]); + } }