5
5
use DB ;
6
6
use Carbon \Carbon ;
7
7
use App \Models \User ;
8
+ use App \Models \Location ;
8
9
use Carbon \CarbonPeriod ;
9
10
use Illuminate \Support \Arr ;
10
11
use App \Formatter \DateFormatter ;
12
+ use Laravel \Jetstream \Jetstream ;
11
13
use App \Facades \PeriodCalculator ;
14
+ use Illuminate \Support \Facades \Gate ;
12
15
use App \Contracts \UpdatesTimeTracking ;
13
16
use Illuminate \Support \Facades \Validator ;
14
17
use Illuminate \Validation \ValidationException ;
@@ -22,8 +25,14 @@ public function __construct(DateFormatter $dateFormatter)
22
25
$ this ->dateFormatter = $ dateFormatter ;
23
26
}
24
27
25
- public function update (User $ employee , $ timeTrackingId , array $ data , array $ pauseTimes )
28
+ public function update (User $ user , Location $ location , int $ managingTimeTrackingForId , int $ timeTrackingId , array $ data , array $ pauseTimes )
26
29
{
30
+ Gate::forUser ($ user )->authorize ('updateTimeTracking ' , [
31
+ TimeTracking::class,
32
+ $ managingTimeTrackingForId ,
33
+ $ location
34
+ ]);
35
+
27
36
Validator::make (array_merge ($ data , [
28
37
'time_tracking_id ' => $ timeTrackingId
29
38
]),[
@@ -37,20 +46,21 @@ public function update(User $employee, $timeTrackingId, array $data, array $paus
37
46
$ startsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['starts_at ' ]);
38
47
$ endsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['ends_at ' ]);
39
48
40
- $ this ->ensureDateIsNotBeforeEmploymentDate ($ employee , $ startsAt );
49
+ $ this ->ensureDateIsNotBeforeEmploymentDate ($ user , $ startsAt );
41
50
$ this ->ensureDateIsNotTooFarInTheFuture ($ endsAt );
42
- $ this ->ensureGivenTimeIsNotOverlappingWithExisting ($ employee , $ startsAt , $ endsAt , $ timeTrackingId );
51
+ $ this ->ensureGivenTimeIsNotOverlappingWithExisting ($ user , $ startsAt , $ endsAt , $ timeTrackingId );
43
52
44
53
$ this ->validatePauseTimes (
45
54
PeriodCalculator::fromTimesArray ($ pauseTimes ),
46
55
$ startsAt ,
47
56
$ endsAt
48
57
);
49
58
50
- $ trackedTime = $ employee -> currentLocation ->timeTrackings ()->whereKey ($ timeTrackingId )->first ();
59
+ $ trackedTime = $ location ->timeTrackings ()->whereKey ($ timeTrackingId )->first ();
51
60
52
- DB ::transaction (function () use ($ trackedTime , $ startsAt , $ endsAt , $ data , $ pauseTimes ) {
61
+ DB ::transaction (function () use ($ trackedTime , $ startsAt , $ endsAt , $ data , $ pauseTimes, $ managingTimeTrackingForId ) {
53
62
$ trackedTime ->update (array_merge ([
63
+ 'user_id ' => $ managingTimeTrackingForId ,
54
64
'starts_at ' => $ startsAt ,
55
65
'ends_at ' => $ endsAt ,
56
66
], Arr::except ($ data , ['starts_at ' ,'ends_at ' ,'time_tracking_id ' ])));
@@ -85,19 +95,19 @@ protected function ensureDateIsNotTooFarInTheFuture($endsAt)
85
95
}
86
96
}
87
97
88
- protected function ensureDateIsNotBeforeEmploymentDate ($ employee , $ startsAt )
98
+ protected function ensureDateIsNotBeforeEmploymentDate ($ user , $ startsAt )
89
99
{
90
- if ($ employee ->date_of_employment ) {
91
- if ($ startsAt ->isBefore ($ employee ->date_of_employment )) {
100
+ if ($ user ->date_of_employment ) {
101
+ if ($ startsAt ->isBefore ($ user ->date_of_employment )) {
92
102
throw ValidationException::withMessages ([
93
103
'date ' => [ __ ('Date should not before employment date. ' ) ],
94
104
])->errorBag ('addTimeTracking ' );
95
105
}
96
106
}
97
107
}
98
108
99
- protected function ensureGivenTimeIsNotOverlappingWithExisting ($ employee , $ startsAt , $ endsAt , $ timeTrackingId ) {
100
- if ($ employee ->timeTrackings ()->where (function ($ query ) use ($ startsAt , $ endsAt , $ timeTrackingId ) {
109
+ protected function ensureGivenTimeIsNotOverlappingWithExisting ($ user , $ startsAt , $ endsAt , $ timeTrackingId ) {
110
+ if ($ user ->timeTrackings ()->where (function ($ query ) use ($ startsAt , $ endsAt , $ timeTrackingId ) {
101
111
$ query ->whereBetween ('starts_at ' , [$ startsAt , $ endsAt ])
102
112
->orWhereBetween ('ends_at ' , [$ startsAt , $ endsAt ])
103
113
->orWhere (function ($ query ) use ($ startsAt , $ endsAt ) {
0 commit comments