Skip to content

Commit

Permalink
feat: load employee statuses on demand (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Dec 15, 2020
1 parent 9ca5671 commit f7ecdad
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 34 deletions.
2 changes: 0 additions & 2 deletions app/Http/Controllers/Company/Employee/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Http\Controllers\Controller;
use App\Http\Collections\PronounCollection;
use App\Http\Collections\PositionCollection;
use App\Http\Collections\EmployeeStatusCollection;
use App\Services\Company\Employee\Manager\AssignManager;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\ViewHelpers\Employee\EmployeeShowViewHelper;
Expand Down Expand Up @@ -154,7 +153,6 @@ public function show(Request $request, int $companyId, int $employeeId)
'employeeTeams' => $employeeTeams,
'positions' => PositionCollection::prepare($company->positions()->get()),
'teams' => $teams,
'statuses' => EmployeeStatusCollection::prepare($company->employeeStatuses()->get()),
'pronouns' => PronounCollection::prepare(Pronoun::all()),
'ships' => $ships,
'skills' => $skills,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use App\Http\Controllers\Controller;
use App\Http\Collections\PronounCollection;
use App\Http\Collections\PositionCollection;
use App\Http\Collections\EmployeeStatusCollection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\ViewHelpers\Employee\EmployeeShowViewHelper;
use App\Http\ViewHelpers\Employee\EmployeePerformanceViewHelper;
Expand Down Expand Up @@ -77,7 +76,6 @@ public function show(Request $request, int $companyId, int $employeeId)
'employeeTeams' => $employeeTeams,
'positions' => PositionCollection::prepare($company->positions()->get()),
'teams' => $teams,
'statuses' => EmployeeStatusCollection::prepare($company->employeeStatuses()->get()),
'pronouns' => PronounCollection::prepare(Pronoun::all()),
'surveys' => $surveys,
]);
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/Company/Employee/EmployeeStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@
use App\Models\Company\Employee;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\ViewHelpers\Employee\EmployeeShowViewHelper;
use App\Services\Company\Employee\EmployeeStatus\AssignEmployeeStatusToEmployee;
use App\Services\Company\Employee\EmployeeStatus\RemoveEmployeeStatusFromEmployee;

class EmployeeStatusController extends Controller
{
/**
* Return the list of employee statuses in the company.
*
* @param Request $request
* @param int $companyId
* @param int $employeeId
* @return JsonResponse
*/
public function index(Request $request, int $companyId, int $employeeId): JsonResponse
{
$loggedCompany = InstanceHelper::getLoggedCompany();

$statuses = EmployeeShowViewHelper::employeeStatuses($loggedCompany);

return response()->json([
'data' => $statuses,
], 200);
}

/**
* Assign an employee status to the given employee.
*
Expand Down
15 changes: 7 additions & 8 deletions app/Http/Middleware/CheckCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ public function handle($request, Closure $next)
->where('company_id', $requestedCompanyId)
->firstOrFail();

if ($employee->locked) {
abort(401);
}
if ($employee->locked) {
abort(401);
}

$cachedCompanyObject = 'cachedCompanyObject_' . Auth::user()->id;
$cachedEmployeeObject = 'cachedEmployeeObject_' . Auth::user()->id;

Cache::put($cachedCompanyObject, $employee->company, now()->addMinutes(60));
Cache::put($cachedEmployeeObject, $employee, now()->addMinutes(60));
$cachedCompanyObject = 'cachedCompanyObject_' . Auth::user()->id;
$cachedEmployeeObject = 'cachedEmployeeObject_' . Auth::user()->id;

Cache::put($cachedCompanyObject, $employee->company, now()->addMinutes(60));
Cache::put($cachedEmployeeObject, $employee, now()->addMinutes(60));
} catch (ModelNotFoundException $e) {
abort(401);
}
Expand Down
22 changes: 22 additions & 0 deletions app/Http/ViewHelpers/Employee/EmployeeShowViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Helpers\MoneyHelper;
use App\Helpers\StringHelper;
use App\Helpers\WorklogHelper;
use App\Models\Company\Company;
use App\Models\Company\Employee;
use Illuminate\Support\Collection;
use App\Helpers\WorkFromHomeHelper;
Expand Down Expand Up @@ -625,4 +626,25 @@ public static function oneOnOnes(Employee $employee, array $permissions): ?array
]),
];
}

/**
* Get the employee statuses for the given company.
*
* @param Company $company
* @return Collection
*/
public static function employeeStatuses(Company $company): Collection
{
$statuses = $company->employeeStatuses()->get();

$statusCollection = collect([]);
foreach ($statuses as $status) {
$statusCollection->push([
'id' => $status->id,
'name' => $status->name,
]);
}

return $statusCollection;
}
}
26 changes: 20 additions & 6 deletions resources/js/Pages/Employee/Partials/EmployeeStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<li class="di" data-cy="status-name-right-permission">
{{ updatedEmployee.status.name }}
</li>
<li data-cy="open-status-modal" class="bb b--dotted bt-0 bl-0 br-0 pointer di" @click.prevent="modal = true">
<li data-cy="open-status-modal" class="bb b--dotted bt-0 bl-0 br-0 pointer di" @click.prevent="displayModal()">
{{ $t('app.edit') }}
</li>
</ul>
Expand All @@ -37,7 +37,7 @@
</ul>

<!-- Action when there is no status defined -->
<a v-show="!updatedEmployee.status" v-if="permissions.can_manage_status" class="bb b--dotted bt-0 bl-0 br-0 pointer" data-cy="open-status-modal-blank" @click.prevent="modal = true">
<a v-show="!updatedEmployee.status" v-if="permissions.can_manage_status" class="bb b--dotted bt-0 bl-0 br-0 pointer" data-cy="open-status-modal-blank" @click.prevent="displayModal()">
{{ $t('employee.status_modal_cta') }}
</a>
<span v-else v-show="!updatedEmployee.status">
Expand Down Expand Up @@ -109,10 +109,6 @@ export default {
type: Object,
default: null,
},
statuses: {
type: Array,
default: null,
},
permissions: {
type: Object,
default: null,
Expand All @@ -121,6 +117,7 @@ export default {
data() {
return {
statuses: null,
modal: false,
search: '',
updatedEmployee: Object,
Expand All @@ -144,10 +141,27 @@ export default {
},
methods: {
displayModal() {
this.load();
this.modal = true;
},
toggleModal() {
this.modal = false;
},
load() {
if (! this.statuses) {
axios.get('/' + this.$page.props.auth.company.id + '/employees/' + this.employee.id + '/employeestatuses')
.then(response => {
this.statuses = response.data.data;
})
.catch(error => {
this.form.errors = error.response.data;
});
}
},
assign(status) {
axios.post('/' + this.$page.props.auth.company.id + '/employees/' + this.employee.id + '/employeestatuses', status)
.then(response => {
Expand Down
5 changes: 0 additions & 5 deletions resources/js/Pages/Employee/Partials/HeaderEmployee.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
<span class="f7 gray">{{ $t('employee.status_title') }}</span>
<employee-status
:employee="employee"
:statuses="statuses"
:permissions="permissions"
/>
</p>
Expand Down Expand Up @@ -227,10 +226,6 @@ export default {
type: Array,
default: null,
},
statuses: {
type: Array,
default: null,
},
pronouns: {
type: Array,
default: null,
Expand Down
5 changes: 0 additions & 5 deletions resources/js/Pages/Employee/Performance/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
:employee-teams="employeeTeams"
:positions="positions"
:teams="teams"
:statuses="statuses"
:pronouns="pronouns"
/>

Expand Down Expand Up @@ -105,10 +104,6 @@ export default {
type: Array,
default: null,
},
statuses: {
type: Array,
default: null,
},
pronouns: {
type: Array,
default: null,
Expand Down
5 changes: 0 additions & 5 deletions resources/js/Pages/Employee/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
:employee-teams="employeeTeams"
:positions="positions"
:teams="teams"
:statuses="statuses"
:pronouns="pronouns"
/>

Expand Down Expand Up @@ -196,10 +195,6 @@ export default {
type: Object,
default: null,
},
statuses: {
type: Array,
default: null,
},
pronouns: {
type: Array,
default: null,
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
]);

Route::resource('{employee}/employeestatuses', 'Company\\Employee\\EmployeeStatusController')->only([
'store', 'destroy',
'index', 'store', 'destroy',
]);

Route::resource('{employee}/pronoun', 'Company\\Employee\\EmployeePronounController')->only([
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/ViewHelpers/Employee/EmployeeShowViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\Company\Question;
use App\Models\Company\WorkFromHome;
use App\Models\Company\OneOnOneEntry;
use App\Models\Company\EmployeeStatus;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Company\Employee\Manager\AssignManager;
use App\Http\ViewHelpers\Employee\EmployeeShowViewHelper;
Expand Down Expand Up @@ -548,4 +549,26 @@ public function it_gets_a_collection_of_latest_one_on_one_entries(): void
$array = EmployeeShowViewHelper::oneOnOnes($dwight, ['can_see_one_on_one_with_manager' => false]);
$this->assertNull($array);
}

/** @test */
public function it_gets_a_collection_of_employee_statuses(): void
{
$michael = $this->createAdministrator();

$collection = EmployeeShowViewHelper::employeeStatuses($michael->company);

$this->assertEquals(1, $collection->count());

$status = EmployeeStatus::first();

$this->assertEquals(
[
0 => [
'id' => $status->id,
'name' => $status->name,
],
],
$collection->toArray()
);
}
}

0 comments on commit f7ecdad

Please sign in to comment.