-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to archive contact (monicahq/chandler#140)
- Loading branch information
Showing
12 changed files
with
284 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
domains/Contact/ManageContact/Services/ToggleArchiveContact.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManageContact\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Services\BaseService; | ||
|
||
class ToggleArchiveContact extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|integer|exists:accounts,id', | ||
'vault_id' => 'required|integer|exists:vaults,id', | ||
'author_id' => 'required|integer|exists:users,id', | ||
'contact_id' => 'required|integer|exists:contacts,id', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
* | ||
* @return array | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'contact_must_belong_to_vault', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
/** | ||
* Toggle the archive state of a contact. | ||
* | ||
* @param array $data | ||
* @return Contact | ||
*/ | ||
public function execute(array $data): Contact | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
|
||
$this->contact->listed = ! $this->contact->listed; | ||
$this->contact->save(); | ||
|
||
$this->createFeedItem(); | ||
|
||
return $this->contact; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
} | ||
|
||
private function createFeedItem(): void | ||
{ | ||
ContactFeedItem::create([ | ||
'author_id' => $this->author->id, | ||
'contact_id' => $this->contact->id, | ||
'action' => $this->contact->refresh()->listed ? ContactFeedItem::ACTION_UNARCHIVED_CONTACT : ContactFeedItem::ACTION_ARCHIVED_CONTACT, | ||
]); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
domains/Contact/ManageContact/Web/Controllers/ContactArchiveController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManageContact\Web\Controllers; | ||
|
||
use App\Contact\ManageContact\Services\ToggleArchiveContact; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ContactArchiveController extends Controller | ||
{ | ||
public function update(Request $request, int $vaultId, int $contactId) | ||
{ | ||
$data = [ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'vault_id' => $vaultId, | ||
'contact_id' => $contactId, | ||
]; | ||
|
||
(new ToggleArchiveContact())->execute($data); | ||
|
||
return response()->json([ | ||
'data' => route('contact.show', [ | ||
'vault' => $vaultId, | ||
'contact' => $contactId, | ||
]), | ||
], 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/Unit/Domains/Contact/ManageContact/Services/ToggleArchiveContactTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Domains\Contact\ManageContact\Services; | ||
|
||
use App\Contact\ManageContact\Services\ToggleArchiveContact; | ||
use App\Exceptions\NotEnoughPermissionException; | ||
use App\Models\Account; | ||
use App\Models\Contact; | ||
use App\Models\User; | ||
use App\Models\Vault; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Validation\ValidationException; | ||
use Tests\TestCase; | ||
|
||
class ToggleArchiveContactTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_toggles_a_contact(): void | ||
{ | ||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); | ||
$contact = Contact::factory()->create(['vault_id' => $vault->id]); | ||
$this->executeService($regis, $regis->account, $vault, $contact); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_wrong_parameters_are_given(): void | ||
{ | ||
$request = [ | ||
'title' => 'Ross', | ||
]; | ||
|
||
$this->expectException(ValidationException::class); | ||
(new ToggleArchiveContact())->execute($request); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_user_doesnt_belong_to_account(): void | ||
{ | ||
$this->expectException(ModelNotFoundException::class); | ||
|
||
$regis = $this->createUser(); | ||
$account = $this->createAccount(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_MANAGE, $vault); | ||
$contact = Contact::factory()->create(['vault_id' => $vault->id]); | ||
|
||
$this->executeService($regis, $account, $vault, $contact); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_contact_doesnt_belong_to_vault(): void | ||
{ | ||
$this->expectException(ModelNotFoundException::class); | ||
|
||
$regis = $this->createUser(); | ||
$vault = Vault::factory()->create(); | ||
$contact = Contact::factory()->create(['vault_id' => $vault->id]); | ||
$otherVault = Vault::factory()->create(); | ||
|
||
$this->executeService($regis, $regis->account, $otherVault, $contact); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_user_doesnt_have_right_permission_in_vault(): void | ||
{ | ||
$this->expectException(NotEnoughPermissionException::class); | ||
|
||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); | ||
$contact = Contact::factory()->create(['vault_id' => $vault->id]); | ||
|
||
$this->executeService($regis, $regis->account, $vault, $contact); | ||
} | ||
|
||
private function executeService(User $author, Account $account, Vault $vault, Contact $contact): void | ||
{ | ||
$request = [ | ||
'account_id' => $account->id, | ||
'vault_id' => $vault->id, | ||
'author_id' => $author->id, | ||
'contact_id' => $contact->id, | ||
]; | ||
|
||
$contact = (new ToggleArchiveContact())->execute($request); | ||
|
||
$this->assertDatabaseHas('contacts', [ | ||
'id' => $contact->id, | ||
'vault_id' => $vault->id, | ||
'listed' => false, | ||
]); | ||
|
||
$contact = (new ToggleArchiveContact())->execute($request); | ||
|
||
$this->assertDatabaseHas('contacts', [ | ||
'id' => $contact->id, | ||
'vault_id' => $vault->id, | ||
'listed' => true, | ||
]); | ||
} | ||
} |
Oops, something went wrong.