55use DB ;
66use Carbon \Carbon ;
77use App \Models \User ;
8+ use App \Models \Location ;
89use Carbon \CarbonPeriod ;
910use Illuminate \Support \Arr ;
1011use App \Formatter \DateFormatter ;
12+ use Laravel \Jetstream \Jetstream ;
1113use App \Facades \PeriodCalculator ;
14+ use Illuminate \Support \Facades \Gate ;
1215use App \Contracts \UpdatesTimeTracking ;
1316use Illuminate \Support \Facades \Validator ;
1417use Illuminate \Validation \ValidationException ;
@@ -22,8 +25,14 @@ public function __construct(DateFormatter $dateFormatter)
2225 $ this ->dateFormatter = $ dateFormatter ;
2326 }
2427
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 )
2629 {
30+ Gate::forUser ($ user )->authorize ('updateTimeTracking ' , [
31+ TimeTracking::class,
32+ $ managingTimeTrackingForId ,
33+ $ location
34+ ]);
35+
2736 Validator::make (array_merge ($ data , [
2837 'time_tracking_id ' => $ timeTrackingId
2938 ]),[
@@ -37,20 +46,21 @@ public function update(User $employee, $timeTrackingId, array $data, array $paus
3746 $ startsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['starts_at ' ]);
3847 $ endsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['ends_at ' ]);
3948
40- $ this ->ensureDateIsNotBeforeEmploymentDate ($ employee , $ startsAt );
49+ $ this ->ensureDateIsNotBeforeEmploymentDate ($ user , $ startsAt );
4150 $ this ->ensureDateIsNotTooFarInTheFuture ($ endsAt );
42- $ this ->ensureGivenTimeIsNotOverlappingWithExisting ($ employee , $ startsAt , $ endsAt , $ timeTrackingId );
51+ $ this ->ensureGivenTimeIsNotOverlappingWithExisting ($ user , $ startsAt , $ endsAt , $ timeTrackingId );
4352
4453 $ this ->validatePauseTimes (
4554 PeriodCalculator::fromTimesArray ($ pauseTimes ),
4655 $ startsAt ,
4756 $ endsAt
4857 );
4958
50- $ trackedTime = $ employee -> currentLocation ->timeTrackings ()->whereKey ($ timeTrackingId )->first ();
59+ $ trackedTime = $ location ->timeTrackings ()->whereKey ($ timeTrackingId )->first ();
5160
52- DB ::transaction (function () use ($ trackedTime , $ startsAt , $ endsAt , $ data , $ pauseTimes ) {
61+ DB ::transaction (function () use ($ trackedTime , $ startsAt , $ endsAt , $ data , $ pauseTimes, $ managingTimeTrackingForId ) {
5362 $ trackedTime ->update (array_merge ([
63+ 'user_id ' => $ managingTimeTrackingForId ,
5464 'starts_at ' => $ startsAt ,
5565 'ends_at ' => $ endsAt ,
5666 ], Arr::except ($ data , ['starts_at ' ,'ends_at ' ,'time_tracking_id ' ])));
@@ -85,19 +95,19 @@ protected function ensureDateIsNotTooFarInTheFuture($endsAt)
8595 }
8696 }
8797
88- protected function ensureDateIsNotBeforeEmploymentDate ($ employee , $ startsAt )
98+ protected function ensureDateIsNotBeforeEmploymentDate ($ user , $ startsAt )
8999 {
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 )) {
92102 throw ValidationException::withMessages ([
93103 'date ' => [ __ ('Date should not before employment date. ' ) ],
94104 ])->errorBag ('addTimeTracking ' );
95105 }
96106 }
97107 }
98108
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 ) {
101111 $ query ->whereBetween ('starts_at ' , [$ startsAt , $ endsAt ])
102112 ->orWhereBetween ('ends_at ' , [$ startsAt , $ endsAt ])
103113 ->orWhere (function ($ query ) use ($ startsAt , $ endsAt ) {
0 commit comments