Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set and clear personal description now appears in change log #4893

Merged
merged 7 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 60 additions & 42 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;
djaiss marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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,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',
Expand All @@ -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'),
]);
}
}
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
4 changes: 4 additions & 0 deletions tests/Unit/Services/Contact/Contact/UpdateContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
asbiin marked this conversation as resolved.
Show resolved Hide resolved
'contact_id' => $contact->id,
'first_name' => 'john',
'middle_name' => 'franck',
Expand Down