diff --git a/app/Http/Controllers/Api/ApiContactController.php b/app/Http/Controllers/Api/ApiContactController.php index 11a56811340..04fa66ae3df 100644 --- a/app/Http/Controllers/Api/ApiContactController.php +++ b/app/Http/Controllers/Api/ApiContactController.php @@ -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..dac371d15db 100644 --- a/app/Http/Controllers/Contacts/ContactAuditLogController.php +++ b/app/Http/Controllers/Contacts/ContactAuditLogController.php @@ -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..6ee7fb7e4c5 100644 --- a/app/Http/Controllers/Contacts/RelationshipsController.php +++ b/app/Http/Controllers/Contacts/RelationshipsController.php @@ -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..4ca3ca5cdfd 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -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..e81ac3ad872 100644 --- a/app/Services/Contact/Contact/UpdateContact.php +++ b/app/Services/Contact/Contact/UpdateContact.php @@ -6,9 +6,14 @@ use App\Services\BaseService; use App\Models\Contact\Contact; use App\Jobs\Avatars\GenerateDefaultAvatar; +use App\Services\Contact\Description\SetPersonalDescription; +use App\Services\Contact\Description\ClearPersonalDescription; 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,28 @@ 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->updateDeceasedInformation($data, $contact); + $this->updateGeneralInformation(); + $this->updateDescription(); + $this->updateBirthDayInformation(); + $this->updateDeceasedInformation(); - // we query the DB again to fill the object with all the new properties - $contact->refresh(); - - 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 +91,66 @@ private function updateGeneralInformation(array $data, Contact $contact) 'deceased_date_month', 'deceased_date_year', 'deceased_date_add_reminder', + 'description', ] ); - $oldName = $contact->name; - - $contact->update($dataOnly); + $oldName = $this->contact->name; + $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); } } - /** - * Update the information about the birthday. - * - * @param array $data - * @param Contact $contact - * @return void - */ - private function updateBirthDayInformation(array $data, Contact $contact) + 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'], + ]); + } + } + } + + private function updateBirthDayInformation(): void { 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(): void { 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..9b8ee5d7ffd 100644 --- a/app/Services/Contact/Description/SetPersonalDescription.php +++ b/app/Services/Contact/Description/SetPersonalDescription.php @@ -10,6 +10,9 @@ 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..6a9b1be94aa 100644 --- a/tests/Unit/Services/Contact/Contact/UpdateContactTest.php +++ b/tests/Unit/Services/Contact/Contact/UpdateContactTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit\Services\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; @@ -16,9 +17,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 +94,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',