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: add notion of _me_ as a contact #2899

Merged
merged 15 commits into from
Aug 17, 2019
Prev Previous commit
Next Next commit
Add Api function
  • Loading branch information
asbiin committed Aug 13, 2019
commit d257ff2a10a2c7f1635525a824d733d36fa9adff
21 changes: 21 additions & 0 deletions app/Http/Controllers/Api/ApiContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Collection;
use Illuminate\Database\QueryException;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\SetMeContact;
use App\Services\Contact\Contact\CreateContact;
use App\Services\Contact\Contact\UpdateContact;
use App\Services\Contact\Contact\DestroyContact;
Expand Down Expand Up @@ -175,4 +176,24 @@ private function applyWithParameter($contacts, string $parameter = null)

return ContactResource::collection($contacts);
}

/**
* Set a contact as 'me'.
*
* @param Request $request
* @param int $contactId
*
* @return string
*/
public function setMe(Request $request, $contactId)
{
$data = [
'contact_id' => $contactId,
'account_id' => auth()->user()->account->id,
'user_id' => auth()->user()->id,
];
app(SetMeContact::class)->execute($data);

return 'true';
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/Account/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helpers\DateHelper;
use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;
use App\Http\Resources\Settings\Currency\Currency as CurrencyResource;

class User extends Resource
Expand All @@ -22,6 +23,7 @@ public function toArray($request)
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
'me_contact' => new ContactShortResource($this->me),
'timezone' => $this->timezone,
'currency' => new CurrencyResource($this->currency),
'locale' => $this->locale,
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function toArray($request)
'is_partial' => (bool) $this->is_partial,
'is_active' => (bool) $this->is_active,
'is_dead' => (bool) $this->is_dead,
'is_me' => $this->id == auth()->user()->me_contact_id,
'last_called' => $this->when(! $this->is_partial, $this->getLastCalled()),
'last_activity_together' => $this->when(! $this->is_partial, $this->getLastActivityDate()),
'stay_in_touch_frequency' => $this->when(! $this->is_partial, $this->stay_in_touch_frequency),
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/Contact/ContactShort.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function toArray($request)
'gender_type' => is_null($this->gender) ? null : $this->gender->type,
'is_partial' => (bool) $this->is_partial,
'is_dead' => (bool) $this->is_dead,
'is_me' => $this->id == auth()->user()->me_contact_id,
'information' => [
'birthdate' => [
'is_age_based' => (is_null($this->birthdate) ? null : (bool) $this->birthdate->is_age_based),
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/Contact/ContactWithContactFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function toArray($request)
'is_partial' => (bool) $this->is_partial,
'is_active' => (bool) $this->is_active,
'is_dead' => (bool) $this->is_dead,
'is_me' => $this->id == auth()->user()->me_contact_id,
'last_called' => $this->when(! $this->is_partial, $this->getLastCalled()),
'last_activity_together' => $this->when(! $this->is_partial, $this->getLastActivityDate()),
'stay_in_touch_frequency' => $this->when(! $this->is_partial, $this->stay_in_touch_frequency),
Expand Down
10 changes: 10 additions & 0 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ public function account()
return $this->belongsTo(Account::class);
}

/**
* Get the contact record associated with the 'me' contact.
*
* @return BelongsTo
*/
public function me()
{
return $this->belongsTo(Contact::class, 'me_contact_id');
}

/**
* Get the term records associated with the user.
*
Expand Down
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function register()
\App\Services\Contact\Call\UpdateCall::class => \App\Services\Contact\Call\UpdateCall::class,
\App\Services\Contact\Contact\CreateContact::class => \App\Services\Contact\Contact\CreateContact::class,
\App\Services\Contact\Contact\DestroyContact::class => \App\Services\Contact\Contact\DestroyContact::class,
\App\Services\Contact\Contact\SetMeContact::class => \App\Services\Contact\Contact\SetMeContact::class,
\App\Services\Contact\Contact\UpdateBirthdayInformation::class => \App\Services\Contact\Contact\UpdateBirthdayInformation::class,
\App\Services\Contact\Contact\UpdateContact::class => \App\Services\Contact\Contact\UpdateContact::class,
\App\Services\Contact\Contact\UpdateDeceasedInformation::class => \App\Services\Contact\Contact\UpdateDeceasedInformation::class,
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// Contacts
Route::apiResource('contacts', 'ApiContactController');
Route::post('/contacts/{contact}/setMe', 'ApiContactController@setMe');

// Genders
Route::apiResource('genders', 'Account\\ApiGenderController');
Expand Down
41 changes: 41 additions & 0 deletions tests/Api/Contact/ApiContactControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ApiContactControllerTest extends ApiTestCase
'is_starred',
'is_partial',
'is_dead',
'is_me',
'last_called',
'last_activity_together',
'stay_in_touch_frequency',
Expand Down Expand Up @@ -1352,4 +1353,44 @@ public function test_it_deletes_a_contact()
'id' => $contact->id,
]);
}

public function test_it_sets_me_contact()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);

$response = $this->json('POST', '/api/contacts/'.$contact->id.'/setMe');

$response->assertStatus(200);

$this->assertDatabaseHas('users', [
'account_id' => $user->account_id,
'me_contact_id' => $contact->id,
]);
}

public function test_it_gets_me_contact()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$user->me_contact_id = $contact->id;
$user->save();

$response = $this->json('GET', '/api/contacts/'.$contact->id);

$response->assertOk();

$response->assertJsonStructure([
'data' => $this->jsonStructureContactShort,
]);

$response->assertJsonFragment([
'id' => $contact->id,
'is_me' => true,
]);
}
}