-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathScheduleTemplateController.php
More file actions
91 lines (76 loc) · 2.79 KB
/
ScheduleTemplateController.php
File metadata and controls
91 lines (76 loc) · 2.79 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
<?php
namespace Fleetbase\Http\Controllers\Internal\v1;
use Fleetbase\Http\Controllers\FleetbaseController;
use Fleetbase\Models\Schedule;
use Fleetbase\Models\ScheduleTemplate;
use Fleetbase\Services\Scheduling\ScheduleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ScheduleTemplateController extends FleetbaseController
{
/**
* The resource to query.
*
* @var string
*/
public $resource = 'schedule_template';
/**
* The ScheduleService instance.
*/
protected ScheduleService $scheduleService;
public function __construct(ScheduleService $scheduleService)
{
parent::__construct();
$this->scheduleService = $scheduleService;
}
/**
* Apply a library template to a specific Schedule.
*
* Creates a driver-specific copy of the template linked to the schedule
* and immediately materializes it for the rolling 60-day window.
*
* POST /schedule-templates/{id}/apply
* Body: { "schedule_uuid": "...", "subject_type": "driver", "subject_uuid": "...", "effective_from": "..." }
*/
public function apply(Request $request, string $id): JsonResponse
{
$request->validate([
'schedule_uuid' => 'required|string',
]);
$template = ScheduleTemplate::where('uuid', $id)
->orWhere('public_id', $id)
->firstOrFail();
$schedule = Schedule::where('uuid', $request->input('schedule_uuid'))
->orWhere('public_id', $request->input('schedule_uuid'))
->firstOrFail();
// applyTemplateToSchedule now returns ['template' => $applied, 'items_created' => $count]
$result = $this->scheduleService->applyTemplateToSchedule($template, $schedule);
$applied = $result['template'];
return response()->json([
'status' => 'ok',
'schedule_template' => new \Fleetbase\Http\Resources\ScheduleTemplate($applied),
'items_created' => $result['items_created'],
]);
}
/**
* Manually trigger materialization for a specific applied template.
*
* POST /schedule-templates/{id}/materialize
*/
public function materialize(string $id): JsonResponse
{
$template = ScheduleTemplate::where('uuid', $id)
->orWhere('public_id', $id)
->whereNotNull('schedule_uuid')
->firstOrFail();
$schedule = $template->schedule;
if (!$schedule) {
return response()->json(['error' => 'Template is not applied to any schedule.'], 422);
}
$created = $this->scheduleService->materializeTemplate($template, $schedule);
return response()->json([
'status' => 'ok',
'items_created' => $created,
]);
}
}