Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change timezone location #1024

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Helpers/InstanceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class InstanceHelper
public static function getLoggedCompany()
{
if (Auth::check()) {
return Cache::get('cachedCompanyObject_'.Auth::user()->id);
return Cache::get('cachedCompanyObject_'.Auth::id());
}
}

Expand All @@ -28,7 +28,7 @@ public static function getLoggedCompany()
public static function getLoggedEmployee()
{
if (Auth::check()) {
return Cache::get('cachedEmployeeObject_'.Auth::user()->id);
return Cache::get('cachedEmployeeObject_'.Auth::id());
}
}
}
3 changes: 2 additions & 1 deletion app/Helpers/WorklogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Company\Morale;
use App\Models\Company\Worklog;
use App\Models\Company\Employee;
use Illuminate\Support\Facades\Auth;

class WorklogHelper
{
Expand All @@ -22,7 +23,7 @@ class WorklogHelper
public static function getDailyInformationForEmployee(Carbon $date, Worklog $worklog = null, Morale $morale = null, Employee $loggedEmployee): array
{
return [
'date' => DateHelper::formatShortDateWithTime($date, $loggedEmployee->timezone),
'date' => DateHelper::formatShortDateWithTime($date, Auth::user()->timezone),
'friendly_date' => DateHelper::formatDayAndMonthInParenthesis($date),
'status' => DateHelper::determineDateStatus($date),
'worklog_parsed_content' => is_null($worklog) ? null : StringHelper::parse($worklog->content),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

use Carbon\Carbon;
use Inertia\Inertia;
use Inertia\Response;
use App\Helpers\DateHelper;
use App\Helpers\ImageHelper;
use Illuminate\Http\Request;
use App\Helpers\InstanceHelper;
use App\Models\Company\Project;
use App\Models\Company\Employee;
use Illuminate\Http\JsonResponse;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Company\Project\CreateProjectDecision;
use App\Services\Company\Project\DestroyProjectDecision;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -147,7 +146,7 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
'data' => [
'id' => $projectDecision->id,
'title' => $projectDecision->title,
'decided_at' => DateHelper::formatDate($projectDecision->decided_at, $loggedEmployee->timezone),
'decided_at' => DateHelper::formatDate($projectDecision->decided_at, Auth::user()->timezone),
'deciders' => $decidersCollection,
],
], 201);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Http\JsonResponse;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Company\Project\AddFileToProject;
use App\Services\Company\Adminland\File\UploadFile;
use App\Services\Company\Project\DestroyProjectFile;
Expand Down Expand Up @@ -106,7 +107,7 @@ public function store(Request $request, int $companyId, int $projectId): ?JsonRe
'employee' => $loggedEmployee,
]),
],
'created_at' => DateHelper::formatDate($file->created_at, $loggedEmployee->timezone),
'created_at' => DateHelper::formatDate($file->created_at, Auth::user()->timezone),
],
], 200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Http\JsonResponse;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Company\Project\AddEmployeeToProject;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\ViewHelpers\Company\Project\ProjectViewHelper;
Expand Down Expand Up @@ -106,7 +107,7 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 64),
'role' => $request->input('role'),
'added_at' => DateHelper::formatDate(Carbon::now(), $loggedEmployee->timezone),
'added_at' => DateHelper::formatDate(Carbon::now(), Auth::user()->timezone),
'position' => (! $employee->position) ? null : [
'id' => $employee->position->id,
'title' => $employee->position->title,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Company/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function create(): Response
public function store(Request $request)
{
$company = (new CreateCompany)->execute([
'author_id' => Auth::user()->id,
'author_id' => Auth::id(),
'name' => $request->input('name'),
]);

Expand All @@ -61,7 +61,7 @@ public function join(Request $request)
public function actuallyJoin(Request $request)
{
$company = (new JoinCompany)->execute([
'user_id' => Auth::user()->id,
'user_id' => Auth::id(),
'code' => $request->input('code'),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use App\Helpers\DateHelper;
use App\Helpers\MoneyHelper;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Helpers\InstanceHelper;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Company\Employee\Expense\CreateExpense;

class DashboardMeExpenseController extends Controller
Expand Down Expand Up @@ -46,7 +46,7 @@ public function store(Request $request): JsonResponse
'amount' => MoneyHelper::format($expense->amount, $expense->currency),
'status' => $expense->status,
'category' => ($expense->category) ? $expense->category->name : null,
'expensed_at' => DateHelper::formatDate($expense->expensed_at, $employee->timezone),
'expensed_at' => DateHelper::formatDate($expense->expensed_at, Auth::user()->timezone),
'url' => route('employee.administration.expenses.show', [
'company' => $employee->company,
'employee' => $employee,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Inertia\Response;
use Illuminate\Http\Request;
use App\Helpers\InstanceHelper;
use App\Helpers\TimezoneHelper;
use App\Models\Company\Country;
use App\Models\Company\Employee;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -61,7 +60,6 @@ public function show(Request $request, int $companyId, int $employeeId)

return Inertia::render('Employee/Edit', [
'employee' => EmployeeEditViewHelper::show($employee),
'timezones' => TimezoneHelper::getListOfTimezones(),
'permissions' => EmployeeShowViewHelper::permissions($loggedEmployee, $employee),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Company\Employee;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\ViewHelpers\Employee\EmployeeShowViewHelper;
use App\Http\ViewHelpers\Employee\EmployeeWorkViewHelper;
Expand Down Expand Up @@ -43,8 +44,8 @@ public function show(Request $request, int $companyId, int $employeeId)
$permissions = EmployeeShowViewHelper::permissions($loggedEmployee, $employee);

// worklogs
$startOfWeek = Carbon::now()->setTimezone($loggedEmployee->timezone)->startOfWeek();
$currentDay = Carbon::now()->setTimezone($loggedEmployee->timezone);
$startOfWeek = Carbon::now()->setTimezone(Auth::user()->timezone)->startOfWeek();
$currentDay = Carbon::now()->setTimezone(Auth::user()->timezone);
$worklogsCollection = EmployeeWorkViewHelper::worklog($employee, $loggedEmployee, $startOfWeek, $currentDay);
$weeks = EmployeeWorkViewHelper::weeks($loggedEmployee);

Expand Down Expand Up @@ -102,12 +103,12 @@ public function worklogDay(Request $request, int $companyId, int $employeeId, st
return redirect('home');
}

$startOfWeek = Carbon::createFromFormat('Y-m-d', $week, $loggedEmployee->timezone);
$startOfWeek = Carbon::createFromFormat('Y-m-d', $week, Auth::user()->timezone);

if (! $day) {
$day = $startOfWeek->copy()->addDays(4);
} else {
$day = Carbon::createFromFormat('Y-m-d', $day, $loggedEmployee->timezone);
$day = Carbon::createFromFormat('Y-m-d', $day, Auth::user()->timezone);
}

$worklog = EmployeeWorkViewHelper::worklog($employee, $loggedEmployee, $startOfWeek, $day);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HelpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HelpController extends Controller
public function toggle(Request $request): JsonResponse
{
$data = [
'user_id' => Auth::user()->id,
'user_id' => Auth::id(),
'visibility' => ! Auth::user()->show_help,
];

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function list()
*/
private function companies(bool $redirect)
{
Cache::forget('cachedCompanyObject_'.Auth::user()->id);
Cache::forget('cachedEmployeeObject_'.Auth::user()->id);
Cache::forget('cachedCompanyObject_'.Auth::id());
Cache::forget('cachedEmployeeObject_'.Auth::id());

$employees = Auth::user()->employees()->with('company')->notLocked()->get();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/User/LocaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LocaleController extends Controller
public function update(Request $request)
{
(new UpdateLocale)->execute([
'user_id' => Auth::user()->id,
'user_id' => Auth::id(),
'locale' => $request->input('locale'),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Helpers\InstanceHelper;
use App\Helpers\NotificationHelper;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class NotificationController extends Controller
{
Expand All @@ -35,7 +36,7 @@ public function index(Request $request, int $companyId): Response
'action' => $notification->action,
'localized_content' => $notification->content,
'read' => $notification->read,
'created_at' => DateHelper::formatShortDateWithTime($notification->created_at, $loggedEmployee->timezone),
'created_at' => DateHelper::formatShortDateWithTime($notification->created_at, Auth::user()->timezone),
]);
}

Expand Down
42 changes: 42 additions & 0 deletions app/Http/Controllers/User/TimezoneController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Helpers\TimezoneHelper;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\User\UpdateTimezone;

class TimezoneController extends Controller
{
/**
* Get all timezones.
*
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
return new JsonResponse(TimezoneHelper::getListOfTimezones(), 200);
}

/**
* Update the user timezone.
*
* @param Request $request
* @return mixed
*/
public function update(Request $request)
{
(new UpdateTimezone)->execute([
'user_id' => Auth::id(),
'timezone' => $request->input('timezone'),
]);

return $request->wantsJson()
? new JsonResponse('', 200)
: back()->with('status', 'timezone-changed');
}
}
6 changes: 3 additions & 3 deletions app/Http/Middleware/CheckCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public function handle($request, Closure $next)
$requestedCompanyId = $request->route()->parameter('company');

try {
$employee = Employee::where('user_id', Auth::user()->id)
$employee = Employee::where('user_id', Auth::id())
->where('company_id', $requestedCompanyId)
->firstOrFail();

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

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

Cache::put($cachedCompanyObject, $employee->company, now()->addMinutes(60));
Cache::put($cachedEmployeeObject, $employee, now()->addMinutes(60));
Expand Down
1 change: 1 addition & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function share(Request $request)
'name' => $request->user()->name,
'show_help' => $request->user()->show_help,
'locale' => $request->user()->locale,
'timezone' => $request->user()->timezone,
] : null,
'company' => $request->user() && ! is_null(InstanceHelper::getLoggedCompany()) ? InstanceHelper::getLoggedCompany() : null,
'employee' => $request->user() && ! is_null(InstanceHelper::getLoggedEmployee()) ? [
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Middleware/ShareInertiaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Inertia\Inertia;
use App\Helpers\LocaleHelper;
use App\Helpers\TimezoneHelper;
use Illuminate\Support\Facades\Session;

/**
Expand All @@ -27,6 +28,7 @@ public function handle($request, $next)
return [
'flash' => $request->session()->get('flash', []),
'languages' => LocaleHelper::getLocaleList(),
'timezones' => TimezoneHelper::getListOfTimezones(),
asbiin marked this conversation as resolved.
Show resolved Hide resolved
'enableSignups' => config('officelife.enable_signups'),
];
},
Expand Down
3 changes: 2 additions & 1 deletion app/Http/ViewHelpers/Adminland/AdminAuditLogViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Helpers\ImageHelper;
use App\Models\Company\Employee;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class AdminAuditLogViewHelper
{
Expand All @@ -29,7 +30,7 @@ public static function index($logs, Employee $loggedEmployee): Collection
'employee' => $author,
]),
],
'localized_audited_at' => DateHelper::formatShortDateWithTime($log->audited_at, $loggedEmployee->timezone),
'localized_audited_at' => DateHelper::formatShortDateWithTime($log->audited_at, Auth::user()->timezone),
]);
}

Expand Down
5 changes: 2 additions & 3 deletions app/Http/ViewHelpers/Adminland/AdminGeneralViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use App\Helpers\DateHelper;
use App\Helpers\ImageHelper;
use App\Models\Company\File;
use App\Models\Company\Company;
use App\Models\Company\Employee;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Money\Currencies\ISOCurrencies;
use Illuminate\Support\Facades\Auth;

class AdminGeneralViewHelper
{
Expand Down Expand Up @@ -45,7 +44,7 @@ public static function information($company, Employee $loggedEmployee): ?array
}

// creation date of the account
$creationDate = DateHelper::formatShortDateWithTime($company->created_at, $loggedEmployee->timezone);
$creationDate = DateHelper::formatShortDateWithTime($company->created_at, Auth::user()->timezone);

// total file sizes
$totalSize = DB::table('files')->where('company_id', $company->id)
Expand Down
3 changes: 2 additions & 1 deletion app/Http/ViewHelpers/Adminland/AdminHardwareViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Company\Employee;
use App\Models\Company\Hardware;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class AdminHardwareViewHelper
{
Expand Down Expand Up @@ -209,7 +210,7 @@ public static function history(Hardware $hardware, Employee $employee): ?Collect

$logsCollection->push([
'id' => $log->id,
'date' => DateHelper::formatDate($log->audited_at, $employee->timezone),
'date' => DateHelper::formatDate($log->audited_at, Auth::user()->timezone),
'sentence' => $sentence,
]);
}
Expand Down
Loading