feat: FieldSchedulingController API #16
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace Moko\Component\MokoSuiteField\Api\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Controller\BaseController;
|
||||
|
||||
/**
|
||||
* Scheduling + Daily Route API.
|
||||
*
|
||||
* GET /schedule/slots — Available appointment slots
|
||||
* POST /schedule/book — Schedule a work order
|
||||
* GET /schedule/today — Today's schedule for all techs
|
||||
* GET /schedule/tech/{id} — Single tech's daily route
|
||||
*/
|
||||
class FieldSchedulingController extends BaseController
|
||||
{
|
||||
private function requireAuth(string $action = 'core.manage'): void
|
||||
{
|
||||
$user = Factory::getApplication()->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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user