Skip to content

Commit

Permalink
feat: import vcard using uuid (#5533)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Sep 26, 2021
1 parent 3812232 commit 160b36e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 25 deletions.
4 changes: 4 additions & 0 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Contact extends Model
* @var array<string>
*/
protected $fillable = [
'uuid',
'first_name',
'middle_name',
'last_name',
Expand All @@ -117,6 +118,9 @@ class Contact extends Model
'created_at',
'first_met_additional_info',
'address_book_id',
'vcard',
'avatar_gravatar_url',
'avatar_source',
];

/**
Expand Down
53 changes: 34 additions & 19 deletions app/Services/Contact/Contact/CreateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services\Contact\Contact;

use Ramsey\Uuid\Uuid;
use App\Models\User\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -29,6 +30,7 @@ public function rules()
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'uuid' => 'nullable|string',
'address_book_id' => 'nullable|integer|exists:addressbooks,id',
'first_name' => 'required|string|max:255',
'middle_name' => 'nullable|string|max:255',
Expand Down Expand Up @@ -76,6 +78,30 @@ public function execute(array $data): Contact
->findOrFail($data['address_book_id']);
}

$contact = $this->create($data);

$this->updateBirthDayInformation($data, $contact);
$this->updateDeceasedInformation($data, $contact);
$this->updateEmail($data, $contact);
$this->generateUUID($contact);
$this->addAvatars($contact);

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

// we query the DB again to fill the object with all the new properties
$contact->refresh();

return $contact;
}

/**
* Create the contact.
*
* @param array $data
* @return Contact
*/
private function create(array $data): Contact
{
// filter out the data that shall not be updated here
$dataOnly = Arr::except(
$data,
Expand All @@ -98,24 +124,11 @@ public function execute(array $data): Contact
]
);

$contact = Contact::create($dataOnly);

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

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

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

$this->generateUUID($contact);

$this->addAvatars($contact);

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

// we query the DB again to fill the object with all the new properties
$contact->refresh();
if (! empty($uuid = Arr::get($data, 'uuid')) && Uuid::isValid($uuid)) {
$dataOnly['uuid'] = $uuid;
}

return $contact;
return Contact::create($dataOnly);
}

/**
Expand All @@ -126,8 +139,10 @@ public function execute(array $data): Contact
*/
private function generateUUID(Contact $contact)
{
$contact->uuid = Str::uuid()->toString();
$contact->save();
if (empty($contact->uuid)) {
$contact->uuid = Str::uuid()->toString();
$contact->save();
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions app/Services/Contact/Contact/UpdateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services\Contact\Contact;

use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use App\Services\BaseService;
use App\Helpers\AccountHelper;
Expand All @@ -27,6 +28,7 @@ public function rules()
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'contact_id' => 'required|integer',
'uuid' => 'nullable|string',
'first_name' => 'required|string|max:255',
'middle_name' => 'nullable|string|max:255',
'last_name' => 'nullable|string|max:255',
Expand Down Expand Up @@ -92,6 +94,7 @@ private function updateGeneralInformation(): void
$this->data,
[
'author_id',
'uuid',
'is_birthdate_known',
'birthdate_day',
'birthdate_month',
Expand All @@ -109,6 +112,10 @@ private function updateGeneralInformation(): void
]
);

if (! empty($uuid = Arr::get($this->data, 'uuid')) && Uuid::isValid($uuid)) {
$dataOnly['uuid'] = $uuid;
}

$oldName = $this->contact->name;
$this->contact->update($dataOnly);

Expand Down
36 changes: 30 additions & 6 deletions app/Services/VCard/ImportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ private function getExistingContact(VCard $entry, $contact_id = null)
->find($contact_id);
}

if (! $contact) {
$contact = $this->existingUuid($entry);
}

if (! $contact) {
$contact = $this->existingContactWithEmail($entry);
}
Expand Down Expand Up @@ -490,6 +494,23 @@ private function existingContactWithName(VCard $entry)
])->first();
}

/**
* Search with uuid.
*
* @param VCard $entry
* @return Contact|null
*/
private function existingUuid(VCard $entry): ?Contact
{
return ! empty($uuid = (string) $entry->UID) && Uuid::isValid($uuid)
? Contact::where([
'account_id' => $this->accountId,
'uuid' => $uuid,
'address_book_id' => $this->addressBook ? $this->addressBook->id : null,
])->first()
: null;
}

/**
* Create the Contact object matching the current entry.
*
Expand All @@ -501,7 +522,6 @@ private function importEntry(?Contact $contact, VCard $entry): Contact
{
$contact = $this->importGeneralInformation($contact, $entry);

$this->importUid($contact, $entry);
$this->importPhoto($contact, $entry);
$this->importWorkInformation($contact, $entry);
$this->importAddress($contact, $entry);
Expand Down Expand Up @@ -533,6 +553,7 @@ private function importGeneralInformation(?Contact $contact, VCard $entry): Cont
$contactData = $this->getContactData($contact);
$original = $contactData;

$contactData = $this->importUid($contactData, $entry);
$contactData = $this->importNames($contactData, $entry);
$contactData = $this->importGender($contactData, $entry);
$contactData = $this->importBirthday($contactData, $entry);
Expand All @@ -556,6 +577,7 @@ private function getContactData(?Contact $contact): array
{
$result = [
'account_id' => $contact ? $contact->account_id : $this->accountId,
'uuid' => $contact ? (string) $contact->uuid : null,
'address_book_id' => $this->addressBook ? $this->addressBook->id : null,
'first_name' => $contact ? $contact->first_name : null,
'middle_name' => $contact ? $contact->middle_name : null,
Expand Down Expand Up @@ -728,15 +750,17 @@ private function importFromFN(array $contactData, VCard $entry): array
/**
* Import uid of the contact.
*
* @param Contact $contact
* @param array $contactData
* @param VCard $entry
* @return void
* @return array
*/
private function importUid(Contact $contact, VCard $entry): void
private function importUid(array $contactData, VCard $entry): array
{
if (empty($contact->uuid) && Uuid::isValid((string) $entry->UID)) {
$contact->uuid = (string) $entry->UID;
if (! empty($uuid = (string) $entry->UID) && Uuid::isValid($uuid)) {
$contactData['uuid'] = $uuid;
}

return $contactData;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Services/VCard/ImportVCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1300,4 +1300,45 @@ public function it_imports_new_categories()
'tag_id' => $tag3->id,
]);
}

/** @test */
public function it_imports_uuid_default()
{
$account = factory(Account::class)->create();
$importVCard = new ImportVCard;
$importVCard->accountId = $account->id;

$vcard = new VCard([
'UID' => '31fdc242-c974-436e-98de-6b21624d6e34',
]);

$contact = [];

$contact = $this->invokePrivateMethod($importVCard, 'importUid', [$contact, $vcard]);

$this->assertEquals('31fdc242-c974-436e-98de-6b21624d6e34', $contact['uuid']);
}

/** @test */
public function it_imports_uuid_contact()
{
$user = factory(User::class)->create([]);
$importVCard = new ImportVCard;
$importVCard->accountId = $user->account_id;
$importVCard->userId = $user->id;

$vcard = new VCard([
'FN' => 'John Doe',
'UID' => '31fdc242-c974-436e-98de-6b21624d6e34',
]);

$contact = $this->invokePrivateMethod($importVCard, 'importEntry', [null, $vcard]);

$this->assertDatabaseHas('contacts', [
'account_id' => $user->account_id,
'id' => $contact->id,
'uuid' => '31fdc242-c974-436e-98de-6b21624d6e34',
]);
$this->assertEquals('31fdc242-c974-436e-98de-6b21624d6e34', $contact->uuid, );
}
}

0 comments on commit 160b36e

Please sign in to comment.