From 2f2b5223b4ae07fb7accf7588445aa36514f2ea6 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 19 Feb 2021 16:28:17 -0500 Subject: [PATCH 1/7] feat: set and clear personal description now appears in contact change log --- .../Controllers/Api/ApiContactController.php | 25 ++-- .../Contacts/ContactAuditLogController.php | 4 +- .../Contacts/RelationshipsController.php | 19 +-- app/Http/Controllers/ContactsController.php | 35 +++--- .../Contact/Contact/UpdateContact.php | 108 ++++++++++-------- .../Description/SetPersonalDescription.php | 36 +++--- app/ViewHelpers/ContactHelper.php | 2 +- .../Contact/Contact/UpdateContactTest.php | 8 +- 8 files changed, 132 insertions(+), 105 deletions(-) diff --git a/app/Http/Controllers/Api/ApiContactController.php b/app/Http/Controllers/Api/ApiContactController.php index 11a56811340..043422971fe 100644 --- a/app/Http/Controllers/Api/ApiContactController.php +++ b/app/Http/Controllers/Api/ApiContactController.php @@ -2,24 +2,24 @@ namespace App\Http\Controllers\Api; -use Illuminate\Http\Request; use App\Helpers\SearchHelper; -use App\Models\Contact\Contact; -use Illuminate\Http\JsonResponse; +use App\Http\Resources\Contact\Contact as ContactResource; use App\Jobs\UpdateLastConsultedDate; -use Illuminate\Database\QueryException; -use App\Services\Contact\Contact\SetMeContact; -use Illuminate\Validation\ValidationException; +use App\Models\Contact\Contact; use App\Services\Contact\Contact\CreateContact; -use App\Services\Contact\Contact\UpdateContact; -use App\Services\Contact\Contact\DestroyContact; -use Illuminate\Http\Resources\Json\JsonResource; use App\Services\Contact\Contact\DeleteMeContact; +use App\Services\Contact\Contact\DestroyContact; +use App\Services\Contact\Contact\SetMeContact; +use App\Services\Contact\Contact\UpdateContact; +use App\Services\Contact\Contact\UpdateContactFoodPreferences; +use App\Services\Contact\Contact\UpdateContactIntroduction; use App\Services\Contact\Contact\UpdateWorkInformation; use Illuminate\Database\Eloquent\ModelNotFoundException; -use App\Http\Resources\Contact\Contact as ContactResource; -use App\Services\Contact\Contact\UpdateContactIntroduction; -use App\Services\Contact\Contact\UpdateContactFoodPreferences; +use Illuminate\Database\QueryException; +use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; +use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Validation\ValidationException; class ApiContactController extends ApiController { @@ -146,6 +146,7 @@ public function update(Request $request, $contactId) [ 'contact_id' => $contactId, 'account_id' => auth()->user()->account_id, + 'author_id' => auth()->user()->id, ] ); } catch (ModelNotFoundException $e) { diff --git a/app/Http/Controllers/Contacts/ContactAuditLogController.php b/app/Http/Controllers/Contacts/ContactAuditLogController.php index d2593412d1d..9afa9ab5527 100644 --- a/app/Http/Controllers/Contacts/ContactAuditLogController.php +++ b/app/Http/Controllers/Contacts/ContactAuditLogController.php @@ -3,8 +3,8 @@ namespace App\Http\Controllers\Contacts; use App\Helpers\DateHelper; -use App\Models\Contact\Contact; use App\Http\Controllers\Controller; +use App\Models\Contact\Contact; class ContactAuditLogController extends Controller { @@ -21,7 +21,7 @@ public function index(Contact $contact) $logsCollection = collect(); foreach ($logs as $log) { - $description = trans('app.contact_log_'.$log->action); + $description = trans('logs.contact_log_'.$log->action); $logsCollection->push([ 'author_name' => ($log->author) ? $log->author->name : $log->author_name, diff --git a/app/Http/Controllers/Contacts/RelationshipsController.php b/app/Http/Controllers/Contacts/RelationshipsController.php index f2825a4d9ce..3ec469c7e78 100644 --- a/app/Http/Controllers/Contacts/RelationshipsController.php +++ b/app/Http/Controllers/Contacts/RelationshipsController.php @@ -2,24 +2,24 @@ namespace App\Http\Controllers\Contacts; -use Illuminate\View\View; use App\Helpers\DateHelper; use App\Helpers\FormHelper; -use Illuminate\Http\Request; use App\Helpers\GenderHelper; -use App\Models\Contact\Contact; -use Illuminate\Support\Collection; use App\Http\Controllers\Controller; -use Illuminate\Support\Facades\Auth; -use Illuminate\Http\RedirectResponse; +use App\Http\Resources\Contact\ContactShort as ContactResource; +use App\Models\Contact\Contact; use App\Models\Relationship\Relationship; -use Illuminate\Support\Facades\Validator; use App\Services\Contact\Contact\CreateContact; use App\Services\Contact\Contact\UpdateContact; use App\Services\Contact\Relationship\CreateRelationship; -use App\Services\Contact\Relationship\UpdateRelationship; use App\Services\Contact\Relationship\DestroyRelationship; -use App\Http\Resources\Contact\ContactShort as ContactResource; +use App\Services\Contact\Relationship\UpdateRelationship; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Validator; +use Illuminate\View\View; class RelationshipsController extends Controller { @@ -152,6 +152,7 @@ public function update(Request $request, Contact $contact, Relationship $relatio app(UpdateContact::class)->execute($datas + [ 'contact_id' => $otherContact->id, + 'author_id' => auth()->user()->id, ]); } diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php index adcd01e8925..f509bef021f 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -2,34 +2,34 @@ namespace App\Http\Controllers; -use Illuminate\View\View; +use App\Helpers\AccountHelper; use App\Helpers\DateHelper; use App\Helpers\FormHelper; -use App\Models\Contact\Tag; -use Illuminate\Support\Str; -use Illuminate\Http\Request; use App\Helpers\GenderHelper; use App\Helpers\LocaleHelper; use App\Helpers\SearchHelper; -use App\Helpers\AccountHelper; use App\Helpers\StorageHelper; -use App\Models\Contact\Contact; -use App\Services\VCard\ExportVCard; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Auth; +use App\Http\Resources\Contact\ContactSearch as ContactResource; use App\Jobs\UpdateLastConsultedDate; -use Illuminate\Http\RedirectResponse; -use Illuminate\Contracts\View\Factory; +use App\Models\Contact\Contact; +use App\Models\Contact\Tag; use App\Models\Relationship\Relationship; -use Barryvdh\Debugbar\Facade as Debugbar; -use App\Services\User\UpdateViewPreference; -use Illuminate\Validation\ValidationException; use App\Services\Contact\Contact\CreateContact; -use App\Services\Contact\Contact\UpdateContact; use App\Services\Contact\Contact\DestroyContact; -use App\Services\Contact\Contact\UpdateWorkInformation; +use App\Services\Contact\Contact\UpdateContact; use App\Services\Contact\Contact\UpdateContactFoodPreferences; -use App\Http\Resources\Contact\ContactSearch as ContactResource; +use App\Services\Contact\Contact\UpdateWorkInformation; +use App\Services\User\UpdateViewPreference; +use App\Services\VCard\ExportVCard; +use Barryvdh\Debugbar\Facade as Debugbar; +use Illuminate\Contracts\View\Factory; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; +use Illuminate\Validation\ValidationException; +use Illuminate\View\View; class ContactsController extends Controller { @@ -401,6 +401,7 @@ public function update(Request $request, Contact $contact) $data = [ 'account_id' => auth()->user()->account_id, + 'author_id' => auth()->user()->id, 'contact_id' => $contact->id, 'first_name' => $request->input('firstname'), 'middle_name' => $request->input('middlename', null), diff --git a/app/Services/Contact/Contact/UpdateContact.php b/app/Services/Contact/Contact/UpdateContact.php index 61c5e55fc2b..69c67e69114 100644 --- a/app/Services/Contact/Contact/UpdateContact.php +++ b/app/Services/Contact/Contact/UpdateContact.php @@ -2,13 +2,18 @@ namespace App\Services\Contact\Contact; -use Illuminate\Support\Arr; -use App\Services\BaseService; -use App\Models\Contact\Contact; use App\Jobs\Avatars\GenerateDefaultAvatar; +use App\Models\Contact\Contact; +use App\Services\BaseService; +use App\Services\Contact\Description\ClearPersonalDescription; +use App\Services\Contact\Description\SetPersonalDescription; +use Illuminate\Support\Arr; class UpdateContact extends BaseService { + private array $data; + private Contact $contact; + /** * Get the validation rules that apply to the service. * @@ -18,6 +23,7 @@ public function rules() { return [ 'account_id' => 'required|integer|exists:accounts,id', + 'author_id' => 'required|integer|exists:users,id', 'contact_id' => 'required|integer', 'first_name' => 'required|string|max:255', 'middle_name' => 'nullable|string|max:255', @@ -50,37 +56,34 @@ public function rules() */ public function execute(array $data): Contact { - $this->validate($data); + $this->data = $data; + $this->validate($this->data); - /** @var Contact */ - $contact = Contact::where('account_id', $data['account_id']) + /* @var Contact */ + $this->contact = Contact::where('account_id', $data['account_id']) ->findOrFail($data['contact_id']); - $this->updateGeneralInformation($data, $contact); - - $this->updateBirthDayInformation($data, $contact); + $this->updateGeneralInformation(); - $this->updateDeceasedInformation($data, $contact); + $this->updateBirthDayInformation(); - // we query the DB again to fill the object with all the new properties - $contact->refresh(); + $this->updateDeceasedInformation(); - return $contact; + return $this->contact->refresh(); } /** * Update general information. * - * @param array $data - * @param Contact $contact * @return void */ - private function updateGeneralInformation(array $data, Contact $contact) + private function updateGeneralInformation(): void { // filter out the data that shall not be updated here $dataOnly = Arr::except( - $data, + $this->data, [ + 'author_id', 'is_birthdate_known', 'birthdate_day', 'birthdate_month', @@ -94,60 +97,75 @@ private function updateGeneralInformation(array $data, Contact $contact) 'deceased_date_month', 'deceased_date_year', 'deceased_date_add_reminder', + 'description', ] ); - $oldName = $contact->name; + $oldName = $this->contact->name; + $oldDescription = $this->contact->description; - $contact->update($dataOnly); + $this->contact->update($dataOnly); // only update the avatar if the name has changed - if ($oldName != $contact->name) { - GenerateDefaultAvatar::dispatch($contact); + if ($oldName != $this->contact->name) { + GenerateDefaultAvatar::dispatch($this->contact); + } + + if ($oldDescription != $this->data['description'] && $this->data['description']) { + app(SetPersonalDescription::class)->execute([ + 'account_id' => $this->data['account_id'], + 'contact_id' => $this->data['contact_id'], + 'author_id' => $this->data['author_id'], + 'description' => $this->data['description'], + ]); + } + + if ($oldDescription != $this->data['description'] && ! $this->data['description']) { + app(ClearPersonalDescription::class)->execute([ + 'account_id' => $this->data['account_id'], + 'contact_id' => $this->data['contact_id'], + 'author_id' => $this->data['author_id'], + ]); } } /** * Update the information about the birthday. * - * @param array $data - * @param Contact $contact * @return void */ - private function updateBirthDayInformation(array $data, Contact $contact) + private function updateBirthDayInformation() { app(UpdateBirthdayInformation::class)->execute([ - 'account_id' => $data['account_id'], - 'contact_id' => $contact->id, - 'is_date_known' => $data['is_birthdate_known'], - 'day' => $this->nullOrvalue($data, 'birthdate_day'), - 'month' => $this->nullOrvalue($data, 'birthdate_month'), - 'year' => $this->nullOrvalue($data, 'birthdate_year'), - 'is_age_based' => $this->nullOrvalue($data, 'birthdate_is_age_based'), - 'age' => $this->nullOrvalue($data, 'birthdate_age'), - 'add_reminder' => $this->nullOrvalue($data, 'birthdate_add_reminder'), - 'is_deceased' => $data['is_deceased'], + 'account_id' => $this->data['account_id'], + 'contact_id' => $this->contact->id, + 'is_date_known' => $this->data['is_birthdate_known'], + 'day' => $this->nullOrvalue($this->data, 'birthdate_day'), + 'month' => $this->nullOrvalue($this->data, 'birthdate_month'), + 'year' => $this->nullOrvalue($this->data, 'birthdate_year'), + 'is_age_based' => $this->nullOrvalue($this->data, 'birthdate_is_age_based'), + 'age' => $this->nullOrvalue($this->data, 'birthdate_age'), + 'add_reminder' => $this->nullOrvalue($this->data, 'birthdate_add_reminder'), + 'is_deceased' => $this->data['is_deceased'], ]); } /** * Update the information about the date of death. * - * @param array $data - * @param Contact $contact * @return void */ - private function updateDeceasedInformation(array $data, Contact $contact) + private function updateDeceasedInformation() { app(UpdateDeceasedInformation::class)->execute([ - 'account_id' => $data['account_id'], - 'contact_id' => $contact->id, - 'is_deceased' => $data['is_deceased'], - 'is_date_known' => $data['is_deceased_date_known'], - 'day' => $this->nullOrvalue($data, 'deceased_date_day'), - 'month' => $this->nullOrvalue($data, 'deceased_date_month'), - 'year' => $this->nullOrvalue($data, 'deceased_date_year'), - 'add_reminder' => $this->nullOrvalue($data, 'deceased_date_add_reminder'), + 'account_id' => $this->data['account_id'], + 'contact_id' => $this->contact->id, + 'is_deceased' => $this->data['is_deceased'], + 'is_date_known' => $this->data['is_deceased_date_known'], + 'day' => $this->nullOrvalue($this->data, 'deceased_date_day'), + 'month' => $this->nullOrvalue($this->data, 'deceased_date_month'), + 'year' => $this->nullOrvalue($this->data, 'deceased_date_year'), + 'add_reminder' => $this->nullOrvalue($this->data, 'deceased_date_add_reminder'), ]); } } diff --git a/app/Services/Contact/Description/SetPersonalDescription.php b/app/Services/Contact/Description/SetPersonalDescription.php index 10001f0cf16..176003e5eae 100644 --- a/app/Services/Contact/Description/SetPersonalDescription.php +++ b/app/Services/Contact/Description/SetPersonalDescription.php @@ -2,14 +2,17 @@ namespace App\Services\Contact\Description; +use App\Jobs\AuditLog\LogAccountAudit; +use App\Models\Contact\Contact; use App\Models\User\User; use App\Services\BaseService; use function Safe\json_encode; -use App\Models\Contact\Contact; -use App\Jobs\AuditLog\LogAccountAudit; class SetPersonalDescription extends BaseService { + private array $data; + private Contact $contact; + /** * Get the validation rules that apply to the service. * @@ -21,7 +24,7 @@ public function rules(): array 'account_id' => 'required|integer|exists:accounts,id', 'contact_id' => 'required|integer|exists:contacts,id', 'author_id' => 'required|integer|exists:users,id', - 'description' => 'required|string|max:255', + 'description' => 'nullable|string|max:255', ]; } @@ -36,42 +39,41 @@ public function rules(): array */ public function execute(array $data): Contact { - $this->validate($data); + $this->data = $data; + $this->validate($this->data); - /** @var Contact */ - $contact = Contact::where('account_id', $data['account_id']) + /* @var Contact */ + $this->contact = Contact::where('account_id', $data['account_id']) ->findOrFail($data['contact_id']); - $contact->description = $data['description']; - $contact->save(); + $this->contact->description = $data['description']; + $this->contact->save(); - $this->log($data, $contact); + $this->log(); - return $contact; + return $this->contact->refresh(); } /** * Add an audit log. * - * @param array $data - * @param Contact $contact * @return void */ - private function log(array $data, Contact $contact): void + private function log(): void { - $author = User::find($data['author_id']); + $author = User::find($this->data['author_id']); LogAccountAudit::dispatch([ 'action' => 'contact_description_updated', 'account_id' => $author->account_id, - 'about_contact_id' => $contact->id, + 'about_contact_id' => $this->contact->id, 'author_id' => $author->id, 'author_name' => $author->name, 'audited_at' => now(), 'should_appear_on_dashboard' => true, 'objects' => json_encode([ - 'contact_name' => $contact->name, - 'contact_id' => $contact->id, + 'contact_name' => $this->contact->name, + 'contact_id' => $this->contact->id, ]), ]); } diff --git a/app/ViewHelpers/ContactHelper.php b/app/ViewHelpers/ContactHelper.php index 833a08aca04..7ed3470335e 100644 --- a/app/ViewHelpers/ContactHelper.php +++ b/app/ViewHelpers/ContactHelper.php @@ -27,7 +27,7 @@ public static function getListOfAuditLogs($logs): Collection $logsCollection = collect(); foreach ($logs as $log) { - $description = trans('app.contact_log_'.$log->action); + $description = trans('logs.contact_log_'.$log->action); $logsCollection->push([ 'author_name' => ($log->author) ? $log->author->name : $log->author_name, diff --git a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php index a04f3f6e174..667f92ca4fe 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -2,11 +2,11 @@ namespace Tests\Unit\Services\Contact\Contact; -use Tests\TestCase; use App\Models\Contact\Contact; -use Illuminate\Validation\ValidationException; use App\Services\Contact\Contact\UpdateContact; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Validation\ValidationException; +use Tests\TestCase; class UpdateContactTest extends TestCase { @@ -16,9 +16,11 @@ class UpdateContactTest extends TestCase public function it_updates_a_contact() { $contact = factory(Contact::class)->create([]); + $user = factory(User::class)->create([]); $request = [ 'account_id' => $contact->account_id, + 'author_id' => $user->id, 'contact_id' => $contact->id, 'first_name' => 'john', 'middle_name' => 'franck', @@ -91,9 +93,11 @@ public function it_fails_if_wrong_parameters_are_given() public function it_throws_an_exception_if_account_doesnt_exist() { $contact = factory(Contact::class)->create([]); + $user = factory(User::class)->create([]); $request = [ 'account_id' => 11111, + 'author_id' => $user()->id, 'contact_id' => $contact->id, 'first_name' => 'john', 'middle_name' => 'franck', From 1af1b1b06bafa4802334f10875eb8fbc8e9d5c49 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 19 Feb 2021 21:28:43 +0000 Subject: [PATCH 2/7] Apply fixes from StyleCI --- .../Controllers/Api/ApiContactController.php | 24 ++++++------- .../Contacts/ContactAuditLogController.php | 2 +- .../Contacts/RelationshipsController.php | 18 +++++----- app/Http/Controllers/ContactsController.php | 34 +++++++++---------- .../Contact/Contact/UpdateContact.php | 8 ++--- .../Description/SetPersonalDescription.php | 4 +-- .../Contact/Contact/UpdateContactTest.php | 4 +-- 7 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/Http/Controllers/Api/ApiContactController.php b/app/Http/Controllers/Api/ApiContactController.php index 043422971fe..04fa66ae3df 100644 --- a/app/Http/Controllers/Api/ApiContactController.php +++ b/app/Http/Controllers/Api/ApiContactController.php @@ -2,24 +2,24 @@ namespace App\Http\Controllers\Api; +use Illuminate\Http\Request; use App\Helpers\SearchHelper; -use App\Http\Resources\Contact\Contact as ContactResource; -use App\Jobs\UpdateLastConsultedDate; use App\Models\Contact\Contact; -use App\Services\Contact\Contact\CreateContact; -use App\Services\Contact\Contact\DeleteMeContact; -use App\Services\Contact\Contact\DestroyContact; +use Illuminate\Http\JsonResponse; +use App\Jobs\UpdateLastConsultedDate; +use Illuminate\Database\QueryException; use App\Services\Contact\Contact\SetMeContact; +use Illuminate\Validation\ValidationException; +use App\Services\Contact\Contact\CreateContact; use App\Services\Contact\Contact\UpdateContact; -use App\Services\Contact\Contact\UpdateContactFoodPreferences; -use App\Services\Contact\Contact\UpdateContactIntroduction; +use App\Services\Contact\Contact\DestroyContact; +use Illuminate\Http\Resources\Json\JsonResource; +use App\Services\Contact\Contact\DeleteMeContact; use App\Services\Contact\Contact\UpdateWorkInformation; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Database\QueryException; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; -use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Validation\ValidationException; +use App\Http\Resources\Contact\Contact as ContactResource; +use App\Services\Contact\Contact\UpdateContactIntroduction; +use App\Services\Contact\Contact\UpdateContactFoodPreferences; class ApiContactController extends ApiController { diff --git a/app/Http/Controllers/Contacts/ContactAuditLogController.php b/app/Http/Controllers/Contacts/ContactAuditLogController.php index 9afa9ab5527..dac371d15db 100644 --- a/app/Http/Controllers/Contacts/ContactAuditLogController.php +++ b/app/Http/Controllers/Contacts/ContactAuditLogController.php @@ -3,8 +3,8 @@ namespace App\Http\Controllers\Contacts; use App\Helpers\DateHelper; -use App\Http\Controllers\Controller; use App\Models\Contact\Contact; +use App\Http\Controllers\Controller; class ContactAuditLogController extends Controller { diff --git a/app/Http/Controllers/Contacts/RelationshipsController.php b/app/Http/Controllers/Contacts/RelationshipsController.php index 3ec469c7e78..6ee7fb7e4c5 100644 --- a/app/Http/Controllers/Contacts/RelationshipsController.php +++ b/app/Http/Controllers/Contacts/RelationshipsController.php @@ -2,24 +2,24 @@ namespace App\Http\Controllers\Contacts; +use Illuminate\View\View; use App\Helpers\DateHelper; use App\Helpers\FormHelper; +use Illuminate\Http\Request; use App\Helpers\GenderHelper; -use App\Http\Controllers\Controller; -use App\Http\Resources\Contact\ContactShort as ContactResource; use App\Models\Contact\Contact; +use Illuminate\Support\Collection; +use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; use App\Models\Relationship\Relationship; +use Illuminate\Support\Facades\Validator; use App\Services\Contact\Contact\CreateContact; use App\Services\Contact\Contact\UpdateContact; use App\Services\Contact\Relationship\CreateRelationship; -use App\Services\Contact\Relationship\DestroyRelationship; use App\Services\Contact\Relationship\UpdateRelationship; -use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Validator; -use Illuminate\View\View; +use App\Services\Contact\Relationship\DestroyRelationship; +use App\Http\Resources\Contact\ContactShort as ContactResource; class RelationshipsController extends Controller { diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php index f509bef021f..4ca3ca5cdfd 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -2,34 +2,34 @@ namespace App\Http\Controllers; -use App\Helpers\AccountHelper; +use Illuminate\View\View; use App\Helpers\DateHelper; use App\Helpers\FormHelper; +use App\Models\Contact\Tag; +use Illuminate\Support\Str; +use Illuminate\Http\Request; use App\Helpers\GenderHelper; use App\Helpers\LocaleHelper; use App\Helpers\SearchHelper; +use App\Helpers\AccountHelper; use App\Helpers\StorageHelper; -use App\Http\Resources\Contact\ContactSearch as ContactResource; -use App\Jobs\UpdateLastConsultedDate; use App\Models\Contact\Contact; -use App\Models\Contact\Tag; +use App\Services\VCard\ExportVCard; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Auth; +use App\Jobs\UpdateLastConsultedDate; +use Illuminate\Http\RedirectResponse; +use Illuminate\Contracts\View\Factory; use App\Models\Relationship\Relationship; +use Barryvdh\Debugbar\Facade as Debugbar; +use App\Services\User\UpdateViewPreference; +use Illuminate\Validation\ValidationException; use App\Services\Contact\Contact\CreateContact; -use App\Services\Contact\Contact\DestroyContact; use App\Services\Contact\Contact\UpdateContact; -use App\Services\Contact\Contact\UpdateContactFoodPreferences; +use App\Services\Contact\Contact\DestroyContact; use App\Services\Contact\Contact\UpdateWorkInformation; -use App\Services\User\UpdateViewPreference; -use App\Services\VCard\ExportVCard; -use Barryvdh\Debugbar\Facade as Debugbar; -use Illuminate\Contracts\View\Factory; -use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Str; -use Illuminate\Validation\ValidationException; -use Illuminate\View\View; +use App\Services\Contact\Contact\UpdateContactFoodPreferences; +use App\Http\Resources\Contact\ContactSearch as ContactResource; class ContactsController extends Controller { diff --git a/app/Services/Contact/Contact/UpdateContact.php b/app/Services/Contact/Contact/UpdateContact.php index 69c67e69114..43f58a7ef13 100644 --- a/app/Services/Contact/Contact/UpdateContact.php +++ b/app/Services/Contact/Contact/UpdateContact.php @@ -2,12 +2,12 @@ namespace App\Services\Contact\Contact; -use App\Jobs\Avatars\GenerateDefaultAvatar; -use App\Models\Contact\Contact; +use Illuminate\Support\Arr; use App\Services\BaseService; -use App\Services\Contact\Description\ClearPersonalDescription; +use App\Models\Contact\Contact; +use App\Jobs\Avatars\GenerateDefaultAvatar; use App\Services\Contact\Description\SetPersonalDescription; -use Illuminate\Support\Arr; +use App\Services\Contact\Description\ClearPersonalDescription; class UpdateContact extends BaseService { diff --git a/app/Services/Contact/Description/SetPersonalDescription.php b/app/Services/Contact/Description/SetPersonalDescription.php index 176003e5eae..9b8ee5d7ffd 100644 --- a/app/Services/Contact/Description/SetPersonalDescription.php +++ b/app/Services/Contact/Description/SetPersonalDescription.php @@ -2,11 +2,11 @@ namespace App\Services\Contact\Description; -use App\Jobs\AuditLog\LogAccountAudit; -use App\Models\Contact\Contact; use App\Models\User\User; use App\Services\BaseService; use function Safe\json_encode; +use App\Models\Contact\Contact; +use App\Jobs\AuditLog\LogAccountAudit; class SetPersonalDescription extends BaseService { diff --git a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php index 667f92ca4fe..bac9a31893d 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -2,11 +2,11 @@ namespace Tests\Unit\Services\Contact\Contact; +use Tests\TestCase; use App\Models\Contact\Contact; +use Illuminate\Validation\ValidationException; use App\Services\Contact\Contact\UpdateContact; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Validation\ValidationException; -use Tests\TestCase; class UpdateContactTest extends TestCase { From 2501b2e748a704ff7494c2591206dee5df9ca0f1 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 19 Feb 2021 19:52:04 -0500 Subject: [PATCH 3/7] Update UpdateContactTest.php --- tests/Unit/Services/Contact/Contact/UpdateContactTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php index bac9a31893d..e68aca370db 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -2,11 +2,12 @@ namespace Tests\Unit\Services\Contact\Contact; -use Tests\TestCase; use App\Models\Contact\Contact; -use Illuminate\Validation\ValidationException; +use App\Models\User\User; use App\Services\Contact\Contact\UpdateContact; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Validation\ValidationException; +use Tests\TestCase; class UpdateContactTest extends TestCase { From 57cf58b1f538950cc3f0ab62e56681f7396e5342 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sat, 20 Feb 2021 00:52:16 +0000 Subject: [PATCH 4/7] Apply fixes from StyleCI --- tests/Unit/Services/Contact/Contact/UpdateContactTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php index e68aca370db..4ae10717f8d 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -2,12 +2,12 @@ namespace Tests\Unit\Services\Contact\Contact; -use App\Models\Contact\Contact; +use Tests\TestCase; use App\Models\User\User; +use App\Models\Contact\Contact; +use Illuminate\Validation\ValidationException; use App\Services\Contact\Contact\UpdateContact; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Validation\ValidationException; -use Tests\TestCase; class UpdateContactTest extends TestCase { From b5672f8fb2c6e77a4637bc3d5bf54a2a516ba9d3 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sat, 20 Feb 2021 09:17:58 +0100 Subject: [PATCH 5/7] Update tests/Unit/Services/Contact/Contact/UpdateContactTest.php --- tests/Unit/Services/Contact/Contact/UpdateContactTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php index 4ae10717f8d..6a9b1be94aa 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -98,7 +98,7 @@ public function it_throws_an_exception_if_account_doesnt_exist() $request = [ 'account_id' => 11111, - 'author_id' => $user()->id, + 'author_id' => $user->id, 'contact_id' => $contact->id, 'first_name' => 'john', 'middle_name' => 'franck', From db33e3afbc40b5bf048fe3e56d945626bd7f9727 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sat, 20 Feb 2021 17:54:05 -0500 Subject: [PATCH 6/7] fix --- .../Contact/Contact/UpdateContact.php | 55 +++++++------------ tests/Feature/RelationshipTest.php | 6 +- 2 files changed, 23 insertions(+), 38 deletions(-) diff --git a/app/Services/Contact/Contact/UpdateContact.php b/app/Services/Contact/Contact/UpdateContact.php index 43f58a7ef13..2ef5e4da9b3 100644 --- a/app/Services/Contact/Contact/UpdateContact.php +++ b/app/Services/Contact/Contact/UpdateContact.php @@ -2,12 +2,12 @@ namespace App\Services\Contact\Contact; -use Illuminate\Support\Arr; -use App\Services\BaseService; -use App\Models\Contact\Contact; use App\Jobs\Avatars\GenerateDefaultAvatar; -use App\Services\Contact\Description\SetPersonalDescription; +use App\Models\Contact\Contact; +use App\Services\BaseService; use App\Services\Contact\Description\ClearPersonalDescription; +use App\Services\Contact\Description\SetPersonalDescription; +use Illuminate\Support\Arr; class UpdateContact extends BaseService { @@ -64,19 +64,13 @@ public function execute(array $data): Contact ->findOrFail($data['contact_id']); $this->updateGeneralInformation(); - + $this->updateDescription(); $this->updateBirthDayInformation(); - $this->updateDeceasedInformation(); return $this->contact->refresh(); } - /** - * Update general information. - * - * @return void - */ private function updateGeneralInformation(): void { // filter out the data that shall not be updated here @@ -102,39 +96,35 @@ private function updateGeneralInformation(): void ); $oldName = $this->contact->name; - $oldDescription = $this->contact->description; - $this->contact->update($dataOnly); // only update the avatar if the name has changed if ($oldName != $this->contact->name) { GenerateDefaultAvatar::dispatch($this->contact); } + } - if ($oldDescription != $this->data['description'] && $this->data['description']) { - app(SetPersonalDescription::class)->execute([ - 'account_id' => $this->data['account_id'], - 'contact_id' => $this->data['contact_id'], - 'author_id' => $this->data['author_id'], - 'description' => $this->data['description'], - ]); - } - - if ($oldDescription != $this->data['description'] && ! $this->data['description']) { + private function updateDescription(): void + { + if (is_null($this->nullOrValue($this->data, 'description'))) { app(ClearPersonalDescription::class)->execute([ 'account_id' => $this->data['account_id'], 'contact_id' => $this->data['contact_id'], 'author_id' => $this->data['author_id'], ]); + } else { + if ($this->contact->description != $this->data['description']) { + app(SetPersonalDescription::class)->execute([ + 'account_id' => $this->data['account_id'], + 'contact_id' => $this->data['contact_id'], + 'author_id' => $this->data['author_id'], + 'description' => $this->data['description'], + ]); + } } } - /** - * Update the information about the birthday. - * - * @return void - */ - private function updateBirthDayInformation() + private function updateBirthDayInformation(): void { app(UpdateBirthdayInformation::class)->execute([ 'account_id' => $this->data['account_id'], @@ -150,12 +140,7 @@ private function updateBirthDayInformation() ]); } - /** - * Update the information about the date of death. - * - * @return void - */ - private function updateDeceasedInformation() + private function updateDeceasedInformation(): void { app(UpdateDeceasedInformation::class)->execute([ 'account_id' => $this->data['account_id'], diff --git a/tests/Feature/RelationshipTest.php b/tests/Feature/RelationshipTest.php index e5a5dbf0cb4..bb9b8829986 100644 --- a/tests/Feature/RelationshipTest.php +++ b/tests/Feature/RelationshipTest.php @@ -2,13 +2,13 @@ namespace Tests\Feature; -use Tests\FeatureTestCase; -use App\Models\Contact\Gender; use App\Models\Contact\Contact; +use App\Models\Contact\Gender; use App\Models\Relationship\Relationship; -use Illuminate\Foundation\Testing\WithFaker; use App\Models\Relationship\RelationshipType; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\WithFaker; +use Tests\FeatureTestCase; class RelationshipTest extends FeatureTestCase { From b080097ac9ea956188d7b04141260faa03fb890b Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sat, 20 Feb 2021 22:54:16 +0000 Subject: [PATCH 7/7] Apply fixes from StyleCI --- app/Services/Contact/Contact/UpdateContact.php | 8 ++++---- tests/Feature/RelationshipTest.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Services/Contact/Contact/UpdateContact.php b/app/Services/Contact/Contact/UpdateContact.php index 2ef5e4da9b3..e81ac3ad872 100644 --- a/app/Services/Contact/Contact/UpdateContact.php +++ b/app/Services/Contact/Contact/UpdateContact.php @@ -2,12 +2,12 @@ namespace App\Services\Contact\Contact; -use App\Jobs\Avatars\GenerateDefaultAvatar; -use App\Models\Contact\Contact; +use Illuminate\Support\Arr; use App\Services\BaseService; -use App\Services\Contact\Description\ClearPersonalDescription; +use App\Models\Contact\Contact; +use App\Jobs\Avatars\GenerateDefaultAvatar; use App\Services\Contact\Description\SetPersonalDescription; -use Illuminate\Support\Arr; +use App\Services\Contact\Description\ClearPersonalDescription; class UpdateContact extends BaseService { diff --git a/tests/Feature/RelationshipTest.php b/tests/Feature/RelationshipTest.php index bb9b8829986..e5a5dbf0cb4 100644 --- a/tests/Feature/RelationshipTest.php +++ b/tests/Feature/RelationshipTest.php @@ -2,13 +2,13 @@ namespace Tests\Feature; -use App\Models\Contact\Contact; +use Tests\FeatureTestCase; use App\Models\Contact\Gender; +use App\Models\Contact\Contact; use App\Models\Relationship\Relationship; +use Illuminate\Foundation\Testing\WithFaker; use App\Models\Relationship\RelationshipType; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Foundation\Testing\WithFaker; -use Tests\FeatureTestCase; class RelationshipTest extends FeatureTestCase {