Skip to content

Commit

Permalink
feat: e-coffee with employees (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Feb 7, 2021
1 parent 875fb4a commit c13f75d
Show file tree
Hide file tree
Showing 56 changed files with 2,288 additions and 4 deletions.
36 changes: 36 additions & 0 deletions app/Console/Commands/GenerateNewECoffeeSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Console\Commands;

use App\Models\Company\Company;
use Illuminate\Console\Command;
use App\Jobs\CreateNewECoffeeSession;

class GenerateNewECoffeeSession extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ecoffee:start';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate new eCoffee sessions for all companies in the instance';

/**
* Execute the console command.
*/
public function handle(): void
{
Company::chunk(100, function ($companies) {
$companies->each(function (Company $company) {
CreateNewECoffeeSession::dispatch($company)->onQueue('low');
});
});
}
}
14 changes: 14 additions & 0 deletions app/Console/Commands/Tests/SetupDummyAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use App\Services\Company\Employee\Timesheet\CreateOrGetTimesheet;
use App\Services\Company\Employee\Contract\SetContractRenewalDate;
use App\Services\Company\Employee\Pronoun\AssignPronounToEmployee;
use App\Services\Company\Employee\ECoffee\MatchEmployeesForECoffee;
use App\Services\Company\Employee\OneOnOne\CreateOneOnOneActionItem;
use App\Services\Company\Employee\OneOnOne\ToggleOneOnOneActionItem;
use App\Services\Company\Employee\Position\AssignPositionToEmployee;
Expand Down Expand Up @@ -202,6 +203,7 @@ public function handle(): void
$this->addProjects();
$this->createTimeTrackingEntries();
$this->setContractRenewalDates();
$this->setECoffeeProcess();
$this->addSecondaryBlankAccount();
$this->stop();
}
Expand Down Expand Up @@ -1976,6 +1978,18 @@ private function setContractRenewalDates(): void
]);
}

private function setECoffeeProcess(): void
{
$this->company->e_coffee_enabled = true;
$this->company->save();

for ($i = 0; $i < 10; $i++) {
(new MatchEmployeesForECoffee)->execute([
'company_id' => $this->company->id,
]);
}
}

private function addSecondaryBlankAccount(): void
{
$this->info('☐ Create a blank account');
Expand Down
13 changes: 13 additions & 0 deletions app/Helpers/LogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,19 @@ public static function processAuditLog(AuditLog $log): string
]);
break;

case 'e_coffee_match_session_as_happened':
$sentence = trans('account.log_e_coffee_match_session_as_happened', [
'employee_id' => $log->object->{'employee_id'},
'employee_name' => $log->object->{'employee_name'},
'other_employee_id' => $log->object->{'other_employee_id'},
'other_employee_name' => $log->object->{'other_employee_name'},
]);
break;

case 'toggle_e_coffee_process':
$sentence = trans('account.log_toggle_e_coffee_process');
break;

default:
$sentence = '';
break;
Expand Down
59 changes: 59 additions & 0 deletions app/Http/Controllers/Company/Adminland/AdminECoffeeController.php
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\AdminECoffeeViewHelper;
use App\Services\Company\Adminland\ECoffee\ToggleECoffeeProcess;

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

$eCoffeeDetails = AdminECoffeeViewHelper::eCoffee($company);

return Inertia::render('Adminland/ECoffee/Index', [
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'ecoffee' => $eCoffeeDetails,
]);
}

/**
* Toggle the eCoffee session in the company.
*
* @param Request $request
* @param int $companyId
* @return JsonResponse
*/
public function store(Request $request, int $companyId): JsonResponse
{
$loggedEmployee = InstanceHelper::getLoggedEmployee();
$company = InstanceHelper::getLoggedCompany();

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

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

return response()->json([
'data' => [
'enabled' => $company->e_coffee_enabled,
],
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function index(): Response
'oneOnOnes' => DashboardMeViewHelper::oneOnOnes($employee),
'contractRenewal' => DashboardMeViewHelper::contractRenewal($employee),
'defaultCurrency' => $defaultCompanyCurrency,
'eCoffee' => DashboardMeViewHelper::eCoffee($employee, $company),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers\Company\Dashboard;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Helpers\InstanceHelper;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Company\Employee\ECoffee\MarkECoffeeSessionAsHappened;

class DashboardMeECoffeeController extends Controller
{
/**
* Mark an e-coffee match as happened.
*
* @param Request $request
* @param int $companyId
* @param int $eCoffeeId
* @param int $eCoffeeMatchId
* @return JsonResponse
*/
public function store(Request $request, int $companyId, int $eCoffeeId, int $eCoffeeMatchId): JsonResponse
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

$data = [
'company_id' => $company->id,
'author_id' => $employee->id,
'e_coffee_id' => $eCoffeeId,
'e_coffee_match_id' => $eCoffeeMatchId,
];

(new MarkECoffeeSessionAsHappened)->execute($data);

return response()->json([
'data' => true,
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function store(Request $request): JsonResponse
'status' => $expense->status,
'category' => ($expense->category) ? $expense->category->name : null,
'expensed_at' => DateHelper::formatDate($expense->expensed_at),
'url' => route('employee.expenses.show', [
'url' => route('employee.administration.expenses.show', [
'company' => $employee->company,
'employee' => $employee,
'expense' => $expense,
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Company/Employee/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public function show(Request $request, int $companyId, int $employeeId)
// all skills of this employee
$skills = EmployeeShowViewHelper::skills($employee);

// all eCoffee session of this employee
$ecoffees = EmployeeShowViewHelper::eCoffees($employee, $company);

// information about the employee that the logged employee consults, that depends on what the logged Employee has the right to see
$employee = EmployeeShowViewHelper::informationAboutEmployee($employee, $permissions);

Expand All @@ -114,6 +117,7 @@ public function show(Request $request, int $companyId, int $employeeId)
'questions' => $questions,
'teams' => $employeeTeams,
'skills' => $skills,
'ecoffees' => $ecoffees,
]);
}

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

namespace App\Http\Controllers\Company\Employee;

use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Helpers\InstanceHelper;
use App\Models\Company\Employee;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\ViewHelpers\Employee\EmployeeECoffeeViewHelper;

class EmployeeECoffeeController extends Controller
{
/**
* Show the list of current and past eCoffee sessions.
*
* @param Request $request
* @param int $companyId
* @param int $employeeId
* @return mixed
*/
public function index(Request $request, int $companyId, int $employeeId)
{
$company = InstanceHelper::getLoggedCompany();

try {
$employee = Employee::where('company_id', $companyId)
->where('id', $employeeId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return redirect('home');
}

// information about the eCoffee sessions
$eCoffees = EmployeeECoffeeViewHelper::index($employee, $company);

return Inertia::render('Employee/ECoffee/Index', [
'employee' => [
'id' => $employeeId,
'name' => $employee->name,
],
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'eCoffees' => $eCoffees,
]);
}
}
23 changes: 23 additions & 0 deletions app/Http/ViewHelpers/Adminland/AdminECoffeeViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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
* used in the company.
*
* @param Company $company
* @return array|null
*/
public static function eCoffee(Company $company): ?array
{
return [
'enabled' => $company->e_coffee_enabled,
];
}
}
71 changes: 71 additions & 0 deletions app/Http/ViewHelpers/Dashboard/DashboardMeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use App\Helpers\MoneyHelper;
use App\Helpers\QuestionHelper;
use App\Models\Company\Company;
use App\Models\Company\ECoffee;
use App\Models\Company\Expense;
use App\Models\Company\Employee;
use Illuminate\Support\Collection;
use Money\Currencies\ISOCurrencies;
use App\Models\Company\ECoffeeMatch;
use App\Models\Company\OneOnOneEntry;
use App\Models\Company\EmployeeStatus;
use App\Services\Company\Employee\OneOnOne\CreateOneOnOneEntry;
Expand Down Expand Up @@ -255,6 +257,7 @@ public static function oneOnOnes(Employee $employee): Collection
* Get the information about contract renewal, if the employee is external,
* and if the contract is due in the next 3 months or less.
*
* @param Employee $employee
* @return array|null
*/
public static function contractRenewal(Employee $employee): ?array
Expand Down Expand Up @@ -291,4 +294,72 @@ public static function contractRenewal(Employee $employee): ?array
'late' => false,
];
}

/**
* Get the latest match for the eCoffee program, if it’s enabled for the
* company.
*
* @param Employee $employee
* @param Company $company
* @return array|null
*/
public static function eCoffee(Employee $employee, Company $company): ?array
{
if (! $company->e_coffee_enabled) {
return null;
}

$latestECoffee = $company->eCoffees()->orderBy('id', 'desc')->first();

if (! $latestECoffee) {
return null;
}

$match = ECoffeeMatch::where('e_coffee_id', $latestECoffee->id)
->where(function ($query) use ($employee) {
$query->where('employee_id', $employee->id)
->orWhere('with_employee_id', $employee->id);
})
->firstOrFail();

if ($match->employee_id == $employee->id) {
$otherEmployee = $match->employeeMatchedWith;
} else {
$otherEmployee = $match->employee;
}

$teams = $otherEmployee->teams;
$teamsCollection = collect([]);
foreach ($teams as $team) {
$teamsCollection->push([
'id' => $team->id,
'name' => $team->name,
'url' => route('team.show', [
'company' => $company,
'team' => $team,
]),
]);
}

return [
'id' => $match->id,
'e_coffee_id' => $latestECoffee->id,
'happened' => $match->happened,
'employee' => [
'avatar' => $employee->avatar,
],
'other_employee' => [
'id' => $otherEmployee->id,
'name' => $otherEmployee->name,
'first_name' => $otherEmployee->first_name,
'avatar' => $otherEmployee->avatar,
'position' => $otherEmployee->position ? $otherEmployee->position->title : null,
'url' => route('employees.show', [
'company' => $company,
'employee' => $otherEmployee,
]),
'teams' => $teamsCollection->count() == 0 ? null : $teamsCollection,
],
];
}
}

0 comments on commit c13f75d

Please sign in to comment.