From fd5e2adaf4a248210f507ad4d945465a0c98d737 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Tue, 11 Feb 2020 19:59:55 -0500 Subject: [PATCH 1/2] feat: services to set and clear personal description --- .../Description/ClearPersonalDescription.php | 43 +++++++++++++ .../Description/SetPersonalDescription.php | 47 ++++++++++++++ .../ClearPersonalDescriptionTest.php | 60 ++++++++++++++++++ .../SetPersonalDescriptionTest.php | 61 +++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 app/Services/Contact/Description/ClearPersonalDescription.php create mode 100644 app/Services/Contact/Description/SetPersonalDescription.php create mode 100644 tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php create mode 100644 tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php diff --git a/app/Services/Contact/Description/ClearPersonalDescription.php b/app/Services/Contact/Description/ClearPersonalDescription.php new file mode 100644 index 00000000000..2e183bf9541 --- /dev/null +++ b/app/Services/Contact/Description/ClearPersonalDescription.php @@ -0,0 +1,43 @@ + 'required|integer|exists:accounts,id', + 'contact_id' => 'required|integer|exists:contacts,id', + 'author_id' => 'required|integer|exists:users,id', + ]; + } + + /** + * Clear a contact's description. + * + * @param array $data + * @return Contact + */ + public function execute(array $data): Contact + { + $this->validate($data); + + $contact = Contact::where('account_id', $data['account_id']) + ->findOrFail($data['contact_id']); + + $contact->description = null; + $contact->save(); + + return $contact; + } +} diff --git a/app/Services/Contact/Description/SetPersonalDescription.php b/app/Services/Contact/Description/SetPersonalDescription.php new file mode 100644 index 00000000000..71f70c44466 --- /dev/null +++ b/app/Services/Contact/Description/SetPersonalDescription.php @@ -0,0 +1,47 @@ + 'required|integer|exists:accounts,id', + 'contact_id' => 'required|integer|exists:contacts,id', + 'author_id' => 'required|integer|exists:users,id', + 'description' => 'required|string|max:255', + ]; + } + + /** + * Set a contact's description. + * The description should be saved as unparsed markdown content, and fetched + * as unparsed markdown content. The UI is responsible for parsing and + * displaying the proper content. + * + * @param array $data + * @return Contact + */ + public function execute(array $data): Contact + { + $this->validate($data); + + $contact = Contact::where('account_id', $data['account_id']) + ->findOrFail($data['contact_id']); + + $contact->description = $data['description']; + $contact->save(); + + return $contact; + } +} diff --git a/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php b/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php new file mode 100644 index 00000000000..4c5f1110627 --- /dev/null +++ b/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php @@ -0,0 +1,60 @@ +create([]); + $user = factory(User::class)->create([ + 'account_id' => $contact->account_id, + ]); + + $request = [ + 'account_id' => $contact->account_id, + 'author_id' => $user->id, + 'contact_id' => $contact->id, + ]; + + $michael = (new ClearPersonalDescription)->execute($request); + + $this->assertDatabaseHas('contacts', [ + 'account_id' => $contact->account_id, + 'id' => $contact->id, + 'description' => null, + ]); + + $this->assertInstanceOf( + Contact::class, + $contact + ); + } + + /** @test */ + public function it_fails_if_wrong_parameters_are_given(): void + { + $request = [ + 'first_name' => 'Dwight', + ]; + + $this->expectException(ValidationException::class); + (new ClearPersonalDescription)->execute($request); + } +} diff --git a/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php b/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php new file mode 100644 index 00000000000..4d8af61349a --- /dev/null +++ b/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php @@ -0,0 +1,61 @@ +create([]); + $user = factory(User::class)->create([ + 'account_id' => $contact->account_id, + ]); + + $request = [ + 'account_id' => $contact->account_id, + 'author_id' => $user->id, + 'contact_id' => $contact->id, + 'description' => 'This is just great', + ]; + + $contact = (new SetPersonalDescription)->execute($request); + + $this->assertDatabaseHas('contacts', [ + 'account_id' => $contact->account_id, + 'id' => $contact->id, + 'description' => 'This is just great', + ]); + + $this->assertInstanceOf( + Contact::class, + $contact + ); + } + + /** @test */ + public function it_fails_if_wrong_parameters_are_given(): void + { + $request = [ + 'first_name' => 'Dwight', + ]; + + $this->expectException(ValidationException::class); + (new SetPersonalDescription)->execute($request); + } +} From 8ca294baa1e3eb8bcb43647485cf0cc90e50b35d Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Wed, 12 Feb 2020 01:00:33 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- .../Contact/Description/ClearPersonalDescription.php | 3 +-- .../Contact/Description/SetPersonalDescription.php | 3 +-- .../Contact/Description/ClearPersonalDescriptionTest.php | 7 ++----- .../Contact/Description/SetPersonalDescriptionTest.php | 7 ++----- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/Services/Contact/Description/ClearPersonalDescription.php b/app/Services/Contact/Description/ClearPersonalDescription.php index 2e183bf9541..c7b2311c5ab 100644 --- a/app/Services/Contact/Description/ClearPersonalDescription.php +++ b/app/Services/Contact/Description/ClearPersonalDescription.php @@ -2,9 +2,8 @@ namespace App\Services\Contact\Description; -use App\Models\Contact\Contact; -use Carbon\Carbon; use App\Services\BaseService; +use App\Models\Contact\Contact; class ClearPersonalDescription extends BaseService { diff --git a/app/Services/Contact/Description/SetPersonalDescription.php b/app/Services/Contact/Description/SetPersonalDescription.php index 71f70c44466..9bbe3883bc9 100644 --- a/app/Services/Contact/Description/SetPersonalDescription.php +++ b/app/Services/Contact/Description/SetPersonalDescription.php @@ -2,9 +2,8 @@ namespace App\Services\Contact\Description; -use App\Models\Contact\Contact; -use Carbon\Carbon; use App\Services\BaseService; +use App\Models\Contact\Contact; class SetPersonalDescription extends BaseService { diff --git a/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php b/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php index 4c5f1110627..1136cfd4e91 100644 --- a/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php +++ b/tests/Unit/Services/Contact/Description/ClearPersonalDescriptionTest.php @@ -3,15 +3,12 @@ namespace Tests\Unit\Services\Contact\Description; use Tests\TestCase; -use App\Jobs\LogAccountAudit; -use App\Jobs\LogEmployeeAudit; -use App\Models\Company\Employee; -use App\Models\Contact\Contact; use App\Models\User\User; -use App\Services\Contact\Description\ClearPersonalDescription; +use App\Models\Contact\Contact; use Illuminate\Support\Facades\Queue; use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Testing\DatabaseTransactions; +use App\Services\Contact\Description\ClearPersonalDescription; class ClearPersonalDescriptionTest extends TestCase { diff --git a/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php b/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php index 4d8af61349a..8d782fdda33 100644 --- a/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php +++ b/tests/Unit/Services/Contact/Description/SetPersonalDescriptionTest.php @@ -3,15 +3,12 @@ namespace Tests\Unit\Services\Contact\Description; use Tests\TestCase; -use App\Jobs\LogAccountAudit; -use App\Jobs\LogEmployeeAudit; -use App\Models\Company\Employee; -use App\Models\Contact\Contact; use App\Models\User\User; -use App\Services\Contact\Description\SetPersonalDescription; +use App\Models\Contact\Contact; use Illuminate\Support\Facades\Queue; use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Testing\DatabaseTransactions; +use App\Services\Contact\Description\SetPersonalDescription; class SetPersonalDescriptionTest extends TestCase {