Skip to content

Commit

Permalink
feat: add ability to disable work from home feature (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Jun 21, 2021
1 parent 80912d7 commit 9b5b66a
Show file tree
Hide file tree
Showing 22 changed files with 449 additions and 8 deletions.
6 changes: 5 additions & 1 deletion app/Helpers/LogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,10 @@ public static function processAuditLog(AuditLog $log): string
$sentence = trans('account.log_toggle_e_coffee_process');
break;

case 'toggle_work_from_home_process':
$sentence = trans('account.log_toggle_work_from_home_process');
break;

case 'group_created':
$sentence = trans('account.log_group_created', [
'group_id' => $log->object->{'group_id'},
Expand Down Expand Up @@ -1295,7 +1299,7 @@ public static function processAuditLog(AuditLog $log): string
'expense_title' => $log->object->{'expense_title'},
]);
break;

case 'file_added_to_software':
$sentence = trans('account.log_file_added_to_software', [
'software_name' => $log->object->{'software_name'},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers\Company\Adminland;

use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Http\Request;
use App\Helpers\InstanceHelper;
use Illuminate\Http\JsonResponse;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use App\Http\ViewHelpers\Adminland\AdminWorkFromHomeViewHelper;
use App\Services\Company\Adminland\WorkFromHome\ToggleWorkFromHomeProcess;

class AdminWorkFromHomeController extends Controller
{
/**
* Show the work from home page.
*
* @return Response
*/
public function index(): Response
{
$company = InstanceHelper::getLoggedCompany();

$details = AdminWorkFromHomeViewHelper::index($company);

return Inertia::render('Adminland/WorkFromHome/Index', [
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'process' => $details,
]);
}

/**
* Toggle the work from home setting in the company.
*
* @param Request $request
* @param int $companyId
* @return JsonResponse
*/
public function update(Request $request, int $companyId): JsonResponse
{
$loggedEmployee = InstanceHelper::getLoggedEmployee();
$company = InstanceHelper::getLoggedCompany();

$data = [
'company_id' => $company->id,
'author_id' => $loggedEmployee->id,
];

$company = (new ToggleWorkFromHomeProcess)->execute($data);

return response()->json([
'data' => [
'enabled' => $company->work_from_home_enabled,
],
], 200);
}
}
3 changes: 1 addition & 2 deletions app/Http/ViewHelpers/Adminland/AdminECoffeeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace App\Http\ViewHelpers\Adminland;

use App\Models\Company\Company;
use Illuminate\Support\Collection;

class AdminECoffeeViewHelper
{
/**
* Collection containing all the information about the expense categories
* Get the information about the ecoffee process
* used in the company.
*
* @param Company $company
Expand Down
22 changes: 22 additions & 0 deletions app/Http/ViewHelpers/Adminland/AdminWorkFromHomeViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\ViewHelpers\Adminland;

use App\Models\Company\Company;

class AdminWorkFromHomeViewHelper
{
/**
* Get the information about the work from home process
* used in the company.
*
* @param Company $company
* @return array|null
*/
public static function index(Company $company): ?array
{
return [
'enabled' => $company->work_from_home_enabled,
];
}
}
1 change: 1 addition & 0 deletions app/Http/ViewHelpers/Dashboard/DashboardMeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ public static function morale(Employee $employee): ?array
public static function workFromHome(Employee $employee): ?array
{
return [
'feature_enabled' => $employee->company->work_from_home_enabled,
'has_worked_from_home_today' => WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, Carbon::now()),
];
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/ViewHelpers/Employee/EmployeeShowViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public static function workFromHomeStats(Employee $employee): array
$workFromHomes = $employee->workFromHomes()->whereYear('date', (string) $currentYear)->count();

return [
'feature_enabled' => $employee->company->work_from_home_enabled,
'work_from_home_today' => WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, $now),
'number_times_this_year' => $workFromHomes,
'url' => route('employee.work.workfromhome', [
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Company/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Company extends Model
'has_dummy_data',
'logo_file_id',
'e_coffee_enabled',
'work_from_home_enabled',
'founded_at',
'code_to_join_company',
];
Expand All @@ -50,6 +51,7 @@ class Company extends Model
protected $casts = [
'has_dummy_data' => 'boolean',
'e_coffee_enabled' => 'boolean',
'work_from_home_enabled' => 'boolean',
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Services\Company\Adminland\WorkFromHome;

use Carbon\Carbon;
use App\Jobs\LogAccountAudit;
use App\Services\BaseService;
use App\Models\Company\Company;

class ToggleWorkFromHomeProcess extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'company_id' => 'required|integer|exists:companies,id',
'author_id' => 'required|integer|exists:employees,id',
];
}

/**
* Toggle the work from home process in the company.
*
* @param array $data
* @return Company
*/
public function execute(array $data): Company
{
$this->validateRules($data);

$this->author($data['author_id'])
->inCompany($data['company_id'])
->asAtLeastHR()
->canExecuteService();

$company = Company::findOrFail($data['company_id']);

$company->work_from_home_enabled = ! $company->work_from_home_enabled;
$company->save();
$company->refresh();

LogAccountAudit::dispatch([
'company_id' => $data['company_id'],
'action' => 'toggle_work_from_home_process',
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'audited_at' => Carbon::now(),
'objects' => json_encode([]),
])->onQueue('low');

return $company;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddToggleWorkFromHome extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('companies', function (Blueprint $table) {
$table->boolean('work_from_home_enabled')->default(true)->after('e_coffee_enabled');
});
}
}
Binary file added public/img/streamline-icon-cat-house@140x140.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion resources/js/Pages/Adminland/ECoffee/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<h2 class="tc normal mb4">
{{ $t('account.ecoffee_title') }}

<help :url="$page.props.help_links.account_general_currency" :datacy="'help-icon-general'" :top="'2px'" />
<help :url="$page.props.help_links.ecoffee" :datacy="'help-icon-general'" :top="'2px'" />
</h2>

<div class="relative">
Expand Down
9 changes: 8 additions & 1 deletion resources/js/Pages/Adminland/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@
<div class="pa2 pl0 relative">
<span class="mr1">
☕️
</span> <inertia-link :href="'/' + $page.props.auth.company.id + '/account/ecoffee'" data-cy="expenses-admin-link">
</span> <inertia-link :href="'/' + $page.props.auth.company.id + '/account/ecoffee'">
{{ $t('account.home_manage_ecoffee') }}
</inertia-link>
</div>
<div class="pa2 pl0 relative">
<span class="mr1">
🏡
</span> <inertia-link :href="'/' + $page.props.auth.company.id + '/account/workFromHome'">
{{ $t('account.home_manage_work_from_home') }}
</inertia-link>
</div>
</div>
</div>

Expand Down
139 changes: 139 additions & 0 deletions resources/js/Pages/Adminland/WorkFromHome/Index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<style lang="scss" scoped>
.status-active {
background-color: #dcf7ee;
.dot {
background-color: #00b760;
}
}
.status-inactive {
background-color: #ffe9e3;
.dot {
background-color: #ff3400;
}
}
.dot {
height: 8px;
top: 3px;
}
</style>

<template>
<layout :notifications="notifications">
<div class="ph2 ph0-ns">
<!-- BREADCRUMB -->
<div class="mt4-l mt1 mw6 br3 bg-white box center breadcrumb relative z-0 f6 pb2">
<ul class="list ph0 tc-l tl">
<li class="di">
<inertia-link :href="'/' + $page.props.auth.company.id + '/dashboard'">{{ $t('app.breadcrumb_dashboard') }}</inertia-link>
</li>
<li class="di">
<inertia-link :href="'/' + $page.props.auth.company.id + '/account'">{{ $t('app.breadcrumb_account_home') }}</inertia-link>
</li>
<li class="di">
{{ $t('app.breadcrumb_account_manage_work_from_home') }}
</li>
</ul>
</div>

<!-- BODY -->
<div class="mw7 center br3 mb5 bg-white box restricted relative z-1">
<div class="pa3 mt5">
<h2 class="tc normal mb4">
{{ $t('account.work_from_home_title') }}

<help :url="$page.props.help_links.work_from_home" :datacy="'help-icon-general'" :top="'2px'" />
</h2>

<div class="relative">
<img loading="lazy" src="/img/streamline-icon-cat-house@140x140.png" alt="work from home symbol" class="absolute left-1 mr1" height="80"
width="80"
/>

<div class="ml6">
<p class="lh-copy">{{ $t('account.work_from_home_desc') }}</p>

<p v-if="localWorkFromHome.enabled" data-cy="message-enable" class="status-active dib pa3 br3">
<span class="br3 f7 fw3 ph2 pv1 dib relative mr1 dot"></span>
{{ $t('account.work_from_home_enabled') }}
</p>

<p v-if="!localWorkFromHome.enabled" data-cy="message-disable" class="status-inactive dib pa3 br3">
<span class="br3 f7 fw3 ph2 pv1 dib relative mr1 dot"></span>
{{ $t('account.work_from_home_disabled') }}
</p>

<form @submit.prevent="toggleProcess">
<errors :errors="form.errors" />

<loading-button v-if="!localWorkFromHome.enabled" :class="'btn w-auto-ns w-100 mb2 pv2 ph3'" :state="loadingState" :text="$t('app.enable')" :cypress-selector="'enable-ecoffee-process'" />
<loading-button v-else :class="'btn w-auto-ns w-100 mb2 pv2 ph3'" :state="loadingState" :text="$t('app.disable')" :cypress-selector="'disable-ecoffee-process'" />
</form>
</div>
</div>
</div>
</div>
</div>
</layout>
</template>

<script>
import LoadingButton from '@/Shared/LoadingButton';
import Layout from '@/Shared/Layout';
import Errors from '@/Shared/Errors';
import Help from '@/Shared/Help';
export default {
components: {
Layout,
LoadingButton,
Errors,
Help,
},
props: {
notifications: {
type: Array,
default: null,
},
process: {
type: Object,
default: null,
},
},
data() {
return {
loadingState: null,
localWorkFromHome: null,
form: {
errors: [],
},
};
},
created() {
this.localWorkFromHome = this.process;
},
methods: {
toggleProcess() {
this.loadingState = 'loading';
axios.put(`${this.$page.props.auth.company.id}/account/workFromHome`, this.form)
.then(response => {
this.loadingState = null;
this.localWorkFromHome = response.data.data;
})
.catch(error => {
this.loadingState = null;
this.form.errors = error.response.data;
});
},
}
};
</script>

0 comments on commit 9b5b66a

Please sign in to comment.