Skip to content

Commit

Permalink
feat: skip home if employee has 1 company (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Jun 5, 2021
1 parent d8e71d5 commit 552c460
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
38 changes: 31 additions & 7 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,48 @@ class HomeController extends Controller
/**
* Display the user home page.
*
* @return Response
* @return Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function index(): Response
public function index()
{
return $this->companies(true);
}

/**
* Display the list of companies.
*
* @return Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function list()
{
return $this->companies(false);
}

/**
* Display the list of companies.
*
* @param bool $redirect
* @return Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
private function companies(bool $redirect)
{
Cache::forget('cachedCompanyObject_'.Auth::user()->id);
Cache::forget('cachedEmployeeObject_'.Auth::user()->id);

$employees = Auth::user()->employees()->with('company')->notLocked()->get();
$companiesCollection = collect([]);

foreach ($employees as $employee) {
$companiesCollection->push([
if ($redirect && $employees->count() === 1) {
return redirect()->route('welcome', ['company' => $employees->first()->company_id]);
}

$companiesCollection = $employees->map(function ($employee) {
return [
'company_name' => $employee->company->name,
'company_id' => $employee->company_id,
'number_of_employees' => $employee->company->employees()->count(),
'joined_at' => $employee->created_at,
]);
}
];
});

return Inertia::render('Home/Index', [
'employees' => $companiesCollection,
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Shared/UserMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ svg {
<path d="M8 5a1 1 0 100 2h5.586l-1.293 1.293a1 1 0 001.414 1.414l3-3a1 1 0 000-1.414l-3-3a1 1 0 10-1.414 1.414L13.586 5H8zM12 15a1 1 0 100-2H6.414l1.293-1.293a1 1 0 10-1.414-1.414l-3 3a1 1 0 000 1.414l3 3a1 1 0 001.414-1.414L6.414 15H12z" />
</svg>
</span>
<inertia-link :href="'/home'" class="no-color no-underline" data-cy="switch-company-button">
<inertia-link :href="route('companies')" class="no-color no-underline" data-cy="switch-company-button">
{{ $t('app.header_switch_company') }}
</inertia-link>
</li>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('home', 'HomeController@index')->name('home');
Route::get('companies', 'HomeController@list')->name('companies');
Route::post('search/employees', 'HeaderSearchController@employees');
Route::post('search/teams', 'HeaderSearchController@teams');

Expand Down

0 comments on commit 552c460

Please sign in to comment.