From 6ad31dd8c053b3730555f45a706e5c14361a4832 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Thu, 18 Jun 2026 11:43:16 -0500 Subject: [PATCH] =?UTF-8?q?Add=20FieldSchedulingController=20API=20?= =?UTF-8?q?=E2=80=94=20appointment=20slots,=20booking,=20daily=20schedule,?= =?UTF-8?q?=20tech=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/FieldSchedulingController.php | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php diff --git a/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php b/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php new file mode 100644 index 0000000..75b125c --- /dev/null +++ b/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php @@ -0,0 +1,83 @@ +getIdentity(); + if (!$user || $user->guest || (!$user->authorise('core.admin') && !$user->authorise($action, 'com_mokosuitefield'))) { + http_response_code(403); + echo json_encode(['error' => 'Access denied']); + Factory::getApplication()->close(); + } + } + + public function availableSlots(): void + { + $this->requireAuth(); + $input = Factory::getApplication()->getInput(); + + $slots = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::getAvailableSlots( + $input->getString('date', date('Y-m-d')), + $input->getString('trade', 'general'), + $input->getInt('duration', 60) + ); + + $this->sendJson($slots); + } + + public function bookSlot(): void + { + $this->requireAuth('core.create'); + $input = Factory::getApplication()->getInput(); + + $result = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::scheduleWorkOrder( + $input->getInt('wo_id', 0), + $input->getString('date', ''), + $input->getString('time', ''), + $input->getInt('tech_id', 0) ?: null + ); + + $this->sendJson(['success' => $result]); + } + + public function todaySchedule(): void + { + $this->requireAuth(); + $schedule = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::getTodaySchedule(); + $this->sendJson($schedule); + } + + public function techRoute(): void + { + $this->requireAuth(); + $techId = Factory::getApplication()->getInput()->getInt('tech_id', 0); + $date = Factory::getApplication()->getInput()->getString('date', date('Y-m-d')); + + $route = \Moko\Plugin\System\MokoSuiteField\Helper\RouteHelper::getTechRoute($techId, $date); + $metrics = \Moko\Plugin\System\MokoSuiteField\Helper\RouteHelper::estimateRouteMetrics($techId, $date); + + $this->sendJson(['route' => $route, 'metrics' => $metrics]); + } + + private function sendJson(mixed $data): void + { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); + Factory::getApplication()->close(); + } +} -- 2.52.0