diff --git a/Admin/Routes/Web/Api.php b/Admin/Routes/Web/Api.php index 8b29d68..415e354 100644 --- a/Admin/Routes/Web/Api.php +++ b/Admin/Routes/Web/Api.php @@ -29,4 +29,15 @@ return [ ], ], ], + '^.*/notification(\?.*|$)' => [ + [ + 'dest' => '\Modules\Notification\Controller\ApiController:apiNotificationsGet', + 'verb' => RouteVerb::GET, + 'permission' => [ + 'module' => ApiController::NAME, + 'type' => PermissionType::READ, + 'state' => PermissionCategory::NOTIFICATION, + ], + ], + ], ]; diff --git a/Controller/ApiController.php b/Controller/ApiController.php index d66de41..607b8a8 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -16,8 +16,10 @@ namespace Modules\Notification\Controller; use Modules\Notification\Models\NotificationMapper; use phpOMS\DataStorage\Database\Query\OrderType; +use phpOMS\Message\NotificationLevel; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; +use phpOMS\System\MimeType; /** * Api controller for the tasks module. @@ -61,4 +63,37 @@ final class ApiController extends Controller $this->createStandardUpdateResponse($request, $response, []); } + + /** + * Api method to create a task + * + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param array $data Generic data + * + * @return void + * + * @api + * + * @since 1.0.0 + */ + public function apiNotificationsGet(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void + { + $now = new \DateTimeImmutable('now'); + $notifications = NotificationMapper::getAll() + ->where('createdFor', $request->header->account) + ->where('seenAt', null) + ->where('createdAt', $now, '<') // Don't show pre-created notifications + ->where('createdAt', $request->getDataDateTime('start') ?? $now->modify('-1 hour'), '>') + ->sort('createdAt', OrderType::ASC) + ->execute(); + + $response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true); + $response->data[$request->uri->__toString()] = [ + 'status' => NotificationLevel::OK, + 'title' => 'New Notification', + 'message' => 'You have new notifications', + 'response' => $notifications, + ]; + } }