Skip to content

Commit 9699082

Browse files
authored
Merge pull request #5 from eporsche/code_refactoring
Code refactoring and small bug fixes
2 parents eae388c + 108d444 commit 9699082

24 files changed

+258
-78
lines changed

app/Actions/AddAbsence.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function __construct()
2929
$this->dateFormatter = app(DateFormatter::class);
3030
}
3131

32-
public function add(User $employee, Location $location, array $data): void
32+
public function add(User $employee, array $data): void
3333
{
3434
Gate::forUser($employee)->authorize('addAbsence', [
3535
Absence::class,
36-
$location
36+
$employee->currentLocation
3737
]);
3838

3939
Validator::make($data, [
@@ -64,15 +64,15 @@ public function add(User $employee, Location $location, array $data): void
6464
$calculator = new AbsenceCalculator(
6565
new EmployeeAbsenceCalendar(
6666
$employee,
67-
$location,
67+
$employee->currentLocation,
6868
new CarbonPeriod($startsAt, $endsAt)
6969
),
7070
AbsenceType::findOrFail($data['absence_type_id'])
7171
);
7272

7373
$absence = $employee->absences()->create(
7474
[
75-
'location_id' => $location->id,
75+
'location_id' => $employee->currentLocation->id,
7676
'vacation_days' => $calculator->sumVacationDays(),
7777
'paid_hours' => $calculator->sumPaidHours(),
7878
'starts_at' => $startsAt,
@@ -81,7 +81,7 @@ public function add(User $employee, Location $location, array $data): void
8181
);
8282

8383
$admins = User::all()->filter
84-
->hasLocationRole($location, 'admin');
84+
->hasLocationRole($employee->currentLocation, 'admin');
8585

8686
Mail::to($admins)
8787
->send(new NewAbsenceWaitingForApproval($absence, $employee));

app/Actions/AddTimeTracking.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ public function add($employee, array $data, array $pauseTimes)
5151
], Arr::except($data, ['starts_at','ends_at'])));
5252

5353
$trackedTime->pauseTimes()->createMany($pauseTimes);
54-
5554
$trackedTime->updatePauseTime();
5655
});
57-
5856
}
5957

6058
protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $endsAt)
@@ -70,13 +68,6 @@ protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $en
7068
});
7169
}
7270

73-
protected function calculatePauseTimeFromDefaultRestingTimes($employee, $workingTimeInSeconds)
74-
{
75-
return optional(
76-
$employee->defaultRestingTimes()->firstWhere('min_hours','<=',$workingTimeInSeconds)
77-
)->duration->inSeconds();
78-
}
79-
8071
protected function ensureDateIsNotTooFarInTheFuture($endsAt)
8172
{
8273
if ($endsAt->isAfter(Carbon::now()->endOfDay())) {

app/Actions/ApproveAbscence.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ApproveAbscence implements ApprovesAbsence
2727
* @param int $absenceId
2828
* @return void
2929
*/
30-
public function approve(User $user, Location $location, $absenceId)
30+
public function approve(User $user, $absenceId)
3131
{
3232
Validator::make([
3333
'absence_id' => $absenceId
@@ -37,9 +37,9 @@ public function approve(User $user, Location $location, $absenceId)
3737

3838
$absence = Absence::findOrFail($absenceId);
3939

40-
DB::transaction(function () use ($absence, $location) {
40+
DB::transaction(function () use ($absence, $user) {
4141
$this->bookVacationDays($absence);
42-
$this->createAbsenceIndex($absence, $location);
42+
$this->createAbsenceIndex($absence, $user->currentLocation);
4343
$absence->markAsConfirmed();
4444
if (Daybreak::hasCaldavFeature()) {
4545
CreateCaldavEvent::dispatch($absence)

app/Actions/RemoveAbsence.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
namespace App\Actions;
44

55
use App\Models\User;
6+
use Illuminate\Support\Facades\Gate;
67
use App\Mail\AbsenceRemoved;
78
use App\Contracts\RemovesAbsence;
89
use Illuminate\Support\Facades\Mail;
10+
use DB;
911

1012
class RemoveAbsence implements RemovesAbsence
1113
{
12-
public function remove(User $employee, $absenceId)
14+
public function remove($user, $removesAbsenceId)
1315
{
14-
$employee->absences()->whereKey($absenceId)->delete();
16+
Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);
1517

16-
Mail::to($employee)->send(new AbsenceRemoved());
18+
tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {
19+
20+
$absence->delete();
21+
22+
Mail::to($absence->employee)->send(new AbsenceRemoved());
23+
});
1724
}
1825
}

app/Actions/RemoveTimeTracking.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class RemoveTimeTracking implements RemovesTimeTracking
99
{
1010
public function remove(User $user, $timeTrackingId)
1111
{
12-
$user->timeTrackings()->whereKey($timeTrackingId)->delete();
12+
$user->currentLocation->timeTrackings()->whereKey($timeTrackingId)->delete();
1313
}
1414
}

app/Actions/UpdateTimeTracking.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ public function update(User $employee, $timeTrackingId, array $data, array $paus
4141
$this->ensureDateIsNotTooFarInTheFuture($endsAt);
4242
$this->ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt, $timeTrackingId);
4343

44-
//check pause times
45-
4644
$this->validatePauseTimes(
4745
PeriodCalculator::fromTimesArray($pauseTimes),
4846
$startsAt,
4947
$endsAt
5048
);
5149

52-
$trackedTime = $employee->timeTrackings()->whereKey($timeTrackingId)->first();
50+
$trackedTime = $employee->currentLocation->timeTrackings()->whereKey($timeTrackingId)->first();
5351

5452
DB::transaction(function () use ($trackedTime, $startsAt, $endsAt, $data, $pauseTimes) {
5553
$trackedTime->update(array_merge([

app/Contracts/AddsAbsences.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ interface AddsAbsences
1111
* Add a absence for user and location
1212
*
1313
* @param User $employee
14-
* @param Location $location
1514
* @param array $data
1615
* @return void
1716
*/
18-
public function add(User $employee, Location $location, array $data) : void;
17+
public function add(User $employee, array $data) : void;
1918
}

app/Contracts/ApprovesAbsence.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace App\Contracts;
44

55
use App\Models\Absence;
6-
use App\Models\Location;
76
use App\Models\User;
87

98
interface ApprovesAbsence
109
{
11-
public function approve(User $user, Location $location, Absence $absence);
10+
public function approve(User $user, Absence $absence);
1211
}

app/Contracts/RemovesAbsence.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ interface RemovesAbsence
1313
* @param mixed $removesAbsenceId
1414
* @return void
1515
*/
16-
public function remove(User $employee, $removesAbsenceId);
16+
public function remove($user, $removesAbsenceId);
1717
}

app/Formatter/DateFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function formatDateForView($date);
2121

2222
public function formatDateTimeForView($date);
2323

24-
public function generateTimeStr(string $date, string $hours = null, string $minutes = null);
24+
public function generateTimeStr(string $date = null, string $hours = null, string $minutes = null);
2525

2626
public function timeStrToCarbon(string $timeStr) : CarbonImmutable;
2727

0 commit comments

Comments
 (0)