Skip to content

Commit

Permalink
fix: fix contact resources (#3583)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Mar 22, 2020
1 parent 091538c commit bb515c7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 178 deletions.
28 changes: 3 additions & 25 deletions app/Http/Controllers/Api/ApiContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Helpers\SearchHelper;
use App\Models\Contact\Contact;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Database\QueryException;
use App\Services\Contact\Contact\SetMeContact;
Expand All @@ -21,7 +20,6 @@
use App\Http\Resources\Contact\Contact as ContactResource;
use App\Services\Contact\Contact\UpdateContactIntroduction;
use App\Services\Contact\Contact\UpdateContactFoodPreferences;
use App\Http\Resources\Contact\ContactWithContactFields as ContactWithContactFieldsResource;

class ApiContactController extends ApiController
{
Expand Down Expand Up @@ -59,9 +57,7 @@ public function index(Request $request)
return $this->respondInvalidQuery();
}

$collection = $this->applyWithParameter($contacts, $this->getWithParameter());

return $collection->additional([
return ContactResource::collection($contacts)->additional([
'meta' => [
'query' => $needle,
],
Expand All @@ -78,15 +74,15 @@ public function index(Request $request)
return $this->respondInvalidQuery();
}

return $this->applyWithParameter($contacts, $this->getWithParameter());
return ContactResource::collection($contacts);
}

/**
* Get the detail of a given contact.
*
* @param Request $request
* @param int $id
* @return ContactResource|JsonResponse|ContactWithContactFieldsResource
* @return ContactResource|JsonResponse
*/
public function show(Request $request, int $id)
{
Expand All @@ -100,10 +96,6 @@ public function show(Request $request, int $id)

UpdateLastConsultedDate::dispatch($contact);

if ($this->getWithParameter() == 'contactfields') {
return new ContactWithContactFieldsResource($contact);
}

return new ContactResource($contact);
}

Expand Down Expand Up @@ -183,20 +175,6 @@ public function destroy(Request $request, $contactId)
return $this->respondObjectDeleted($contactId);
}

/**
* Apply the `?with=` parameter.
* @param Collection $contacts
* @return JsonResource
*/
private function applyWithParameter($contacts, string $parameter = null)
{
if ($parameter == 'contactfields') {
return ContactWithContactFieldsResource::collection($contacts);
}

return ContactResource::collection($contacts);
}

/**
* Set a contact as 'me'.
*
Expand Down
25 changes: 19 additions & 6 deletions app/Http/Resources/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use App\Helpers\DateHelper;
use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\Tag\Tag as TagResource;
use App\Http\Resources\Note\Note as NoteResource;
use App\Http\Resources\Address\Address as AddressResource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;
use App\Http\Resources\ContactField\ContactField as ContactFieldResource;
use App\Http\Resources\Relationship\RelationshipShort as RelationshipShortResource;

class Contact extends Resource
{
Expand All @@ -15,6 +20,11 @@ class Contact extends Resource
* @return array
*/
public function toArray($request)
{
return $this->toArrayInternal($request, $request->input('with') == 'contactfields');
}

protected function toArrayInternal($request, $withContactField)
{
return [
'id' => $this->id,
Expand All @@ -24,6 +34,7 @@ public function toArray($request)
'last_name' => $this->last_name,
'nickname' => $this->nickname,
'complete_name' => $this->name,
'initials' => $this->getInitials(),
'description' => $this->description,
'gender' => is_null($this->gender) ? null : $this->gender->name,
'gender_type' => is_null($this->gender) ? null : $this->gender->type,
Expand All @@ -40,19 +51,19 @@ public function toArray($request)
'relationships' => $this->when(! $this->is_partial, [
'love' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('love')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('love')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('love')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('love'))),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('love')) ? null : RelationshipShortResource::collection($this->getRelationshipsByRelationshipTypeGroup('love'))),
],
'family' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('family')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('family')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('family')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('family'))),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('family')) ? null : RelationshipShortResource::collection($this->getRelationshipsByRelationshipTypeGroup('family'))),
],
'friend' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('friend')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('friend')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('friend')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('friend'))),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('friend')) ? null : RelationshipShortResource::collection($this->getRelationshipsByRelationshipTypeGroup('friend'))),
],
'work' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('work')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('work')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('work')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('work'))),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('work')) ? null : RelationshipShortResource::collection($this->getRelationshipsByRelationshipTypeGroup('work'))),
],
]),
'dates' => [
Expand Down Expand Up @@ -87,8 +98,8 @@ public function toArray($request)
'first_met_through_contact' => new ContactShortResource($this->getIntroducer()),
]),
],
'addresses' => $this->when(! $this->is_partial, $this->getAddressesForAPI()),
'tags' => $this->when(! $this->is_partial, $this->getTagsForAPI()),
'addresses' => $this->when(! $this->is_partial, AddressResource::collection($this->addresses)),
'tags' => $this->when(! $this->is_partial, TagResource::collection($this->tags)),
'statistics' => $this->when(! $this->is_partial, [
'number_of_calls' => $this->calls->count(),
'number_of_notes' => $this->notes->count(),
Expand All @@ -98,6 +109,8 @@ public function toArray($request)
'number_of_gifts' => $this->gifts->count(),
'number_of_debts' => $this->debts->count(),
]),
'contactFields' => $this->when($withContactField && ! $this->is_partial, ContactFieldResource::collection($this->contactFields)),
'notes' => $this->when($withContactField && ! $this->is_partial, NoteResource::collection($this->notes()->latest()->limit(3)->get())),
'url' => $this->when(! $this->is_partial, route('api.contact', $this->id)),
'account' => [
'id' => $this->account_id,
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/Contact/ContactShort.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function toArray($request)
'initials' => $this->getInitials(),
'gender' => is_null($this->gender) ? null : $this->gender->name,
'gender_type' => is_null($this->gender) ? null : $this->gender->type,
'is_starred' => (bool) $this->is_starred,
'is_partial' => (bool) $this->is_partial,
'is_active' => (bool) $this->is_active,
'is_dead' => (bool) $this->is_dead,
'is_me' => $this->isMe(),
'information' => [
Expand Down
94 changes: 1 addition & 93 deletions app/Http/Resources/Contact/ContactWithContactFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace App\Http\Resources\Contact;

use App\Helpers\DateHelper;
use App\Http\Resources\Note\Note as NoteResource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;

class ContactWithContactFields extends Contact
{
/**
Expand All @@ -16,94 +12,6 @@ class ContactWithContactFields extends Contact
*/
public function toArray($request)
{
return [
'id' => $this->id,
'object' => 'contact',
'hash_id' => $this->getHashId(),
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'nickname' => $this->nickname,
'gender' => is_null($this->gender) ? null : $this->gender->name,
'gender_type' => is_null($this->gender) ? null : $this->gender->type,
'is_starred' => (bool) $this->is_starred,
'is_partial' => (bool) $this->is_partial,
'is_active' => (bool) $this->is_active,
'is_dead' => (bool) $this->is_dead,
'is_me' => $this->isMe(),
'last_called' => $this->when(! $this->is_partial, $this->last_talked_to),
'last_activity_together' => $this->when(! $this->is_partial, $this->getLastActivityDate()),
'stay_in_touch_frequency' => $this->when(! $this->is_partial, $this->stay_in_touch_frequency),
'stay_in_touch_trigger_date' => $this->when(! $this->is_partial, DateHelper::getTimestamp($this->stay_in_touch_trigger_date)),
'information' => [
'relationships' => $this->when(! $this->is_partial, [
'love' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('love')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('love')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('love')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('love'))),
],
'family' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('family')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('family')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('family')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('family'))),
],
'friend' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('friend')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('friend')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('friend')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('friend'))),
],
'work' => [
'total' => (is_null($this->getRelationshipsByRelationshipTypeGroup('work')) ? 0 : $this->getRelationshipsByRelationshipTypeGroup('work')->count()),
'contacts' => (is_null($this->getRelationshipsByRelationshipTypeGroup('work')) ? null : \App\Models\Contact\Contact::translateForAPI($this->getRelationshipsByRelationshipTypeGroup('work'))),
],
]),
'dates' => [
'birthdate' => [
'is_age_based' => (is_null($this->birthdate) ? null : (bool) $this->birthdate->is_age_based),
'is_year_unknown' => (is_null($this->birthdate) ? null : (bool) $this->birthdate->is_year_unknown),
'date' => DateHelper::getTimestamp($this->birthdate),
],
'deceased_date' => [
'is_age_based' => (is_null($this->deceasedDate) ? null : (bool) $this->deceasedDate->is_age_based),
'is_year_unknown' => (is_null($this->deceasedDate) ? null : (bool) $this->deceasedDate->is_year_unknown),
'date' => DateHelper::getTimestamp($this->deceasedDate),
],
],
'career' => $this->when(! $this->is_partial, [
'job' => $this->job,
'company' => $this->company,
]),
'avatar' => $this->when(! $this->is_partial, [
'url' => $this->getAvatarUrl(),
'source' => $this->avatar_source,
'default_avatar_color' => $this->default_avatar_color,
]),
'food_preferences' => $this->when(! $this->is_partial, $this->food_preferences),
'how_you_met' => $this->when(! $this->is_partial, [
'general_information' => $this->first_met_additional_info,
'first_met_date' => [
'is_age_based' => (is_null($this->firstMetDate) ? null : (bool) $this->firstMetDate->is_age_based),
'is_year_unknown' => (is_null($this->firstMetDate) ? null : (bool) $this->firstMetDate->is_year_unknown),
'date' => DateHelper::getTimestamp($this->firstMetDate),
],
'first_met_through_contact' => new ContactShortResource($this->getIntroducer()),
]),
],
'addresses' => $this->when(! $this->is_partial, $this->getAddressesForAPI()),
'tags' => $this->when(! $this->is_partial, $this->getTagsForAPI()),
'statistics' => $this->when(! $this->is_partial, [
'number_of_calls' => $this->calls->count(),
'number_of_notes' => $this->notes->count(),
'number_of_activities' => $this->activities->count(),
'number_of_reminders' => $this->reminders->count(),
'number_of_tasks' => $this->tasks->count(),
'number_of_gifts' => $this->gifts->count(),
'number_of_debts' => $this->debts->count(),
]),
'contactFields' => $this->when(! $this->is_partial, $this->getContactFieldsForAPI()),
'notes' => $this->when(! $this->is_partial, NoteResource::collection($this->notes()->latest()->limit(3)->get())),
'url' => $this->when(! $this->is_partial, route('api.contact', $this->id)),
'account' => [
'id' => $this->account_id,
],
'created_at' => DateHelper::getTimestamp($this->created_at),
'updated_at' => DateHelper::getTimestamp($this->updated_at),
];
return $this->toArrayInternal($request, true);
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/Relationship/RelationshipShort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Resources\Relationship;

use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;

class RelationshipShort extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'relationship' => [
'id' => $this->id,
'name' => $this->relationshipType->name,
],
'contact' => new ContactShortResource($this->ofContact),
];
}
}
54 changes: 0 additions & 54 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
use App\Models\Relationship\Relationship;
use Illuminate\Database\Eloquent\Builder;
use App\Models\ModelBindingHasher as Model;
use App\Http\Resources\Tag\Tag as TagResource;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Http\Resources\Address\Address as AddressResource;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;
use App\Http\Resources\ContactField\ContactField as ContactFieldResource;

/**
* @property \App\Models\Instance\SpecialDate $birthdate
Expand Down Expand Up @@ -797,32 +793,6 @@ public function getRelationshipsByRelationshipTypeGroup(string $type)
});
}

/**
* Translate a collection of relationships into a collection that the API can
* parse.
*
* @param Collection $collection
* @return Collection
*/
public static function translateForAPI(Collection $collection)
{
$contacts = collect();

foreach ($collection as $relationship) {
$contact = $relationship->ofContact;

$contacts->push([
'relationship' => [
'id' => $relationship->id,
'name' => $relationship->relationshipType->name,
],
'contact' => new ContactShortResource($contact),
]);
}

return $contacts;
}

/**
* Set the default avatar color for this object.
*
Expand Down Expand Up @@ -1065,30 +1035,6 @@ public function getTagsAsString()
})->join(',');
}

/**
* Get the list of tags for this contact.
*/
public function getTagsForAPI()
{
return TagResource::collection($this->tags);
}

/**
* Get the list of addresses for this contact.
*/
public function getAddressesForAPI()
{
return AddressResource::collection($this->addresses);
}

/**
* Get the list of contact fields for this contact.
*/
public function getContactFieldsForAPI()
{
return ContactFieldResource::collection($this->contactFields);
}

/**
* Is this contact owed money?
* @return bool
Expand Down

0 comments on commit bb515c7

Please sign in to comment.