-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: family summary module (monicahq/chandler#125)
- Loading branch information
Showing
12 changed files
with
258 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
domains/Contact/ManageRelationships/Web/ViewHelpers/ModuleFamilySummaryViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManageRelationships\Web\ViewHelpers; | ||
|
||
use App\Helpers\AvatarHelper; | ||
use App\Models\Contact; | ||
use App\Models\RelationshipGroupType; | ||
use App\Models\RelationshipType; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class ModuleFamilySummaryViewHelper | ||
{ | ||
/** | ||
* Get a summary of the family of the contact: | ||
* - spouse or partner | ||
* - children. | ||
* | ||
* @param Contact $contact | ||
* @param User $user | ||
* @return array | ||
*/ | ||
public static function data(Contact $contact, User $user): array | ||
{ | ||
$loveRelationshipType = $contact->vault->account->relationshipGroupTypes() | ||
->where('type', RelationshipGroupType::TYPE_LOVE) | ||
->first(); | ||
|
||
$loveRelationships = $loveRelationshipType->types() | ||
->where('type', RelationshipType::TYPE_LOVE) | ||
->get(); | ||
|
||
$loveRelationshipsCollection = self::getRelations($loveRelationships, $contact, $user); | ||
|
||
$familyRelationshipType = $contact->vault->account->relationshipGroupTypes() | ||
->where('type', RelationshipGroupType::TYPE_FAMILY) | ||
->first(); | ||
|
||
$familyRelationships = $familyRelationshipType->types() | ||
->where('type', RelationshipType::TYPE_CHILD) | ||
->get(); | ||
|
||
$familyRelationshipsCollection = self::getRelations($familyRelationships, $contact, $user); | ||
|
||
return [ | ||
'family_relationships' => $familyRelationshipsCollection, | ||
'love_relationships' => $loveRelationshipsCollection, | ||
]; | ||
} | ||
|
||
private static function getRelations(EloquentCollection $collection, Contact $contact, User $user): Collection | ||
{ | ||
$relationshipsCollection = collect(); | ||
$counter = 0; | ||
foreach ($collection as $relationshipType) { | ||
$relations = DB::table('relationships') | ||
->join('contacts', 'relationships.contact_id', '=', 'contacts.id') | ||
->join('relationship_types', 'relationships.relationship_type_id', '=', 'relationship_types.id') | ||
->select('relationships.id as main_id', 'relationship_types.id', 'relationships.contact_id', 'relationships.related_contact_id') | ||
->where('relationships.relationship_type_id', $relationshipType->id) | ||
->where(function ($query) use ($contact) { | ||
$query->where('relationships.contact_id', $contact->id) | ||
->orWhere('relationships.related_contact_id', $contact->id); | ||
}) | ||
->get(); | ||
|
||
if ($relations->count() == 0) { | ||
continue; | ||
} | ||
|
||
foreach ($relations as $relation) { | ||
if ($relation->contact_id == $contact->id) { | ||
$relatedContact = Contact::find($relation->related_contact_id); | ||
} else { | ||
continue; | ||
} | ||
|
||
$relationshipsCollection->push([ | ||
'id' => $counter, | ||
'contact' => self::getContact($relatedContact, $user), | ||
]); | ||
} | ||
$counter++; | ||
} | ||
|
||
return $relationshipsCollection; | ||
} | ||
|
||
private static function getContact(Contact $contact, User $user): array | ||
{ | ||
return [ | ||
'id' => $contact->id, | ||
'name' => $contact->name, | ||
'avatar' => AvatarHelper::getSVG($contact), | ||
'age' => $contact->age, | ||
'url' => [ | ||
'show' => $contact->listed ? route('contact.show', [ | ||
'vault' => $contact->vault->id, | ||
'contact' => $contact->id, | ||
]) : null, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.