Skip to content

Commit

Permalink
feat: set and clear personal description now appears in change log (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Feb 20, 2021
1 parent 7235e02 commit 686a0a1
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 77 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Contacts/RelationshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
123 changes: 63 additions & 60 deletions app/Services/Contact/Contact/UpdateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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'),
]);
}
}
32 changes: 17 additions & 15 deletions app/Services/Contact/Description/SetPersonalDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class SetPersonalDescription extends BaseService
{
private array $data;
private Contact $contact;

/**
* Get the validation rules that apply to the service.
*
Expand All @@ -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',
];
}

Expand All @@ -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,
]),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/ViewHelpers/ContactHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Services/Contact/Contact/UpdateContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 686a0a1

Please sign in to comment.