Skip to content

Commit

Permalink
feat: display details of a project task (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Jan 29, 2021
1 parent 6d21621 commit 380fb97
Show file tree
Hide file tree
Showing 35 changed files with 1,626 additions and 266 deletions.
13 changes: 8 additions & 5 deletions app/Console/Commands/Tests/SetupDummyAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -1862,8 +1862,8 @@ private function createTimeTrackingEntries(): void
$this->info('☐ Add time tracking entries');

// create random time tracking entries for the project
// we will create a lot of timesheets, up to 4 years
for ($weeksAgo = 0; $weeksAgo < 150; $weeksAgo++) {
// we will create a lot of timesheets
for ($weeksAgo = 0; $weeksAgo < 5; $weeksAgo++) {
$this->populateTimeTrackingEntries($this->michael, $weeksAgo);
}

Expand All @@ -1873,8 +1873,7 @@ private function createTimeTrackingEntries(): void
foreach ($allDirectReports as $directReport) {
$employee = $directReport->directReport;

$maxWeeksAgo = rand(52, 208);
for ($weeksAgo = 0; $weeksAgo < $maxWeeksAgo; $weeksAgo++) {
for ($weeksAgo = 0; $weeksAgo < 5; $weeksAgo++) {
$this->populateTimeTrackingEntries($employee, $weeksAgo);
}
}
Expand All @@ -1895,6 +1894,10 @@ private function populateTimeTrackingEntries(Employee $employee, int $weeksAgo):
$allTasks = $this->projectInfinity->tasks;

for ($day = 0; $day < 6; $day++) {
$date = $startOfWeek->copy()->addDays($day);
if ($date->isFuture()) {
continue;
}

// taking 3 random tasks in the list of tasks of this project
for ($taskNumber = 0; $taskNumber < 2; $taskNumber++) {
Expand All @@ -1906,7 +1909,7 @@ private function populateTimeTrackingEntries(Employee $employee, int $weeksAgo):
'project_id' => $this->projectInfinity->id,
'project_task_id' => $task->id,
'duration' => rand(30, 180),
'date' => $startOfWeek->copy()->addDays($day)->format('Y-m-d'),
'date' => $date->format('Y-m-d'),
'description' => null,
]);
}
Expand Down
7 changes: 4 additions & 3 deletions app/Helpers/TimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public static function convertToHoursAndMinutes(int $minutes = null): array
}

/**
* Gets a sentence representing the time given in the array.
* Gets a sentence representing the time, like '2h12'.
*
* @param array $duration
* @param int $duration
* @return string
*/
public static function durationInHumanFormat(array $duration): string
public static function durationInHumanFormat(int $duration): string
{
$duration = self::convertToHoursAndMinutes($duration);
$minutes = $duration['minutes'] == 0 ? '00' : $duration['minutes'];

$time = trans('app.duration', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace App\Http\Controllers\Company\Company\Project;

use Carbon\Carbon;
use Inertia\Inertia;
use Inertia\Response;
use App\Helpers\TimeHelper;
use Illuminate\Http\Request;
use App\Helpers\InstanceHelper;
use App\Models\Company\Project;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Helpers\NotificationHelper;
use App\Models\Company\ProjectTask;
use App\Http\Controllers\Controller;
use App\Services\Company\Project\CreateProjectTask;
use App\Services\Company\Project\ToggleProjectTask;
Expand All @@ -18,11 +22,12 @@
use App\Http\ViewHelpers\Company\Project\ProjectViewHelper;
use App\Services\Company\Project\AssignProjecTaskToEmployee;
use App\Http\ViewHelpers\Company\Project\ProjectTasksViewHelper;
use App\Services\Company\Employee\Timesheet\CreateTimeTrackingEntry;

class ProjectTasksController extends Controller
{
/**
* Display the list of messages in the project.
* Display the list of tasks in the project.
*
* @param Request $request
* @param int $companyId
Expand Down Expand Up @@ -50,6 +55,45 @@ public function index(Request $request, int $companyId, int $projectId)
]);
}

/**
* Display the detail of a task.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $taskId
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|Response
*/
public function show(Request $request, int $companyId, int $projectId, int $taskId)
{
$company = InstanceHelper::getLoggedCompany();

try {
$project = Project::where('company_id', $company->id)
->findOrFail($projectId);
} catch (ModelNotFoundException $e) {
return redirect('home');
}

try {
$projectTask = ProjectTask::where('project_id', $project->id)
->with('assignee')
->with('list')
->findOrFail($taskId);
} catch (ModelNotFoundException $e) {
return redirect('home');
}

return Inertia::render('Company/Project/Tasks/Show', [
'tab' => 'tasks',
'project' => ProjectViewHelper::info($projectTask->project),
'task' => ProjectTasksViewHelper::taskDetails($projectTask, $company),
'members' => ProjectTasksViewHelper::members($project),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
}

/**
* Create the task.
*
Expand Down Expand Up @@ -85,7 +129,27 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
}

return response()->json([
'data' => ProjectTasksViewHelper::show($task, $company),
'data' => [
'id' => $task->id,
'title' => $task->title,
'description' => $task->description,
'completed' => $task->completed,
'url' => route('projects.tasks.show', [
'company' => $company,
'project' => $task->project_id,
'task' => $task->id,
]),
'duration' => null,
'assignee' => $task->assignee ? [
'id' => $task->assignee->id,
'name' => $task->assignee->name,
'avatar' => $task->assignee->avatar,
'url' => route('employees.show', [
'company' => $company,
'employee' => $task->assignee,
]),
] : null,
],
], 201);
}

Expand All @@ -109,7 +173,7 @@ public function update(Request $request, int $companyId, int $projectId, int $ta
'project_id' => $projectId,
'project_task_id' => $taskId,
'title' => $request->input('title'),
'content' => $request->input('content'),
'description' => $request->input('description'),
];

$task = (new UpdateProjectTask)->execute($data);
Expand All @@ -124,8 +188,32 @@ public function update(Request $request, int $companyId, int $projectId, int $ta
]);
}

$duration = DB::table('time_tracking_entries')
->where('project_task_id', $task->id)
->sum('duration');

return response()->json([
'data' => ProjectTasksViewHelper::show($task, $company),
'data' => [
'id' => $task->id,
'title' => $task->title,
'description' => $task->description,
'completed' => $task->completed,
'duration' => $duration != 0 ? TimeHelper::durationInHumanFormat($duration) : null,
'url' => route('projects.tasks.show', [
'company' => $company,
'project' => $task->project_id,
'task' => $task->id,
]),
'assignee' => $task->assignee ? [
'id' => $task->assignee->id,
'name' => $task->assignee->name,
'avatar' => $task->assignee->avatar,
'url' => route('employees.show', [
'company' => $company,
'employee' => $task->assignee,
]),
] : null,
],
], 201);
}

Expand Down Expand Up @@ -153,8 +241,8 @@ public function toggle(Request $request, int $companyId, int $projectId, int $pr
$task = (new ToggleProjectTask)->execute($data);

return response()->json([
'data' => ProjectTasksViewHelper::show($task, $company),
], 201);
'data' => true,
], 200);
}

/**
Expand Down Expand Up @@ -182,6 +270,73 @@ public function destroy(Request $request, int $companyId, int $projectId, int $t

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

/**
* Get the time tracking entries for the given task.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $taskId
* @return JsonResponse
*/
public function timeTrackingEntries(Request $request, int $companyId, int $projectId, int $taskId): JsonResponse
{
$company = InstanceHelper::getLoggedCompany();

try {
$project = Project::where('company_id', $company->id)
->findOrFail($projectId);
} catch (ModelNotFoundException $e) {
return redirect('home');
}

try {
$projectTask = ProjectTask::where('project_id', $project->id)
->findOrFail($taskId);
} catch (ModelNotFoundException $e) {
return redirect('home');
}

return response()->json([
'data' => ProjectTasksViewHelper::timeTrackingEntries($projectTask, $company),
], 200);
}

/**
* Get the time tracking entries for the given task.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $taskId
* @return JsonResponse
*/
public function logTime(Request $request, int $companyId, int $projectId, int $taskId): JsonResponse
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

$entry = (new CreateTimeTrackingEntry)->execute([
'author_id' => $employee->id,
'employee_id' => $employee->id,
'company_id' => $company->id,
'project_id' => $projectId,
'project_task_id' => $taskId,
'duration' => $request->input('duration'),
'date' => Carbon::now()->format('Y-m-d'),
]);

$duration = DB::table('time_tracking_entries')
->where('project_task_id', $taskId)
->sum('duration');

$totalDuration = TimeHelper::durationInHumanFormat($duration);

return response()->json([
'data' => $totalDuration,
], 201);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function year(Request $request, int $companyId, int $employeeId, int $yea
->whereYear('started_at', (string) $year)
->addSelect([
'duration' => TimeTrackingEntry::select(DB::raw('SUM(duration) as duration'))
->whereColumn('timesheet_id', 'timesheets.id')
->groupBy('timesheet_id'),
->whereColumn('timesheet_id', 'timesheets.id')
->groupBy('timesheet_id'),
])
->get();

Expand Down

0 comments on commit 380fb97

Please sign in to comment.