Skip to content

Commit

Permalink
feat: ability for employees to upload avatars (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Mar 16, 2021
1 parent 8bb4812 commit 6a1e8c9
Show file tree
Hide file tree
Showing 55 changed files with 1,060 additions and 995 deletions.
50 changes: 50 additions & 0 deletions app/Console/Commands/Tests/SetupDummyAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Faker\Factory as Faker;
use App\Models\Company\File;
use App\Models\Company\Team;
use App\Models\User\Pronoun;
use App\Models\Company\Company;
Expand Down Expand Up @@ -190,6 +191,7 @@ public function handle(): void
$this->createTeams();
$this->addTeamDescriptions();
$this->createEmployees();
$this->addAvatars();
$this->createFutureEmployees();
$this->addSkills();
$this->addWorkFromHomeEntries();
Expand Down Expand Up @@ -735,6 +737,38 @@ private function createEmployees(): void
$this->addSpecificDataToEmployee($this->philip, $description, $this->pronounHeHim, $this->teamWarehouse, $this->employeeStatusFullTime, $this->positionWarehouseStaff, null, $this->val);
}

private function addAvatars(): void
{
$this->info('☐ Add avatars of employees');

$this->michael->avatar_file_id = $this->createAvatar('9952eaee-269b-4e3c-b41d-0f613d4128c3')->id;
$this->michael->save();
$this->toby->avatar_file_id = $this->createAvatar('97502e6a-0898-44e0-afd2-9476d76894ce')->id;
$this->toby->save();
$this->jim->avatar_file_id = $this->createAvatar('fb76ac9c-ece5-49df-a519-3fc18a71602f')->id;
$this->jim->save();
$this->erin->avatar_file_id = $this->createAvatar('d829a8c1-cc01-4b03-ac6d-a997f48d0e6f')->id;
$this->erin->save();
$this->kelly->avatar_file_id = $this->createAvatar('14a6d0a0-1f95-47ea-965c-39405e72d913')->id;
$this->kelly->save();
$this->jan->avatar_file_id = $this->createAvatar('7e10ffe0-c6f8-481b-b96b-be7a579813b3')->id;
$this->jan->save();
$this->kevin->avatar_file_id = $this->createAvatar('9916c3d3-f099-426f-9d1c-d8e4e7c87edd')->id;
$this->kevin->save();
$this->angela->avatar_file_id = $this->createAvatar('1d1e5b14-1382-45fa-8e08-b4daa01b0908')->id;
$this->angela->save();
$this->oscar->avatar_file_id = $this->createAvatar('2ad68247-a0a5-4bda-9db0-839d3e40c2c7')->id;
$this->oscar->save();
$this->meredith->avatar_file_id = $this->createAvatar('8dbdff2c-be0d-4a02-ae0a-a39656820dd9')->id;
$this->meredith->save();
$this->dwight->avatar_file_id = $this->createAvatar('c1eab52a-1b79-4536-a2aa-e821605f92d2')->id;
$this->dwight->save();
$this->phyllis->avatar_file_id = $this->createAvatar('a95cbf7b-9f28-438a-a1fc-8c8d93be4939')->id;
$this->phyllis->save();
$this->dakota->avatar_file_id = $this->createAvatar('6113654a-be8a-4829-90f4-0c7aa2a5469c')->id;
$this->dakota->save();
}

private function createFutureEmployees(): void
{
$this->info('☐ Add employees that will be hired in two days');
Expand Down Expand Up @@ -2024,4 +2058,20 @@ private function artisan(string $message, string $command, array $arguments = []
$this->info($message);
$this->callSilent($command, $arguments);
}

private function createAvatar(string $uuid): File
{
return File::create([
'company_id' => $this->company->id,
'uploader_employee_id' => $this->michael->id,
'uploader_name' => $this->michael->name,
'uuid' => $uuid,
'name' => 'name',
'original_url' => 'https://ucarecdn.com/'.$uuid.'/',
'cdn_url' => 'https://ucarecdn.com/'.$uuid.'/',
'mime_type' => 'image/webp',
'size' => 11100,
'type' => 'avatar',
]);
}
}
15 changes: 13 additions & 2 deletions app/Helpers/AvatarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ class AvatarHelper
* Get the avatar of the user, at the requested size if it exists.
*
* @var Employee
* @var int
* @return string|null
*/
public static function getImage(Employee $employee): ?string
public static function getImage(Employee $employee, int $width = null): ?string
{
return $employee->avatar;
if (! $employee->avatar_file_id) {
return 'https://ui-avatars.com/api/?name='.$employee->name;
}

if ($width) {
$url = $employee->picture->cdn_url.'-/scale_crop/'.$width.'x'.$width.'/smart/';
} else {
$url = $employee->picture->cdn_url;
}

return $url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function assign(Request $request, int $companyId, int $projectId): JsonRe
'data' => [
'id' => $lead->id,
'name' => $lead->name,
'avatar' => AvatarHelper::getImage($lead),
'avatar' => AvatarHelper::getImage($lead, 35),
'position' => (! $lead->position) ? null : [
'id' => $lead->position->id,
'title' => $lead->position->title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
'data' => [
'id' => $employee->id,
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 64),
'role' => $request->input('role'),
'added_at' => DateHelper::formatDate(Carbon::now()),
'position' => (! $employee->position) ? null : [
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Company/Employee/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function index(Request $request, int $companyId): Response
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 64),
'teams' => $employee->teams,
'position' => (! $employee->position) ? null : [
'id' => $employee->position->id,
Expand Down Expand Up @@ -110,6 +110,7 @@ public function show(Request $request, int $companyId, int $employeeId)

return Inertia::render('Employee/Show', [
'menu' => 'presentation',
'uploadcarePublicKey' => config('officelife.uploadcare_public_key'),
'employee' => $employee,
'permissions' => $permissions,
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Controllers\Company\Employee;

use Inertia\Response;
use Illuminate\Http\Request;
use App\Helpers\AvatarHelper;
use App\Helpers\InstanceHelper;
use App\Models\Company\Employee;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Company\Adminland\File\UploadFile;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class EmployeeEditAvatarController extends Controller
{
/**
* Update the avatar.
*
* @param Request $request
* @param int $companyId
* @param int $employeeId
* @return JsonResponse
*/
public function update(Request $request, int $companyId, int $employeeId): JsonResponse
{
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

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

$file = (new UploadFile)->execute([
'company_id' => $loggedCompany->id,
'author_id' => $loggedEmployee->id,
'uuid' => $request->input('uuid'),
'name' => $request->input('name'),
'original_url' => $request->input('original_url'),
'cdn_url' => $request->input('cdn_url'),
'mime_type' => $request->input('mime_type'),
'size' => $request->input('size'),
'type' => 'avatar',
]);

$employee->avatar_file_id = $file->id;
$employee->save();
$employee->refresh();

return response()->json([
'avatar' => AvatarHelper::getImage($employee),
], 200);
}
}
17 changes: 2 additions & 15 deletions app/Http/Controllers/Company/Team/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Http\Collections\TeamNewsCollection;
use App\Http\ViewHelpers\Team\TeamShowViewHelper;
use App\Http\Collections\TeamUsefulLinkCollection;
use App\Http\ViewHelpers\Team\TeamIndexViewHelper;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class TeamController extends Controller
Expand All @@ -27,22 +28,8 @@ public function index(Request $request, int $companyId): Response
{
$company = InstanceHelper::getLoggedCompany();

$teams = $company->teams()
->with('employees')
->orderBy('name', 'asc')
->get();

$teamsCollection = collect([]);
foreach ($teams as $team) {
$teamsCollection->push([
'id' => $team->id,
'name' => $team->name,
'employees' => $team->employees,
]);
}

return Inertia::render('Team/Index', [
'teams' => $teamsCollection,
'teams' => TeamIndexViewHelper::index($company),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function search(Request $request, int $companyId): JsonResponse
$employees->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 23),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewHelpers/Adminland/AdminEmployeeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static function getCollectionOfEmployees(Collection $employees, Company
'id' => $employee->id,
'name' => $employee->name,
'permission_level' => $employee->permission_level,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 64),
'invitation_link' => $employee->invitation_link,
'invited' => (! $employee->invitation_used_at && $employee->invitation_link) === true,
'lock_status' => $employee->locked,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewHelpers/Company/CompanyQuestionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function question(Question $question, $answers, Employee $employee
'body' => $answer->body,
'employee' => [
'name' => $answer->employee->name,
'avatar' => AvatarHelper::getImage($answer->employee),
'avatar' => AvatarHelper::getImage($answer->employee, 22),
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewHelpers/Company/CompanySkillViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function employeesWithSkill(Skill $skill): ?Collection
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 65),
'position' => (! $employee->position) ? null : [
'title' => $employee->position->title,
],
Expand Down
8 changes: 3 additions & 5 deletions app/Http/ViewHelpers/Company/CompanyViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static function birthdaysThisWeek(Company $company): array
$employees = $company->employees()
->where('locked', false)
->whereNotNull('birthdate')
->select('id', 'first_name', 'last_name', 'avatar', 'birthdate')
->get();

$now = Carbon::now();
Expand All @@ -109,7 +108,7 @@ public static function birthdaysThisWeek(Company $company): array
$birthdaysCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 35),
'birthdate' => DateHelper::formatMonthAndDay($birthdateWithCurrentYear),
'sort_key' => Carbon::createFromDate($now->year, $birthdateWithCurrentYear->month, $birthdateWithCurrentYear->day)->format('Y-m-d'),
'url' => route('employees.show', [
Expand Down Expand Up @@ -137,7 +136,6 @@ public static function newHiresThisWeek(Company $company): Collection
{
$now = Carbon::now();
$employees = $company->employees()
->select('id', 'first_name', 'last_name', 'avatar', 'hired_at', 'position_id')
->where('locked', false)
->whereNotNull('hired_at')
->whereDate('hired_at', '>=', $now->copy()->startOfWeek(Carbon::MONDAY))
Expand All @@ -158,7 +156,7 @@ public static function newHiresThisWeek(Company $company): Collection
'employee' => $employee->id,
]),
'name' => $employee->name,
'avatar' => AvatarHelper::getImage($employee),
'avatar' => AvatarHelper::getImage($employee, 35),
'hired_at' => DateHelper::formatDayAndMonthInParenthesis($date),
'position' => (! $position) ? null : $position->title,
]);
Expand Down Expand Up @@ -317,7 +315,7 @@ public static function guessEmployeeGameInformation(Employee $employee, Company

return [
'id' => $game->id,
'avatar_to_find' => AvatarHelper::getImage($employeeToFind),
'avatar_to_find' => AvatarHelper::getImage($employeeToFind, 80),
'choices' => $choices,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function decisions(Project $project): Collection
$decidersCollection->push([
'id' => $decider->id,
'name' => $decider->name,
'avatar' => AvatarHelper::getImage($decider),
'avatar' => AvatarHelper::getImage($decider, 22),
'url' => route('employees.show', [
'company' => $company,
'employee' => $decider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function members(Project $project): array
$membersCollection->push([
'id' => $member->id,
'name' => $member->name,
'avatar' => AvatarHelper::getImage($member),
'avatar' => AvatarHelper::getImage($member, 64),
'role' => $member->pivot->role,
'added_at' => DateHelper::formatDate($member->pivot->created_at),
'position' => (! $member->position) ? null : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function index(Project $project, Employee $employee): Collection
'author' => $author ? [
'id' => $author->id,
'name' => $author->name,
'avatar' => AvatarHelper::getImage($author),
'avatar' => AvatarHelper::getImage($author, 22),
'url_view' => route('employees.show', [
'company' => $company,
'employee' => $author,
Expand Down Expand Up @@ -104,7 +104,7 @@ public static function show(ProjectMessage $projectMessage): array
'author' => $author ? [
'id' => $author->id,
'name' => $author->name,
'avatar' => AvatarHelper::getImage($author),
'avatar' => AvatarHelper::getImage($author, 64),
'role' => $role ? $role->role : null,
'added_at' => $role ? DateHelper::formatDate(Carbon::createFromFormat('Y-m-d H:i:s', $role->created_at)) : null,
'position' => (! $author->position) ? null : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function index(Project $project): array
'assignee' => $assignee ? [
'id' => $assignee->id,
'name' => $assignee->name,
'avatar' => AvatarHelper::getImage($assignee),
'avatar' => AvatarHelper::getImage($assignee, 15),
'url' => route('employees.show', [
'company' => $company,
'employee' => $assignee,
Expand Down Expand Up @@ -109,7 +109,7 @@ public static function index(Project $project): array
'assignee' => $assignee ? [
'id' => $assignee->id,
'name' => $assignee->name,
'avatar' => AvatarHelper::getImage($assignee),
'avatar' => AvatarHelper::getImage($assignee, 15),
'url' => route('employees.show', [
'company' => $company,
'employee' => $assignee,
Expand Down Expand Up @@ -203,7 +203,7 @@ public static function getTaskFullDetails(ProjectTask $task, Company $company):
'author' => $author ? [
'id' => $author->id,
'name' => $author->name,
'avatar' => AvatarHelper::getImage($author),
'avatar' => AvatarHelper::getImage($author, 35),
'role' => $role ? $role->role : null,
'added_at' => $role ? DateHelper::formatDate(Carbon::createFromFormat('Y-m-d H:i:s', $role->created_at)) : null,
'position' => (! $author->position) ? null : $author->position->title,
Expand Down Expand Up @@ -262,7 +262,7 @@ public static function timeTrackingEntries(ProjectTask $projectTask, Company $co
// on long tasks, and this would not be efficient at all
$timeTrackingEntries = DB::table('time_tracking_entries')
->join('employees', 'time_tracking_entries.employee_id', '=', 'employees.id')
->select('time_tracking_entries.id', 'time_tracking_entries.duration', 'time_tracking_entries.happened_at', 'employees.id as employee_id', 'employees.avatar', 'employees.first_name', 'employees.last_name')
->select('time_tracking_entries.id', 'time_tracking_entries.duration', 'time_tracking_entries.happened_at', 'employees.id as employee_id', 'employees.first_name', 'employees.last_name')
->where('project_task_id', $projectTask->id)
->orderBy('time_tracking_entries.happened_at', 'desc')
->get();
Expand Down

0 comments on commit 6a1e8c9

Please sign in to comment.