Skip to content

Commit

Permalink
feat: companies tab (monicahq/chandler#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 29, 2022
1 parent b3e6a3a commit a09614e
Show file tree
Hide file tree
Showing 26 changed files with 524 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Domains\Contact\ManageJobInformation\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Services\BaseService;
use Carbon\Carbon;

class ResetJobInformation extends BaseService implements ServiceInterface
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'vault_id' => 'required|integer|exists:vaults,id',
'author_id' => 'required|integer|exists:users,id',
'contact_id' => 'required|integer|exists:contacts,id',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
'vault_must_belong_to_account',
'author_must_be_vault_editor',
'contact_must_belong_to_vault',
];
}

/**
* Reset job information for the given contact.
*
* @param array $data
* @return Contact
*/
public function execute(array $data): Contact
{
$this->validateRules($data);

$this->contact->company_id = null;
$this->contact->job_position = null;
$this->contact->save();

$this->updateLastEditedDate();

ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_JOB_INFORMATION_UPDATED,
]);

return $this->contact;
}

private function updateLastEditedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Domains\Contact\ManageJobInformation\Web\Controllers;

use App\Domains\Contact\ManageJobInformation\Services\ResetJobInformation;
use App\Domains\Contact\ManageJobInformation\Services\UpdateJobInformation;
use App\Domains\Contact\ManageJobInformation\Web\ViewHelpers\ModuleCompanyViewHelper;
use App\Domains\Vault\ManageCompanies\Services\CreateCompany;
Expand All @@ -28,9 +29,10 @@ public function index(Request $request, int $vaultId, int $contactId): JsonRespo

public function update(Request $request, int $vaultId, int $contactId)
{
$company = 0;
$companyId = 0;
if ($request->input('company_id')) {
$company = Company::findOrFail($request->input('company_id'));
$companyId = $company->id;
}

if ($request->input('company_name')) {
Expand All @@ -43,14 +45,15 @@ public function update(Request $request, int $vaultId, int $contactId)
];

$company = (new CreateCompany())->execute($data);
$companyId = $company->id;
}

(new UpdateJobInformation())->execute([
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'contact_id' => $contactId,
'company_id' => $company->id,
'company_id' => $companyId,
'job_position' => $request->input('job_position'),
]);

Expand All @@ -60,4 +63,20 @@ public function update(Request $request, int $vaultId, int $contactId)
'data' => ModuleCompanyViewHelper::data($contact),
], 200);
}

public function destroy(Request $request, int $vaultId, int $contactId)
{
(new ResetJobInformation())->execute([
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'contact_id' => $contactId,
]);

$contact = Contact::findOrFail($contactId);

return response()->json([
'data' => ModuleCompanyViewHelper::data($contact),
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static function data(Contact $contact): array
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
'destroy' => route('contact.job_information.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Domains\Vault\ManageCompanies\Web\Controllers;

use App\Domains\Vault\ManageCompanies\Web\ViewHelpers\CompanyIndexViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Vault;
use Illuminate\Http\Request;
use Inertia\Inertia;

class VaultCompanyController extends Controller
{
public function index(Request $request, int $vaultId)
{
$vault = Vault::findOrFail($vaultId);

return Inertia::render('Vault/Companies/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => CompanyIndexViewHelper::data($vault),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Domains\Vault\ManageCompanies\Web\ViewHelpers;

use App\Models\Company;
use App\Models\Contact;
use App\Models\Vault;

class CompanyIndexViewHelper
{
public static function data(Vault $vault): array
{
$collection = $vault->companies()->orderBy('name', 'asc')
->with('contacts')
->get()
->map(function ($company) use ($vault) {
return self::dto($vault, $company);
});

return [
'companies' => $collection,
];
}

public static function dto(Vault $vault, Company $company): array
{
return [
'id' => $company->id,
'name' => $company->name,
'contacts' => $company->contacts()
->get()
->map(fn (Contact $contact) => [
'id' => $contact->id,
'avatar' => $contact->avatar,
'url' => [
'show' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
]),
'url' => [
'show' => route('vault.companies.show', [
'vault' => $vault->id,
'company' => $company->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function rules(): array
'show_tasks_tab' => 'required|boolean',
'show_files_tab' => 'required|boolean',
'show_journal_tab' => 'required|boolean',
'show_companies_tab' => 'required|boolean',
];
}

Expand Down Expand Up @@ -54,6 +55,7 @@ public function execute(array $data): Vault
$this->vault->show_tasks_tab = $data['show_tasks_tab'];
$this->vault->show_files_tab = $data['show_files_tab'];
$this->vault->show_journal_tab = $data['show_journal_tab'];
$this->vault->show_companies_tab = $data['show_companies_tab'];
$this->vault->save();

return $this->vault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function layoutData(Vault $vault = null): array
'show_tasks_tab' => $vault->show_tasks_tab,
'show_files_tab' => $vault->show_files_tab,
'show_journal_tab' => $vault->show_journal_tab,
'show_companies_tab' => $vault->show_companies_tab,
],
'url' => [
'dashboard' => route('vault.show', [
Expand All @@ -50,6 +51,9 @@ public static function layoutData(Vault $vault = null): array
'groups' => route('group.index', [
'vault' => $vault->id,
]),
'companies' => route('vault.companies.index', [
'vault' => $vault->id,
]),
'tasks' => route('vault.tasks.index', [
'vault' => $vault->id,
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function update(Request $request, int $vaultId)
'show_tasks_tab' => $request->boolean('show_tasks_tab'),
'show_files_tab' => $request->boolean('show_files_tab'),
'show_journal_tab' => $request->boolean('show_journal_tab'),
'show_companies_tab' => $request->boolean('show_companies_tab'),
];

(new UpdateVaultTabVisibility())->execute($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static function data(Vault $vault): array
'show_tasks_tab' => $vault->show_tasks_tab,
'show_files_tab' => $vault->show_files_tab,
'show_journal_tab' => $vault->show_journal_tab,
'show_companies_tab' => $vault->show_companies_tab,
],
'url' => [
'template_update' => route('vault.settings.template.update', [
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Company extends Model
{
Expand Down Expand Up @@ -41,4 +42,14 @@ public function vault(): BelongsTo
{
return $this->belongsTo(Vault::class);
}

/**
* Get the contacts associated with the company.
*
* @return HasMany
*/
public function contacts(): HasMany
{
return $this->hasMany(Contact::class);
}
}
2 changes: 2 additions & 0 deletions app/Models/Vault.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Vault extends Model
'show_tasks_tab',
'show_files_tab',
'show_journal_tab',
'show_companies_tab',
];

/**
Expand All @@ -58,6 +59,7 @@ class Vault extends Model
'show_tasks_tab' => 'boolean',
'show_files_tab' => 'boolean',
'show_journal_tab' => 'boolean',
'show_companies_tab' => 'boolean',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function up()
$table->boolean('show_tasks_tab')->default(true);
$table->boolean('show_files_tab')->default(true);
$table->boolean('show_journal_tab')->default(true);
$table->boolean('show_companies_tab')->default(true);
$table->timestamps();
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->foreign('default_template_id')->references('id')->on('templates')->onDelete('set null');
Expand Down
2 changes: 2 additions & 0 deletions lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'layout_menu_reports' => 'Reports',
'layout_menu_contacts' => 'Contacts',
'layout_menu_groups' => 'Groups',
'layout_menu_companies' => 'Companies',
'layout_menu_tasks' => 'Tasks',
'layout_menu_gift_center' => 'Gifts',
'layout_menu_loans' => 'Loans & debts',
Expand All @@ -24,6 +25,7 @@
'breadcrumb_dashboard_index' => 'Dashboard',
'breadcrumb_dashboard_reminders' => 'All the planned reminders',
'breadcrumb_group_index' => 'Groups',
'breadcrumb_companies_index' => 'Companies',
'breadcrumb_contact_index' => 'Contacts',
'breadcrumb_contact_show' => 'Profile of :name',
'breadcrumb_contact_create' => 'Create a contact',
Expand Down
Loading

0 comments on commit a09614e

Please sign in to comment.