Skip to content

Commit

Permalink
feat: email field on add person (#5097)
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly authored May 23, 2021
1 parent a658fcf commit 2392afc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function store(Request $request)
'middle_name' => $request->input('middle_name', null),
'last_name' => $request->input('last_name', null),
'nickname' => $request->input('nickname', null),
'email' => $request->input('email', null),
'gender_id' => $request->input('gender'),
'is_birthdate_known' => false,
'is_deceased' => false,
Expand Down
32 changes: 32 additions & 0 deletions app/Services/Contact/Contact/CreateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Jobs\AuditLog\LogAccountAudit;
use App\Models\Contact\ContactFieldType;
use App\Jobs\Avatars\GenerateDefaultAvatar;
use App\Jobs\Avatars\GetAvatarsFromInternet;
use App\Services\Contact\ContactField\CreateContactField;

class CreateContact extends BaseService
{
Expand All @@ -30,6 +32,7 @@ public function rules()
'middle_name' => 'nullable|string|max:255',
'last_name' => 'nullable|string|max:255',
'nickname' => 'nullable|string|max:255',
'email' => 'nullable|string|max:255',
'gender_id' => 'nullable|integer|exists:genders,id',
'description' => 'nullable|string|max:255',
'is_partial' => 'nullable|boolean',
Expand Down Expand Up @@ -71,6 +74,7 @@ public function execute(array $data): Contact
$data,
[
'author_id',
'email',
'is_birthdate_known',
'birthdate_day',
'birthdate_month',
Expand All @@ -93,6 +97,8 @@ public function execute(array $data): Contact

$this->updateDeceasedInformation($data, $contact);

$this->updateEmail($data, $contact);

$this->generateUUID($contact);

$this->addAvatars($contact);
Expand Down Expand Up @@ -159,6 +165,32 @@ private function updateBirthDayInformation(array $data, Contact $contact)
]);
}

/**
* Adds a contact field containing the email address.
*
* @param array $data
* @param Contact $contact
* @return void
*/
private function updateEmail(array $data, Contact $contact)
{
$contactFieldType = ContactFieldType::where([
'account_id' => $data['account_id'],
'type' => ContactFieldType::EMAIL,
])->first();

if (is_null($contactFieldType) || is_null($this->nullOrvalue($data, 'email'))) {
return;
}

$contactField = app(CreateContactField::class)->execute([
'account_id' => $data['account_id'],
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => $data['email'],
]);
}

/**
* Update the information about the date of death.
*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/people.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'people_add_firstname' => 'First name',
'people_add_middlename' => 'Middle name (Optional)',
'people_add_lastname' => 'Last name (Optional)',
'people_add_email' => 'Email (Optional)',
'people_add_nickname' => 'Nickname (Optional)',
'people_add_cta' => 'Add',
'people_save_and_add_another_cta' => 'Submit and add someone else',
Expand Down
9 changes: 9 additions & 0 deletions resources/views/people/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@
@endif
</div>

<div class="pa4-ns ph3 pv2 mb3 mb0-ns bb b--gray-monica">
<form-input
:id="'email'"
:input-type="'text'"
:required="false"
:title="'{{ trans('people.people_add_email') }}'">
</form-input>
</div>

<div class="pa4-ns ph3 pv2 mb3 mb0-ns bb b--gray-monica">
<form-select
:options="{{ $genders }}"
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Services/Contact/Contact/CreateContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\CreateContact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Expand Down Expand Up @@ -63,6 +64,46 @@ public function it_stores_a_contact()
);
}

/** @test */
public function it_stores_a_contact_with_email()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);

factory(ContactFieldType::class)->create([
'account_id' => $account->id,
'type' => 'email',
]);

$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'first_name' => 'john',
'last_name' => 'doe',
'email' => 'email@example.com',

'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];

$contact = app(CreateContact::class)->execute($request);

$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'first_name' => 'john',
]);

$this->assertDatabaseHas('contact_fields', [
'account_id' => $account->id,
'contact_id' => $contact->id,
'data' => 'email@example.com',
]);
}

/** @test */
public function it_stores_a_contact_and_triggers_an_audit_log()
{
Expand Down

0 comments on commit 2392afc

Please sign in to comment.