-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathScheduleExceptionController.php
More file actions
98 lines (80 loc) · 2.86 KB
/
ScheduleExceptionController.php
File metadata and controls
98 lines (80 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Fleetbase\Http\Controllers\Internal\v1;
use Fleetbase\Http\Controllers\FleetbaseController;
use Fleetbase\Models\ScheduleException;
use Fleetbase\Services\Scheduling\ScheduleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ScheduleExceptionController extends FleetbaseController
{
/**
* The resource to query.
*
* @var string
*/
public $resource = 'schedule_exception';
/**
* The ScheduleService instance.
*/
protected ScheduleService $scheduleService;
public function __construct(ScheduleService $scheduleService)
{
parent::__construct();
$this->scheduleService = $scheduleService;
}
/**
* Approve a schedule exception.
* This will also cancel any generated ScheduleItems that fall within the exception's date range.
*
* POST /schedule-exceptions/{id}/approve
*/
public function approve(string $id): JsonResponse
{
$exception = ScheduleException::where('uuid', $id)
->orWhere('public_id', $id)
->firstOrFail();
$reviewerUuid = auth()->user()?->uuid;
$exception = $this->scheduleService->approveException($exception, $reviewerUuid);
return response()->json([
'status' => 'ok',
'schedule_exception' => new \Fleetbase\Http\Resources\ScheduleException($exception),
]);
}
/**
* Reject a schedule exception.
*
* POST /schedule-exceptions/{id}/reject
*/
public function reject(string $id): JsonResponse
{
$exception = ScheduleException::where('uuid', $id)
->orWhere('public_id', $id)
->firstOrFail();
$reviewerUuid = auth()->user()?->uuid;
$exception = $this->scheduleService->rejectException($exception, $reviewerUuid);
return response()->json([
'status' => 'ok',
'schedule_exception' => new \Fleetbase\Http\Resources\ScheduleException($exception),
]);
}
/**
* Get all exceptions for a specific subject.
*
* GET /schedule-exceptions?subject_type=driver&subject_uuid={uuid}
*/
public function forSubject(Request $request): JsonResponse
{
$subjectType = $request->input('subject_type');
$subjectUuid = $request->input('subject_uuid');
if (!$subjectType || !$subjectUuid) {
return response()->json([
'error' => 'subject_type and subject_uuid are required',
], 422);
}
$filters = $request->only(['status', 'type', 'start_at', 'end_at']);
$exceptions = $this->scheduleService->getExceptionsForSubject($subjectType, $subjectUuid, $filters);
return response()->json([
'schedule_exceptions' => \Fleetbase\Http\Resources\ScheduleException::collection($exceptions),
]);
}
}