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

Adding the ability to manage absences for other location users #17

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions app/AbsenceCalendar/EmployeeAbsenceCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function __construct(User $employee, Location $location, CarbonPeriod $pe
$period->getStartDate()->startOfDay(),
$period->getEndDate()->endOfDay()
);

$this->calculateDays();
}

Expand Down Expand Up @@ -113,7 +112,6 @@ protected function diffHoursToStartOrEndOfDay(Carbon $date)
$diffInHours = BigDecimal::of($date->startOfDay()->diffInMinutes($this->endDay))
->dividedBy('60', 2, RoundingMode::HALF_EVEN);
}

return $diffInHours;
}

Expand Down
29 changes: 14 additions & 15 deletions app/Actions/AddAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use App\Models\Location;
use Carbon\CarbonPeriod;
use App\Models\AbsenceType;
use Illuminate\Support\Arr;
use App\Contracts\AddsAbsences;
use App\Formatter\DateFormatter;
use Laravel\Jetstream\Jetstream;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
Expand All @@ -29,11 +29,12 @@ public function __construct()
$this->dateFormatter = app(DateFormatter::class);
}

public function add(User $employee, array $data): void
public function add(User $user, Location $location, int $managingAbsenceForId, array $data)
{
Gate::forUser($employee)->authorize('addAbsence', [
Gate::forUser($user)->authorize('addAbsence', [
Absence::class,
$employee->currentLocation
$managingAbsenceForId,
$location
]);

Validator::make($data, [
Expand All @@ -50,40 +51,38 @@ public function add(User $employee, array $data): void
'full_day' => ['required', 'boolean']
])->validateWithBag('addAbsence');


$startsAt = $this->dateFormatter->timeStrToCarbon($data['starts_at']);
$endsAt = $this->dateFormatter->timeStrToCarbon($data['ends_at']);

//ignore given time if calculation is based on full day
if (isset($data['full_day']) && $data['full_day']) {
$startsAt = $startsAt->copy()->startOfDay();
$endsAt = $endsAt->copy()->endOfDay();

}

$addingAbsenceFor = Jetstream::findUserByIdOrFail($managingAbsenceForId);

$calculator = new AbsenceCalculator(
new EmployeeAbsenceCalendar(
$employee,
$employee->currentLocation,
$addingAbsenceFor,
$location,
new CarbonPeriod($startsAt, $endsAt)
),
AbsenceType::findOrFail($data['absence_type_id'])
);

$absence = $employee->absences()->create(
$absence = $addingAbsenceFor->absences()->create(
[
'location_id' => $employee->currentLocation->id,
'location_id' => $location->id,
'vacation_days' => $calculator->sumVacationDays(),
'paid_hours' => $calculator->sumPaidHours(),
'starts_at' => $startsAt,
'ends_at' => $endsAt,
] + $data
);

$admins = User::all()->filter
->hasLocationRole($employee->currentLocation, 'admin');

Mail::to($admins)
->send(new NewAbsenceWaitingForApproval($absence, $employee));
Mail::to(
$location->allUsers()->filter->hasLocationRole($location, 'admin')
)->send(new NewAbsenceWaitingForApproval($absence, $addingAbsenceFor));
}
}
6 changes: 3 additions & 3 deletions app/Actions/AddLocationMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ protected function rules()
}

/**
* Ensure that the user is not already on the team.
* Ensure that the user is not already on the location.
*
* @param mixed $team
* @param mixed $location
* @param string $email
* @return \Closure
*/
Expand All @@ -129,7 +129,7 @@ protected function ensureUserIsNotAlreadyInLocation($location, string $email)
$validator->errors()->addIf(
$location->hasUserWithEmail($email),
'email',
__('This user already belongs to the team.')
__('This user already belongs to the location.')
);
};
}
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/AddTimeTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ protected function ensureDateIsNotBeforeEmploymentDate($employee, $startsAt)
}
}

protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt) {

protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt)
{
if ($employee->timeTrackings()->where(function ($query) use ($startsAt, $endsAt) {
$query->whereBetween('starts_at', [$startsAt, $endsAt])
->orWhereBetween('ends_at', [$startsAt, $endsAt])
Expand Down
3 changes: 2 additions & 1 deletion app/Actions/ApproveAbscence.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ApproveAbscence implements ApprovesAbsence
* @param int $absenceId
* @return void
*/
public function approve(User $user, $absenceId)
public function approve(User $user, Location $location, $absenceId)
{
Validator::make([
'absence_id' => $absenceId
Expand Down Expand Up @@ -57,6 +57,7 @@ public function bookVacationDays($absence)

//TODO: distribute absence days between available vacation entitlements
$currentVacationEntitlement = $absence->employee->currentVacationEntitlement();

if (!isset($currentVacationEntitlement) || !$currentVacationEntitlement->hasEnoughUnusedVacationDays($absence->vacation_days)) {
throw ValidationException::withMessages([
'error' => [__('Sorry, there is no fitting vacation entitlement for this absence.')],
Expand Down
2 changes: 0 additions & 2 deletions app/Actions/InviteLocationMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public function invite($user, $location, string $email, string $role = null)

$this->validate($location, $email, $role);

// InvitinglocationMember::dispatch($location, $email, $role);

$invitation = $location->locationInvitations()->create([
'email' => $email,
'role' => $role,
Expand Down
15 changes: 10 additions & 5 deletions app/Actions/RemoveAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace App\Actions;

use DB;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use App\Models\Location;
use App\Mail\AbsenceRemoved;
use App\Contracts\RemovesAbsence;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
use DB;

class RemoveAbsence implements RemovesAbsence
{
public function remove($user, $removesAbsenceId)
public function remove(User $user, Location $location, $removesAbsenceId)
{
Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);
tap($location->absences()->whereKey($removesAbsenceId)->first(), function ($absence) use ($user, $location) {

tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {
Gate::forUser($user)->authorize('removeAbsence', [
Absence::class,
$absence,
$location
]);

$absence->delete();

Expand Down
5 changes: 3 additions & 2 deletions app/Contracts/AddsAbsences.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ interface AddsAbsences
/**
* Add a absence for user and location
*
* @param User $employee
* @param User $user
* @param mixed $addingAbsenceForId
* @param array $data
* @return void
*/
public function add(User $employee, array $data) : void;
public function add(User $user, Location $location, int $addingAbsenceForId, array $data);
}
5 changes: 3 additions & 2 deletions app/Contracts/ApprovesAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace App\Contracts;

use App\Models\Absence;
use App\Models\User;
use App\Models\Absence;
use App\Models\Location;

interface ApprovesAbsence
{
public function approve(User $user, Absence $absence);
public function approve(User $user, Location $location, Absence $absence);
}
6 changes: 4 additions & 2 deletions app/Contracts/RemovesAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace App\Contracts;

use App\Models\User;
use App\Models\Location;

interface RemovesAbsence
{
/**
* Remove a absence.
*
* @param mixed $user
* @param User $user
* @param Location $location
* @param mixed $removesAbsenceId
* @return void
*/
public function remove($user, $removesAbsenceId);
public function remove(User $user, Location $location, $removesAbsenceId);
}
4 changes: 1 addition & 3 deletions app/Http/Controllers/AbsenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class AbsenceController extends Controller
{
public function __invoke(Request $request)
{
return view('absences.index', [
'employee' => $request->user()
]);
return view('absences.index');
}
}
Loading