Skip to content

Commit

Permalink
feat: family summary module (monicahq/chandler#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jun 8, 2022
1 parent a97ec06 commit c7d358f
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 5 deletions.
51 changes: 51 additions & 0 deletions app/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\LifeEventType;
use App\Models\Module;
use App\Models\RelationshipGroupType;
use App\Models\RelationshipType;
use App\Models\Template;
use App\Models\TemplatePage;
use App\Models\User;
Expand Down Expand Up @@ -175,6 +176,22 @@ private function addTemplatePageContactInformation(): void
'module_id' => $module->id,
]);

// family summary
$module = (new CreateModule)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'name' => trans('app.module_family_summary'),
'type' => Module::TYPE_FAMILY_SUMMARY,
'can_be_deleted' => false,
]);
(new AssociateModuleToTemplatePage)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'template_id' => $this->template->id,
'template_page_id' => $templatePageContact->id,
'module_id' => $module->id,
]);

// important dates
$module = (new CreateModule)->execute([
'account_id' => $this->user->account_id,
Expand Down Expand Up @@ -513,31 +530,43 @@ private function addRelationshipTypes(): void
'name' => trans('account.relationship_type_partner'),
'name_reverse_relationship' => trans('account.relationship_type_partner'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => false,
'type' => RelationshipType::TYPE_LOVE,
],
[
'name' => trans('account.relationship_type_spouse'),
'name_reverse_relationship' => trans('account.relationship_type_spouse'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => false,
'type' => RelationshipType::TYPE_LOVE,
],
[
'name' => trans('account.relationship_type_date'),
'name_reverse_relationship' => trans('account.relationship_type_date'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_lover'),
'name_reverse_relationship' => trans('account.relationship_type_lover'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_inlovewith'),
'name_reverse_relationship' => trans('account.relationship_type_lovedby'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_ex'),
'name_reverse_relationship' => trans('account.relationship_type_ex'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
]);

Expand All @@ -555,31 +584,43 @@ private function addRelationshipTypes(): void
'name' => trans('account.relationship_type_parent'),
'name_reverse_relationship' => trans('account.relationship_type_child'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => false,
'type' => RelationshipType::TYPE_CHILD,
],
[
'name' => trans('account.relationship_type_sibling'),
'name_reverse_relationship' => trans('account.relationship_type_sibling'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_grandparent'),
'name_reverse_relationship' => trans('account.relationship_type_grandchild'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_uncle'),
'name_reverse_relationship' => trans('account.relationship_type_nephew'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_cousin'),
'name_reverse_relationship' => trans('account.relationship_type_cousin'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_godfather'),
'name_reverse_relationship' => trans('account.relationship_type_godson'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
]);

Expand All @@ -596,11 +637,15 @@ private function addRelationshipTypes(): void
'name' => trans('account.relationship_type_friend'),
'name_reverse_relationship' => trans('account.relationship_type_friend'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_bestfriend'),
'name_reverse_relationship' => trans('account.relationship_type_bestfriend'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
]);

Expand All @@ -617,16 +662,22 @@ private function addRelationshipTypes(): void
'name' => trans('account.relationship_type_colleague'),
'name_reverse_relationship' => trans('account.relationship_type_colleague'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_subordinate'),
'name_reverse_relationship' => trans('account.relationship_type_boss'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
[
'name' => trans('account.relationship_type_mentor'),
'name_reverse_relationship' => trans('account.relationship_type_protege'),
'relationship_group_type_id' => $group->id,
'can_be_deleted' => true,
'type' => null,
],
]);
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Module extends Model
const TYPE_NOTES = 'notes';
const TYPE_CONTACT_NAMES = 'contact_names';
const TYPE_AVATAR = 'avatar';
const TYPE_FAMILY_SUMMARY = 'family_summary';
const TYPE_COMPANY = 'company';
const TYPE_FEED = 'feed';
const TYPE_GENDER_PRONOUN = 'gender_pronoun';
Expand Down
5 changes: 3 additions & 2 deletions app/Models/RelationshipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class RelationshipType extends Model
/**
* Possible types.
*/
const TYPE_FAMILY = 'family';
const TYPE_LOVE = 'love';
const TYPE_LOVE = 'family';
const TYPE_CHILD = 'child';

/**
* The attributes that are mass assignable.
Expand All @@ -29,6 +29,7 @@ class RelationshipType extends Model
'name',
'name_reverse_relationship',
'can_be_deleted',
'type',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Contact\ManageNotes\Web\ViewHelpers\ModuleNotesViewHelper;
use App\Contact\ManagePets\Web\ViewHelpers\ModulePetsViewHelper;
use App\Contact\ManagePronouns\Web\ViewHelpers\ModuleGenderPronounViewHelper;
use App\Contact\ManageRelationships\Web\ViewHelpers\ModuleFamilySummaryViewHelper;
use App\Contact\ManageRelationships\Web\ViewHelpers\ModuleRelationshipViewHelper;
use App\Contact\ManageReminders\Web\ViewHelpers\ModuleRemindersViewHelper;
use App\Contact\ManageTasks\Web\ViewHelpers\ModuleContactTasksViewHelper;
Expand Down Expand Up @@ -147,6 +148,10 @@ private static function getContactInformation(EloquentCollection $templatePages,
$data = ModuleCompanyViewHelper::data($contact);
}

if ($module->type == Module::TYPE_FAMILY_SUMMARY) {
$data = ModuleFamilySummaryViewHelper::data($contact, $user);
}

$modulesCollection->push([
'id' => $module->id,
'type' => $module->type,
Expand Down
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,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function rules(): array
'name' => 'required|string|max:255',
'name_reverse_relationship' => 'required|string|max:255',
'can_be_deleted' => 'required|boolean',
'type' => 'nullable|string|max:255',
];
}

Expand Down Expand Up @@ -59,6 +60,7 @@ public function execute(array $data): RelationshipType
'name' => $data['name'],
'name_reverse_relationship' => $data['name_reverse_relationship'],
'can_be_deleted' => $data['can_be_deleted'],
'type' => $this->valueOrNull($data, 'type'),
]);

CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function execute(array $data): void
$this->validateRules($data);

$type = RelationshipGroupType::where('account_id', $data['account_id'])
->where('can_be_deleted', true)
->findOrFail($data['relationship_group_type_id']);

CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function execute(array $data): void
->findOrFail($data['relationship_group_type_id']);

$type = RelationshipType::where('relationship_group_type_id', $data['relationship_group_type_id'])
->where('can_be_deleted', true)
->findOrFail($data['relationship_type_id']);

CreateAuditLog::dispatch([
Expand Down
Loading

0 comments on commit c7d358f

Please sign in to comment.