Skip to content

Commit

Permalink
feat: contact information management (monicahq/chandler#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jul 23, 2022
1 parent 359c91d commit e3f1233
Show file tree
Hide file tree
Showing 38 changed files with 1,040 additions and 165 deletions.
60 changes: 44 additions & 16 deletions app/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function handle()
$this->addTemplate();
$this->addTemplatePageContactInformation();
$this->addTemplatePageFeed();
$this->addTemplatePageContact();
$this->addTemplatePageSocial();
$this->addTemplatePageLifeEvents();
$this->addTemplatePageInformation();
Expand Down Expand Up @@ -287,6 +288,49 @@ private function addTemplatePageFeed(): void
]);
}

private function addTemplatePageContact(): void
{
$template = (new CreateTemplatePage())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'template_id' => $this->template->id,
'name' => trans('app.default_template_page_contact'),
'can_be_deleted' => true,
]);

// Addresses
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'name' => trans('app.module_addresses'),
'type' => Module::TYPE_ADDRESSES,
'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' => $template->id,
'module_id' => $module->id,
]);

// Contact information
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'name' => trans('app.module_contact_information'),
'type' => Module::TYPE_CONTACT_INFORMATION,
'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' => $template->id,
'module_id' => $module->id,
]);
}

private function addTemplatePageSocial(): void
{
$templatePageSocial = (new CreateTemplatePage())->execute([
Expand Down Expand Up @@ -383,22 +427,6 @@ private function addTemplatePageInformation(): void
'can_be_deleted' => true,
]);

// Addresses
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'name' => trans('app.module_addresses'),
'type' => Module::TYPE_ADDRESSES,
'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' => $templatePageInformation->id,
'module_id' => $module->id,
]);

// Notes
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
Expand Down
23 changes: 23 additions & 0 deletions app/Models/ContactInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -42,4 +43,26 @@ public function contactInformationType(): BelongsTo
{
return $this->belongsTo(ContactInformationType::class, 'type_id');
}

/**
* Get the content of the contact information.
* If the contact information type is a phone number or an email, return the
* content. If it's something else, return the contact information type's label.
*
* @return Attribute
*/
protected function name(): Attribute
{
return Attribute::make(
get: function ($value) {
$type = $this->contactInformationType;

if (! $type->can_be_deleted) {
return $this->data;
} else {
return $type->name;
}
}
);
}
}
1 change: 1 addition & 0 deletions app/Models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Module extends Model
public const TYPE_GOALS = 'goals';
public const TYPE_ADDRESSES = 'addresses';
public const TYPE_GROUPS = 'groups';
public const TYPE_CONTACT_INFORMATION = 'contact_information';

/**
* The attributes that are mass assignable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Contact;
use App\Models\Vault;
use App\Services\BaseService;
use Carbon\Carbon;

class CopyContactToAnotherVault extends BaseService implements ServiceInterface
{
Expand Down Expand Up @@ -58,6 +59,7 @@ public function execute(array $data): Contact
$this->data = $data;
$this->validate();
$this->copy();
$this->updateLastEditedDate();
$this->log();

return $this->newContact;
Expand Down Expand Up @@ -89,6 +91,12 @@ private function copy(): void
$this->newContact->save();
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
7 changes: 7 additions & 0 deletions domains/Contact/ManageContact/Services/CreateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function execute(array $data): Contact
$this->validate();
$this->createContact();
$this->generateAvatar();
$this->updateLastEditedDate();
$this->log();

return $this->contact;
Expand Down Expand Up @@ -124,6 +125,12 @@ private function generateAvatar(): void
$this->contact->save();
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Contact;
use App\Models\Vault;
use App\Services\BaseService;
use Carbon\Carbon;

class MoveContactToAnotherVault extends BaseService implements ServiceInterface
{
Expand Down Expand Up @@ -57,6 +58,7 @@ public function execute(array $data): Contact
$this->data = $data;
$this->validate();
$this->move();
$this->updateLastEditedDate();
$this->log();

return $this->contact;
Expand Down Expand Up @@ -85,6 +87,12 @@ private function move(): void
$this->contact->save();
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Services\BaseService;
use Carbon\Carbon;

class ToggleArchiveContact extends BaseService implements ServiceInterface
{
Expand Down Expand Up @@ -55,6 +56,7 @@ public function execute(array $data): Contact
$this->contact->listed = ! $this->contact->listed;
$this->contact->save();

$this->updateLastEditedDate();
$this->createFeedItem();

return $this->contact;
Expand All @@ -65,6 +67,12 @@ private function validate(): void
$this->validateRules($this->data);
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function createFeedItem(): void
{
ContactFeedItem::create([
Expand Down
7 changes: 7 additions & 0 deletions domains/Contact/ManageContact/Services/UpdateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function execute(array $data): Contact
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();

$this->updateLastEditedDate();
$this->log();
$this->createFeedItem();

Expand All @@ -105,6 +106,12 @@ private function validate(): void
}
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function execute(array $data): Contact

$this->validate();
$this->update();
$this->updateLastEditedDate();
$this->log();

return $this->contact;
Expand All @@ -78,6 +79,12 @@ private function update(): void
$this->contact->save();
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Contact\ManageContactAddresses\Web\ViewHelpers\ModuleContactAddressesViewHelper;
use App\Contact\ManageContactFeed\Web\ViewHelpers\ModuleFeedViewHelper;
use App\Contact\ManageContactImportantDates\Web\ViewHelpers\ModuleImportantDatesViewHelper;
use App\Contact\ManageContactInformation\Web\ViewHelpers\ModuleContactInformationViewHelper;
use App\Contact\ManageContactName\Web\ViewHelpers\ModuleContactNameViewHelper;
use App\Contact\ManageGoals\Web\ViewHelpers\ModuleGoalsViewHelper;
use App\Contact\ManageGroups\Web\ViewHelpers\GroupsViewHelper;
Expand Down Expand Up @@ -232,6 +233,10 @@ public static function modules(TemplatePage $page, Contact $contact, User $user)
$data = ModuleGroupsViewHelper::data($contact);
}

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

$modulesCollection->push([
'id' => $module->id,
'type' => $module->type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\ContactImportantDate;
use App\Models\ContactImportantDateType;
use App\Services\BaseService;
use Carbon\Carbon;

class CreateContactImportantDate extends BaseService implements ServiceInterface
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public function execute(array $data): ContactImportantDate
'year' => $this->valueOrNull($data, 'year'),
]);

$this->updateLastEditedDate();
$this->log();
$this->createFeedItem();

Expand All @@ -88,6 +90,12 @@ private function validate(): void
}
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use App\Models\ContactInformation;
use App\Models\ContactInformationType;
use App\Services\BaseService;
use Carbon\Carbon;

class CreateContactInformation extends BaseService implements ServiceInterface
{
private ContactInformation $contactInformation;
private ContactInformationType $contactInformationType;
private array $data;

/**
* Get the validation rules that apply to the service.
Expand Down Expand Up @@ -54,20 +56,36 @@ public function permissions(): array
*/
public function execute(array $data): ContactInformation
{
$this->validateRules($data);
$this->data = $data;
$this->validate();
$this->create();
$this->updateLastEditedDate();
$this->log();

return $this->contactInformation;
}

$this->contactInformationType = ContactInformationType::where('account_id', $data['account_id'])
->findOrFail($data['contact_information_type_id']);
private function validate(): void
{
$this->validateRules($this->data);

$this->contactInformationType = ContactInformationType::where('account_id', $this->data['account_id'])
->findOrFail($this->data['contact_information_type_id']);
}

private function create(): void
{
$this->contactInformation = ContactInformation::create([
'contact_id' => $this->contact->id,
'type_id' => $this->contactInformationType->id,
'data' => $data['data'],
'data' => $this->data['data'],
]);
}

$this->log();

return $this->contactInformation;
private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
Expand Down
Loading

0 comments on commit e3f1233

Please sign in to comment.