Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete "me" contact when deleting the contact #6531

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +168,21 @@ class Contact extends Model
*/
protected $nameOrder = 'firstname_lastname';

/**
* Boot all the event observers for the model.
*/
protected static function booted()
{
static::deleted(function ($contact) {
ccrims0n marked this conversation as resolved.
Show resolved Hide resolved
if ($contact->isMe() === true) {
app(DeleteMeContact::class)->execute([
'account_id' => $contact->id,
'user_id' => auth()->user()->id,
]);
}
});
}

/**
* Get the user associated with the contact.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/MeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}