src/Controller/NotificationController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\RedisCache;
  4. use App\Service\AlertService;
  5. use App\Service\UserPermission;
  6. use App\Model\OrganizationModel;
  7. use Knp\Component\Pager\Paginator;
  8. use Pimcore\Log\ApplicationLogger;
  9. use Pimcore\Model\DataObject\Tags;
  10. use App\Model\CustomNotificationLog;
  11. use App\Service\NCMWeatherAPIService;
  12. use App\Model\CustomNotificationModel;
  13. use Pimcore\Model\DataObject\Customer;
  14. use Pimcore\Model\DataObject\Severity;
  15. use Symfony\Component\Process\Process;
  16. use Pimcore\Model\DataObject\Parameters;
  17. use App\Service\CustomNotificationService;
  18. use App\Service\MeteomaticsWeatherService;
  19. use Pimcore\Controller\FrontendController;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use App\Model\CustomNotificationConfigModel;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Pimcore\Model\DataObject\CustomNotificationTags;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. use Symfony\Component\Security\Core\User\UserInterface;
  28. use Symfony\Component\Process\Exception\ProcessFailedException;
  29. use Pimcore\Model\DataObject\Fieldcollection\Data\NotificationRule;
  30. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  31. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  32. use App\Model\EwsNotificationModel;
  33. use Symfony\Contracts\HttpClient\HttpClientInterface;
  34. /**
  35.  * Notification controller is responsible for handling various actions related to notificaiton, subscription and alerts sub management.
  36.  *
  37.  * @Route("/api/ncm/notification")
  38.  */
  39. class NotificationController extends FrontendController
  40. {
  41.     private $lang;
  42.     protected $organizationModel;
  43.     protected $customNotificationModel;
  44.     private $ewsNotificationModel;
  45.     private $httpClient;
  46.     public function __construct(
  47.         private TokenStorageInterface $tokenStorageInterface,
  48.         private JWTTokenManagerInterface $jwtManager,
  49.         private UserPermission $userPermission,
  50.         protected TranslatorInterface $translator,
  51.         private MeteomaticsWeatherService $MeteomaticsWeatherService,
  52.         private CustomNotificationService $customNotificationService,
  53.         private AlertService $alertService,
  54.         private NCMWeatherAPIService $ncmWeatherApiService,
  55.         private ApplicationLogger $logger,
  56.         RedisCache $redisCache,
  57.         HttpClientInterface $httpClient,
  58.         private MeteomaticsWeatherService $mateoMaticsService
  59.     ) {
  60.         header('Content-Type: application/json; charset=UTF-8');
  61.         header("Access-Control-Allow-Origin: *");
  62.         $this->organizationModel = new OrganizationModel();
  63.         $this->customNotificationModel = new CustomNotificationConfigModel();
  64.         $this->ncmWeatherApiService = new NCMWeatherAPIService($redisCache$httpClient);
  65.         $this->ewsNotificationModel = new EwsNotificationModel();
  66.         
  67.     }
  68.     /**
  69.      * @Route("/subscribe-custom-notification", methods={"POST"})
  70.      */
  71.     public function subscribeCustomNotification(Request $requestUserInterface $user): JsonResponse
  72.     {
  73.         try {
  74.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  75.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  76.                 return $this->json($permissions401);
  77.             } elseif ($permissions['success'] === false) {
  78.                 return $this->json($permissions);
  79.             }
  80.             $params json_decode($request->getContent(), true);
  81.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  82.             $requiredParameters = ['user_id''location_id''alert_name''title''color''units''min_value''max_value''calculate''weather_model''alert_on_email''alert_on_sms'];
  83.             foreach ($requiredParameters as $param) {
  84.                 if (!isset($params[$param])) {
  85.                     $missingParams[] = $param;
  86.                 }
  87.             }
  88.             if (!empty($missingParams)) {
  89.                 // Throw an exception with a message that includes the missing parameters
  90.                 $parameterList implode(", "$missingParams);
  91.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  92.             }
  93.             $locationId $params['location_id'];
  94.             $alertType $params['alert_name'];
  95.             $title $params['title'];
  96.             $color $params['color'];
  97.             $units $params['units'];
  98.             $minValue = isset($params['min_value']) ? $params['min_value'] : 0;
  99.             $maxValue $params['max_value'];
  100.             $weatherModel $params['weather_model'];
  101.             $alertOnEmail $params['alert_on_email'];
  102.             $alertOnSMS $params['alert_on_sms'];
  103.             $calculate $params['calculate'];
  104.             $duration = isset($params['duration']) ? $params['duration'] : 1;
  105.             $frequency = isset($params['frequency']) ? $params['frequency'] : 1;
  106.             if (!in_array($calculatearray_flip(CUSTOM_NOTIFICATION_RANGE))) {
  107.                 return $this->json(['success' => false'message' => $this->translator->trans('invalid_calculation_type')]);
  108.             }
  109.             
  110.             $result $this->customNotificationService->subscribeAlerts(
  111.                 $user,
  112.                 $locationId,
  113.                 $alertType,
  114.                 $title,
  115.                 $color,
  116.                 $units,
  117.                 $minValue,
  118.                 $maxValue,
  119.                 $weatherModel,
  120.                 $alertOnEmail,
  121.                 $alertOnSMS,
  122.                 $calculate,
  123.                 $duration,
  124.                 $frequency,
  125.                 $this->logger,
  126.                 $this->translator,
  127.                 $this->mateoMaticsService
  128.             );
  129.             return $this->json($result);
  130.         } catch (\Exception $ex) {
  131.             $this->logger->error($ex->getMessage());
  132.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  133.         }
  134.     }
  135.     /**
  136.      * @Route("/subscribe-custom-notification-by-tag", methods={"POST"})
  137.      */
  138.     public function subscribeCustomNotificationByTag(Request $requestUserInterface $user): JsonResponse
  139.     {
  140.         try {
  141.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  142.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  143.                 return $this->json($permissions401);
  144.             } elseif ($permissions['success'] === false) {
  145.                 return $this->json($permissions);
  146.             }
  147.             $params json_decode($request->getContent(), true);
  148.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  149.             $requiredParameters = ['user_id''tag_id''alert_name''title''color''units''min_value''max_value''calculate''weather_model''alert_on_email''alert_on_sms'];
  150.             foreach ($requiredParameters as $param) {
  151.                 if (!isset($params[$param])) {
  152.                     $missingParams[] = $param;
  153.                 }
  154.             }
  155.             if (!empty($missingParams)) {
  156.                 // Throw an exception with a message that includes the missing parameters
  157.                 $parameterList implode(", "$missingParams);
  158.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  159.             }
  160.             $tagId $params['tag_id'];
  161.             $alertType $params['alert_name'];
  162.             $title $params['title'];
  163.             $color $params['color'];
  164.             $units $params['units'];
  165.             $minValue = isset($params['min_value']) ? $params['min_value'] : 0;
  166.             $maxValue $params['max_value'];
  167.             $weatherModel $params['weather_model'];
  168.             $alertOnEmail $params['alert_on_email'];
  169.             $alertOnSMS $params['alert_on_sms'];
  170.             $calculate $params['calculate'];
  171.             $duration = isset($params['duration']) ? $params['duration'] : 1;
  172.             $frequency = isset($params['frequency']) ? $params['frequency'] : 1;
  173.             if (!in_array($calculatearray_flip(CUSTOM_NOTIFICATION_RANGE))) {
  174.                 return $this->json(['success' => false'message' => $this->translator->trans('invalid_calculation_type')]);
  175.             }
  176.             $result $this->customNotificationService->subscribeAlertsByTag(
  177.                 $user,
  178.                 $tagId,
  179.                 $alertType,
  180.                 $title,
  181.                 $color,
  182.                 $units,
  183.                 $minValue,
  184.                 $maxValue,
  185.                 $weatherModel,
  186.                 $alertOnEmail,
  187.                 $alertOnSMS,
  188.                 $calculate,
  189.                 $duration,
  190.                 $frequency,
  191.                 $this->logger,
  192.                 $this->translator,
  193.                 $this->mateoMaticsService
  194.             );
  195.             return $this->json($result);
  196.         } catch (\Exception $ex) {
  197.             $this->logger->error($ex->getMessage());
  198.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  199.         }
  200.     }
  201.     /**
  202.      * @Route("/update-subscribe-custom-notification", methods={"POST"})
  203.      */
  204.     public function updateSubscribeCustomNotification(Request $requestUserInterface $user): JsonResponse
  205.     {
  206.         try {
  207.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  208.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  209.                 return $this->json($permissions401);
  210.             } elseif ($permissions['success'] === false) {
  211.                 return $this->json($permissions);
  212.             }
  213.             $params json_decode($request->getContent(), true);
  214.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  215.             $requiredParameters = ['user_id''id''location_id''alert_name''title''color''units''min_value''max_value''calculate''weather_model''alert_on_email''alert_on_sms'];
  216.             foreach ($requiredParameters as $param) {
  217.                 if (!isset($params[$param])) {
  218.                     $missingParams[] = $param;
  219.                 }
  220.             }
  221.             if (!empty($missingParams)) {
  222.                 // Throw an exception with a message that includes the missing parameters
  223.                 $parameterList implode(", "$missingParams);
  224.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  225.             }
  226.             $notificationId $params['id'];
  227.             $locationId $params['location_id'];
  228.             $alertType $params['alert_name'];
  229.             $title $params['title'];
  230.             $color $params['color'];
  231.             $units $params['units'];
  232.             $minValue = isset($params['min_value']) ? $params['min_value'] : 0;
  233.             $maxValue $params['max_value'];
  234.             $weatherModel $params['weather_model'];
  235.             $alertOnEmail $params['alert_on_email'];
  236.             $alertOnSMS $params['alert_on_sms'];
  237.             $calculate $params['calculate'];
  238.             $duration = isset($params['duration']) ? $params['duration'] : 1;
  239.             $frequency = isset($params['frequency']) ? $params['frequency'] : 1;
  240.             if (!in_array($calculatearray_flip(CUSTOM_NOTIFICATION_RANGE))) {
  241.                 return $this->json(['success' => false'message' => $this->translator->trans('invalid_calculation_type')]);
  242.             }
  243.             $result $this->customNotificationService->updateSubscribeAlerts(
  244.                 $user,
  245.                 $notificationId,
  246.                 $locationId,
  247.                 $alertType,
  248.                 $title,
  249.                 $color,
  250.                 $units,
  251.                 $minValue,
  252.                 $maxValue,
  253.                 $weatherModel,
  254.                 $alertOnEmail,
  255.                 $alertOnSMS,
  256.                 $calculate,
  257.                 $duration,
  258.                 $frequency,
  259.                 $this->translator,
  260.                 $this->mateoMaticsService
  261.             );
  262.             return $this->json($result);
  263.         } catch (\Exception $ex) {
  264.             $this->logger->error($ex->getMessage());
  265.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  266.         }
  267.     }
  268.     /**
  269.      * @Route("/unsubscribe-custom-notification", methods={"POST"})
  270.      */
  271.     public function unsubscribeCustomNotification(Request $requestUserInterface $user): JsonResponse
  272.     {
  273.         try {
  274.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  275.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  276.                 return $this->json($permissions401);
  277.             } elseif ($permissions['success'] === false) {
  278.                 return $this->json($permissions);
  279.             }
  280.             $params json_decode($request->getContent(), true);
  281.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  282.             $requiredParameters = ['user_id''location_id''alert_name'];
  283.             foreach ($requiredParameters as $param) {
  284.                 if (!isset($params[$param])) {
  285.                     $missingParams[] = $param;
  286.                 }
  287.             }
  288.             if (!empty($missingParams)) {
  289.                 // Throw an exception with a message that includes the missing parameters
  290.                 $parameterList implode(", "$missingParams);
  291.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  292.             }
  293.             $locationId $params['location_id'];
  294.             $alertType $params['alert_name'];
  295.             $result $this->customNotificationService->unSubscribeAlerts($user$locationId$alertType$this->translator);
  296.             return $this->json($result);
  297.         } catch (\Exception $ex) {
  298.             $this->logger->error($ex->getMessage());
  299.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  300.         }
  301.     }
  302.     /**
  303.      * @Route("/unsubscribe-custom-notification-bulk", methods={"POST"})
  304.      */
  305.     public function unsubscribeCustomNotificationBulk(Request $requestUserInterface $user): JsonResponse
  306.     {
  307.         try {
  308.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  309.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  310.                 return $this->json($permissions401);
  311.             } elseif ($permissions['success'] === false) {
  312.                 return $this->json($permissions);
  313.             }
  314.             if (!$user) {
  315.                 // throw new \Exception('User is not authenticated');
  316.                 return $this->json(['success' => false'message' => $this->translator->trans('User is not authenticated')]);
  317.             }
  318.             
  319.             $params json_decode($request->getContent(), true);
  320.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  321.             $requiredParameters = ['notification_id'];
  322.             foreach ($requiredParameters as $param) {
  323.                 if (!isset($params[$param])) {
  324.                     $missingParams[] = $param;
  325.                 }
  326.             }
  327.             if (!empty($missingParams)) {
  328.                 // Throw an exception with a message that includes the missing parameters
  329.                 $parameterList implode(", "$missingParams);
  330.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  331.             }
  332.             $notificationId $params['notification_id'];
  333.             $result $this->customNotificationService->unSubscribeAlertsBulk($user$notificationId$this->translator);
  334.             return $this->json($result);
  335.         } catch (\Exception $ex) {
  336.             $this->logger->error($ex->getMessage());
  337.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  338.         }
  339.     }
  340.     /**
  341.      * @Route("/alert-master", methods={"POST"})
  342.      */
  343.     public function alertMaster(Request $requestUserInterface $user): JsonResponse
  344.     {
  345.         try {
  346.             $params json_decode($request->getContent(), true);
  347.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  348.             if (!$user) {
  349.                 throw new \Exception('User is not authenticated');
  350.             }
  351.             $result $this->customNotificationService->getAlertMaster();
  352.             return $this->json($result);
  353.         } catch (\Exception $ex) {
  354.             $this->logger->error($ex->getMessage());
  355.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  356.         }
  357.     }
  358.     /**
  359.      * @Route("/manned-alert-filters", methods={"POST"})
  360.      */
  361.     public function mannedAlertFilters(Request $request): JsonResponse
  362.     {
  363.         try {
  364.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  365.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  366.                 return $this->json($permissions401);
  367.             } elseif ($permissions['success'] === false) {
  368.                 return $this->json($permissions);
  369.             }
  370.             $params json_decode($request->getContent(), true);
  371.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  372.             $result $this->customNotificationService->mannedAlertFilters();
  373.             return $this->json($result);
  374.         } catch (\Exception $ex) {
  375.             $this->logger->error($ex->getMessage());
  376.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  377.         }
  378.     }
  379.     /**
  380.      * @Route("/manned-alerts", methods={"POST"})
  381.      */
  382.     public function mannedAlerts(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  383.     {
  384.         try {
  385.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  386.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  387.                 return $this->json($permissions401);
  388.             } elseif ($permissions['success'] === false) {
  389.                 return $this->json($permissions);
  390.             }
  391.             if (!$user) {
  392.                 throw new \Exception('User is not authenticated');
  393.             }
  394.             $params json_decode($request->getContent(), true);
  395.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  396.             if (!isset($params['from_date']) || !isset($params['to_date'])) {
  397.                 // throw new \Exception('Date range is required');
  398.                 return $this->json(['success' => false'message' =>  $this->translator->trans('date_range_is_required')]);
  399.             }
  400.             $result $this->customNotificationService->getMannedAlerts($params$paginator);
  401.             return $this->json($result);
  402.         } catch (\Exception $ex) {
  403.             $this->logger->error($ex->getMessage());
  404.             return $this->json(['success' => false'message' =>  $this->translator->trans(USER_ERROR_MESSAGE)]);
  405.         }
  406.     }
  407.     /**
  408.      * @Route("/manned-alerts-direct", methods={"GET"})
  409.      */
  410.     public function mannedAlertsDirect(Request $request): JsonResponse
  411.     {
  412.         $params json_decode($request->getContent(), true);
  413.         $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  414.         return $this->json($this->ncmWeatherApiService->getAlerts());
  415.     }
  416.     /**
  417.      * @Route("/get-custom-notification", methods={"POST"})
  418.      */
  419.     public function getCustomNotification(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  420.     {
  421.         try {
  422.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  423.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  424.                 return $this->json($permissions401);
  425.             } elseif ($permissions['success'] === false) {
  426.                 return $this->json($permissions);
  427.             }
  428.             if (!$user) {
  429.                 // throw new \Exception('User is not authenticated');
  430.                 return $this->json(['success' => false'message' => $this->translator->trans('User is not authenticated')]);
  431.             }
  432.             $params json_decode($request->getContent(), true);
  433.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  434.             if (!isset($params['location_id'])) {
  435.                 //throw new \Exception('Location is mandatory');
  436.                 return $this->json(['success' => false'message' => $this->translator->trans('location_is_mandatory')]);
  437.             }
  438.             $locationId $params['location_id'];
  439.             $result $this->customNotificationService->getUserAlerts($user->getId(), $locationId$params$paginator);
  440.             return $this->json($result);
  441.         } catch (\Exception $ex) {
  442.             $this->logger->error($ex->getMessage());
  443.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  444.         }
  445.     }
  446.     /**
  447.      * @Route("/send-custom-notification", methods={"POST"})
  448.      */
  449.     public function sendCustomNotification(Request $request): JsonResponse
  450.     {
  451.         try {
  452.             $params json_decode($request->getContent(), true);
  453.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  454.             $process = new Process(['bin/console''app:get-custom-notification']);
  455.             $process->setWorkingDirectory(PIMCORE_PROJECT_ROOT);
  456.             try {
  457.                 $process->mustRun();
  458.                 $result['success'] = true;
  459.                 $result['message'] = $process->getOutput();
  460.             } catch (ProcessFailedException $exception) {
  461.                 $this->logger->error($exception->getMessage());
  462.                 return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  463.             }
  464.             return $this->json($result);
  465.         } catch (\Exception $ex) {
  466.             $this->logger->error($ex->getMessage());
  467.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  468.         }
  469.     }
  470.     /**
  471.      * @Route("/alert-history", methods={"POST"})
  472.      */
  473.     public function alertHistory(Request $requestUserInterface $user): JsonResponse
  474.     {
  475.         try {
  476.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  477.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  478.                 return $this->json($permissions401);
  479.             } elseif ($permissions['success'] === false) {
  480.                 return $this->json($permissions);
  481.             }
  482.             if (!$user) {
  483.                 throw new \Exception('User is not authenticated');
  484.             }
  485.             $params json_decode($request->getContent(), true);
  486.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  487.             $result $this->customNotificationService->getAlertHistory($user$params);
  488.             if (!empty($result['data'])) {
  489.                 $excelData \App\Lib\Utility::generateExcelFromArray($result['data']);
  490.                 $asset null;
  491.                 if ($excelData) {
  492.                     $asset \App\Lib\Utility::createAsset($excelData$user->getId() . '_alert_history_.xlsx''alert_history');
  493.                 }
  494.             }
  495.             return $this->json($result);
  496.         } catch (\Exception $ex) {
  497.             $this->logger->error($ex->getMessage());
  498.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  499.         }
  500.     }
  501.     /**
  502.      * @Route("/regions", methods={"POST"})
  503.      */
  504.     public function regions(Request $requestUserInterface $user): JsonResponse
  505.     {
  506.         try {
  507.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  508.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  509.                 return $this->json($permissions401);
  510.             } elseif ($permissions['success'] === false) {
  511.                 return $this->json($permissions);
  512.             }
  513.             if (!$user) {
  514.                 throw new \Exception('User is not authenticated');
  515.             }
  516.             $params json_decode($request->getContent(), true);
  517.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  518.             $result $this->ncmWeatherApiService->getRegions(null, isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  519.             return $this->json($result);
  520.         } catch (\Exception $ex) {
  521.             $this->logger->error($ex->getMessage());
  522.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  523.         }
  524.     }
  525.     private function initiate(Request $request)
  526.     {
  527.         // Setting locale        
  528.         $headers $request->headers->all();
  529.         $this->lang = isset($headers['lang']) ? $headers['lang'][0] : DEFAULT_LOCALE;
  530.         $this->translator->setLocale($this->lang);
  531.         $permissions $this->userPermission->initiate($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  532.         return $permissions;
  533.     }
  534.     /**
  535.      * @Route("/get-custom-notification-log", methods={"POST"})
  536.      */
  537.     public function getCustomNotificationLog(Request $requestUserInterface $user): JsonResponse
  538.     {
  539.         try {
  540.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  541.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  542.                 return $this->json($permissions401);
  543.             } elseif ($permissions['success'] === false) {
  544.                 return $this->json($permissions);
  545.             }
  546.             if (!$user) {
  547.                 throw new \Exception('User is not authenticated');
  548.             }
  549.             $params json_decode($request->getContent(), true);
  550.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  551.             $result $this->customNotificationService->getUserAlerts($user->getId(), 0);
  552.             $notificationArr = [];
  553.             if (isset($result['success']) && $result['success'] == true && !empty($result['message'])) {
  554.                 foreach ($result['message'] as $message) {
  555.                     $notification $this->customNotificationService->getCustomNotificationLog($message['id']);
  556.                     if (!empty($notification)) {
  557.                         $notificationArr[] = $notification;
  558.                     }
  559.                 }
  560.                 $result["message"] = array_values($notificationArr);
  561.             }
  562.             return $this->json($result);
  563.         } catch (\Exception $ex) {
  564.             $this->logger->error($ex->getMessage());
  565.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  566.         }
  567.     }
  568.     /**
  569.      * @Route("/get-custom-notification-by-filters", methods={"POST"})
  570.      */
  571.     public function getCustomNotificationByFilters(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  572.     {
  573.         try {
  574.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  575.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  576.                 return $this->json($permissions401);
  577.             } elseif ($permissions['success'] === false) {
  578.                 return $this->json($permissions);
  579.             }
  580.             if (!$user) {
  581.                 throw new \Exception('User is not authenticated');
  582.             }
  583.             $params json_decode($request->getContent(), true);
  584.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  585.             $result $this->customNotificationService->getCustomNotificationAlerts($params$paginator);
  586.             return $this->json($result);
  587.         } catch (\Exception $ex) {
  588.             $this->logger->error($ex->getMessage());
  589.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  590.         }
  591.     }
  592.     /**
  593.      * @Route("/custom-notification-filters", name ="custom-notification-filters" , methods={"POST"})
  594.      */
  595.     public function getCustomNotificationFilters(Request $requestUserInterface $user): JsonResponse
  596.     {
  597.         try {
  598.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  599.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  600.                 return $this->json($permissions401);
  601.             } elseif ($permissions['success'] === false) {
  602.                 return $this->json($permissions);
  603.             }
  604.             if (!$user) {
  605.                 throw new \Exception('User is not authenticated');
  606.             }
  607.             $params json_decode($request->getContent(), true);
  608.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  609.             $result $this->customNotificationService->customNotificationFilters($this->translator);
  610.             return $this->json($result);
  611.         } catch (\Exception $ex) {
  612.             $this->logger->error($ex->getMessage());
  613.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  614.         }
  615.     }
  616.     /**
  617.      * @Route("/unsubscribe-notification-by-token", name ="unsubscribe-notification-by-token" , methods={"GET"})
  618.      */
  619.     public function unSubscribeNotificationByEmail(Request $request): JsonResponse
  620.     {
  621.         try {
  622.             if ($request->query->has('token')) {
  623.                 if ($request->get('token')) {
  624.                     $token $request->get('token');
  625.                     $result $this->customNotificationService->updateSubscribeNotification($token$this->translator);
  626.                     return $this->json($result);
  627.                 }
  628.             }
  629.             return $this->json(["success" => false"message" => "You have already unsubscribed the custom notification"]);
  630.         } catch (\Exception $ex) {
  631.             $this->logger->error($ex->getMessage());
  632.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  633.         }
  634.     }
  635.     /**
  636.      * @Route("/unsubscribe-ews-notification-by-token", name ="unsubscribe-ews-notification-by-token" , methods={"GET"})
  637.      */
  638.     public function unSubscribeEwsNotificationByEmail(Request $request): JsonResponse
  639.     {
  640.         try {
  641.             if ($request->query->has('token')) {
  642.                 if ($request->get('token')) {
  643.                     $token $request->get('token');
  644.                     $result $this->customNotificationService->unSubscribeEwsNotification($token$this->translator);
  645.                     return $this->json($result);
  646.                 }
  647.             }
  648.             return $this->json(["success" => false"message" => "You have already unsubscribed the custom notification"]);
  649.         } catch (\Exception $ex) {
  650.             $this->logger->error($ex->getMessage());
  651.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  652.         }
  653.     }
  654.     /**
  655.      * @Route("/create-manned-alert-subscription", methods={"POST"})
  656.      */
  657.     public function mannedAlertSubscription(Request $requestUserInterface $user): JsonResponse
  658.     {
  659.         try {
  660.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  661.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  662.                 return $this->json($permissions401);
  663.             } elseif ($permissions['success'] === false) {
  664.                 return $this->json($permissions);
  665.             }
  666.             $params json_decode($request->getContent(), true);
  667.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  668.             $requiredParameters = ['region_id''governorate_id''alert_type_id''phenomena_id'];
  669.             foreach ($requiredParameters as $param) {
  670.                 if (!isset($params[$param]) || empty($params[$param])) {
  671.                     $missingParams[] = $param;
  672.                 }
  673.             }
  674.             if (!empty($missingParams)) {
  675.                 // Throw an exception with a message that includes the missing parameters
  676.                 $parameterList implode(", "$missingParams);
  677.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  678.             }
  679.             $result $this->customNotificationService->mannedAlertSubscription($user$params$this->translator);
  680.             return $this->json($result);
  681.         } catch (\Exception $ex) {
  682.             $this->logger->error($ex->getMessage());
  683.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  684.         }
  685.     }
  686.     /**
  687.      * @Route("/assign-alert-subscription-to-subscriber", methods={"POST"})
  688.      */
  689.     public function mannedAlertSubscriber(Request $requestUserInterface $user): JsonResponse
  690.     {
  691.         try {
  692.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  693.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  694.                 return $this->json($permissions401);
  695.             } elseif ($permissions['success'] === false) {
  696.                 return $this->json($permissions);
  697.             }
  698.             $params json_decode($request->getContent(), true);
  699.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  700.             // Validate required parameters: subscription_id and user_id (can be single id or array of ids)
  701.             $missingParams = [];
  702.             if (!isset($params['subscription_id'])) {
  703.                 $missingParams[] = 'subscription_id';
  704.             }
  705.             if (!isset($params['user_id'])) {
  706.                 $missingParams[] = 'user_id';
  707.             } else {
  708.                 // if array, ensure non-empty; if scalar, ensure not empty
  709.                 if (is_array($params['user_id'])) {
  710.                     $sanitized array_values(array_unique(array_map('intval'array_filter($params['user_id']))));
  711.                     if (count($sanitized) === 0) {
  712.                         $missingParams[] = 'user_id';
  713.                     } else {
  714.                         $params['user_id'] = $sanitized;
  715.                     }
  716.                 } else if (empty($params['user_id'])) {
  717.                     $missingParams[] = 'user_id';
  718.                 }
  719.             }
  720.             if (!empty($missingParams)) {
  721.                 $parameterList implode(", "$missingParams);
  722.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  723.             }
  724.             $result $this->customNotificationService->mannedAlertSubscriber($params$user$this->translator);
  725.             return $this->json($result);
  726.         } catch (\Exception $ex) {
  727.             $this->logger->error($ex->getMessage());
  728.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  729.         }
  730.     }
  731.     /**
  732.      * @Route("/list-manned-alert-subscription", methods={"POST"})
  733.      */
  734.     public function listMannedAlertSubscription(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  735.     {
  736.         try {
  737.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  738.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  739.                 return $this->json($permissions401);
  740.             } elseif ($permissions['success'] === false) {
  741.                 return $this->json($permissions);
  742.             }
  743.             $params json_decode($request->getContent(), true);
  744.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  745.             $result $this->customNotificationService->listMannedAlertSubscription($params$user$paginator);
  746.             return $this->json($result);
  747.         } catch (\Exception $ex) {
  748.             $this->logger->error($ex->getMessage());
  749.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  750.         }
  751.     }
  752.     /**
  753.      * @Route("/delete-manned-alert-subscription", methods={"POST"})
  754.      */
  755.     public function deleteMannedAlertSubscription(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  756.     {
  757.         try {
  758.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  759.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  760.                 return $this->json($permissions401);
  761.             } elseif ($permissions['success'] === false) {
  762.                 return $this->json($permissions);
  763.             }
  764.             $params json_decode($request->getContent(), true);
  765.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  766.             if (!isset($params['subscription_id']) || empty($params['subscription_id'])) {
  767.                 return $this->json(['success' => false'message' => $this->translator->trans("missing_required_parameter_subscription_id")]);
  768.             }
  769.             $result $this->customNotificationService->deleteMannedAlertSubscription($request$params$user$this->translator);
  770.             return $this->json($result);
  771.         } catch (\Exception $ex) {
  772.             $this->logger->error($ex->getMessage());
  773.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  774.         }
  775.     }
  776.     /**
  777.      * @Route("/manned-alert-subscribe", methods={"POST"})
  778.      */
  779.     public function mannedAlertSubscribe(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  780.     {
  781.         try {
  782.             $params json_decode($request->getContent(), true);
  783.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  784.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  785.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  786.                 return $this->json($permissions401);
  787.             } elseif ($permissions['success'] === false) {
  788.                 return $this->json($permissions);
  789.             }
  790.             if ((!isset($params['subscription_id']) || empty($params['subscription_id'])) && !isset($params['status'])) {
  791.                 return $this->json(['success' => false'message' => $this->translator->trans("missing_required_parameter")]);
  792.             }
  793.             $result $this->customNotificationService->mannedAlertSubscribe($request$params$user$this->translator);
  794.             return $this->json($result);
  795.         } catch (\Exception $ex) {
  796.             $this->logger->error($ex->getMessage());
  797.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  798.         }
  799.     }
  800.     /**
  801.      * @Route("/advance-alert-subscribe", methods={"POST"}, name="advance_alert_subscribe")
  802.      */
  803.     public function advanceAlertSubscribe(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  804.     {
  805.         try {
  806.             $params json_decode($request->getContent(), true);
  807.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  808.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  809.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  810.                 return $this->json($permissions401);
  811.             } elseif ($permissions['success'] === false) {
  812.                 return $this->json($permissions);
  813.             }
  814.             if (!isset($params['notification_id']) || empty($params['notification_id'])) {
  815.                 return $this->json(['success' => false'message' => $this->translator->trans("missing_required_parameter")]);
  816.             }
  817.             if (!isset($params['notifyEmail'])) {
  818.                 return $this->json(['success' => false'message' => $this->translator->trans("missing_required_parameter")]);
  819.             }
  820.             $result $this->customNotificationService->advanceAlertSubscribe($request$params$user$this->translator);
  821.             return $this->json($result);
  822.         } catch (\Exception $ex) {
  823.             $this->logger->error($ex->getMessage());
  824.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  825.         }
  826.     }
  827.     /**
  828.      * @Route("/custom-alert-subscribe", methods={"POST"}, name="custom_alert_subscribe")
  829.      */
  830.     public function customAlertSubscribe(Request $requestUserInterface $userPaginatorInterface $paginator): JsonResponse
  831.     {
  832.         try {
  833.             $params json_decode($request->getContent(), true);
  834.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  835.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  836.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  837.                 return $this->json($permissions401);
  838.             } elseif ($permissions['success'] === false) {
  839.                 return $this->json($permissions);
  840.             }
  841.             if ((!isset($params['notification_id']) || empty($params['notification_id'])) && !isset($params['status'])) {
  842.                 return $this->json(['success' => false'message' => $this->translator->trans("missing_required_parameter")]);
  843.             }
  844.             $result $this->customNotificationService->customAlertSubscribe($request$params$user$this->translator);
  845.             return $this->json($result);
  846.         } catch (\Exception $ex) {
  847.             $this->logger->error($ex->getMessage());
  848.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  849.         }
  850.     }
  851.     /**
  852.      * @Route("/create-tag", name="create_tag")
  853.      */
  854.     public function createTag(Request $request)
  855.     {
  856.         try {
  857.             $params json_decode($request->getContent(), true);
  858.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  859.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  860.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  861.                 return $this->json($permissions401);
  862.             } elseif ($permissions['success'] === false) {
  863.                 return $this->json($permissions);
  864.             }
  865.             $requiredParameters = ['name''lang'];
  866.             foreach ($requiredParameters as $param) {
  867.                 if (!isset($params[$param])) {
  868.                     $missingParams[] = $param;
  869.                 }
  870.             }
  871.             if (!empty($missingParams)) {
  872.                 // Throw an exception with a message that includes the missing parameters
  873.                 $parameterList implode(", "$missingParams);
  874.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  875.             }
  876.             $result $this->createTag($params$this->translator);
  877.             return $this->json($result);
  878.         } catch (\Exception $ex) {
  879.             $this->logger->error($ex->getMessage());
  880.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  881.         }
  882.     }
  883.     /**
  884.      * @Route("/get-tags", name="get_tags")
  885.      */
  886.     public function getTags(Request $requestUserInterface $userPaginatorInterface $paginator)
  887.     {
  888.         try {
  889.             $params json_decode($request->getContent(), true);
  890.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  891.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  892.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  893.                 return $this->json($permissions401);
  894.             } elseif ($permissions['success'] === false) {
  895.                 return $this->json($permissions);
  896.             }
  897.             if (!$user) {
  898.                 throw new \Exception('User is not authenticated');
  899.             }
  900.             $tags = new CustomNotificationTags\Listing();
  901.             $tags->filterByUser($user);
  902.             $data = [];
  903.             foreach ($tags as $tag) {
  904.                 $data[] = [
  905.                     'id' => $tag->getId(),
  906.                     'name' => $tag->getTagName(),
  907.                 ];
  908.             }
  909.             return $this->json(['success' => true'data' => $data]);
  910.         } catch (\Exception $ex) {
  911.             $this->logger->error($ex->getMessage());
  912.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  913.         }
  914.     }
  915.     /**
  916.      * @Route("/get-notification-tags", name="get_notification_tags" , methods={"POST"})
  917.      */
  918.     public function getNotificationTags(Request $requestUserInterface $userPaginatorInterface $paginator)
  919.     {
  920.         try {
  921.             $params json_decode($request->getContent(), true);
  922.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  923.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  924.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  925.                 return $this->json($permissions401);
  926.             } elseif ($permissions['success'] === false) {
  927.                 return $this->json($permissions);
  928.             }
  929.             if (!$user) {
  930.                 throw new \Exception('User is not authenticated');
  931.             }
  932.             $result $this->customNotificationModel->getNotificationTags($user$paginator$this->translator);
  933.             return $this->json($result);
  934.         } catch (\Exception $ex) {
  935.             $this->logger->error($ex->getMessage());
  936.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  937.         }
  938.     }
  939.     /**
  940.      * @Route("/get-params", name="get_params")
  941.      */
  942.     public function getParametersConfig(Request $request)
  943.     {
  944.         try {
  945.             $params json_decode($request->getContent(), true);
  946.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  947.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  948.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  949.                 return $this->json($permissions401);
  950.             } elseif ($permissions['success'] === false) {
  951.                 return $this->json($permissions);
  952.             }
  953.             $result $this->customNotificationModel->getParametersConfig($this->translator);
  954.             return $this->json($result);
  955.         } catch (\Exception $ex) {
  956.             $this->logger->error($ex->getMessage());
  957.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  958.         }
  959.     }
  960.     /**
  961.      * @Route("/update-parameter-height-depth", name="update_parameter_height_depth", methods={"POST"})
  962.      */
  963.     public function updateParameterHeightDepth(Request $request)
  964.     {
  965.         try {
  966.             $params json_decode($request->getContent(), true) ?? [];
  967.             $this->translator->setlocale($params['lang'] ?? DEFAULT_LOCALE);
  968.             // $permissions = $this->userPermission->validateAPI($request, $this->tokenStorageInterface, $this->jwtManager, $this->translator);
  969.             // if (isset($permissions['code']) && $permissions['code'] === 401) {
  970.             //     return $this->json($permissions, 401);
  971.             // }
  972.             // if (isset($permissions['success']) && $permissions['success'] === false) {
  973.             //     return $this->json($permissions);
  974.             // }
  975.             $result $this->customNotificationModel->updateParameterHeightDepth($params$this->translator);
  976.             return $this->json($result);
  977.         } catch (\Exception $ex) {
  978.             $this->logger->error($ex->getMessage());
  979.             return $this->json(['success' => false'message' => $this->translator->trans(USER_ERROR_MESSAGE)]);
  980.         }
  981.     }
  982.     /**
  983.      * @Route("/save-notification-config", name="save_notification_config")
  984.      */
  985.     public function saveNotificationConfig(Request $requestUserInterface $user)
  986.     {
  987.         try {
  988.             $params json_decode($request->getContent(), true);
  989.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  990.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  991.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  992.                 return $this->json($permissions401);
  993.             } elseif ($permissions['success'] === false) {
  994.                 return $this->json($permissions);
  995.             }
  996.             $requiredParameters = ['notificationName''description''severity''rules''lang''sequence''duration'];
  997.             foreach ($requiredParameters as $param) {
  998.                 if (!isset($params[$param])) {
  999.                     $missingParams[] = $param;
  1000.                 }
  1001.             }
  1002.             if (!empty($missingParams)) {
  1003.                 // Throw an exception with a message that includes the missing parameters
  1004.                 $parameterList implode(", "$missingParams);
  1005.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1006.             }
  1007.             $result $this->customNotificationModel->saveNotificationConfig($params$user$this->translator$this->logger$this->mateoMaticsService);
  1008.             return $this->json($result);
  1009.         } catch (\Exception $ex) {
  1010.             $this->logger->error($ex->getMessage());
  1011.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1012.         }
  1013.     }
  1014.     /**
  1015.      * @Route("/save-advance-custom-notification-by-tag", name="save_advance_custom_notification_by_tag" , methods={"POST"})
  1016.      */
  1017.     public function saveAdvanceCustomNotificationByTag(Request $requestUserInterface $user)
  1018.     {
  1019.         try {
  1020.             $params json_decode($request->getContent(), true);
  1021.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1022.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1023.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1024.                 return $this->json($permissions401);
  1025.             } elseif ($permissions['success'] === false) {
  1026.                 return $this->json($permissions);
  1027.             }
  1028.             $requiredParameters = ['notificationName''locationTagId''description''severity''rules''lang''sequence''duration'];
  1029.             foreach ($requiredParameters as $param) {
  1030.                 if (!isset($params[$param])) {
  1031.                     $missingParams[] = $param;
  1032.                 }
  1033.             }
  1034.             if (!empty($missingParams)) {
  1035.                 // Throw an exception with a message that includes the missing parameters
  1036.                 $parameterList implode(", "$missingParams);
  1037.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1038.             }
  1039.             $result $this->customNotificationModel->saveAdvanceCustomNotificationByTag($params$user$this->translator$this->logger$this->mateoMaticsService);
  1040.             return $this->json($result);
  1041.         } catch (\Exception $ex) {
  1042.             $this->logger->error($ex->getMessage());
  1043.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1044.         }
  1045.     }
  1046.     /**
  1047.      * @Route("/list-advanced-custom-notifications", name="list_advanced_custom_notifications")
  1048.      */
  1049.     public function listAdvancedCustomNotifications(Request $requestUserInterface $userPaginatorInterface $paginator)
  1050.     {
  1051.         try {
  1052.             $params json_decode($request->getContent(), true);
  1053.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1054.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1055.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1056.                 return $this->json($permissions401);
  1057.             } elseif ($permissions['success'] === false) {
  1058.                 return $this->json($permissions);
  1059.             }
  1060.             $result $this->customNotificationModel->listAdvancedCustomNotifications($params$user$paginator$this->translator);
  1061.             return $this->json($result);
  1062.         } catch (\Exception $ex) {
  1063.             $this->logger->error($ex->getMessage());
  1064.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1065.         }
  1066.     }
  1067.     /**
  1068.      * @Route("/del-advanced-custom-notifications", name="del_advanced_custom_notifications")
  1069.      */
  1070.     public function delAdvancedCustomNotification(Request $requestUserInterface $user)
  1071.     {
  1072.         try {
  1073.             $params json_decode($request->getContent(), true);
  1074.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1075.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1076.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1077.                 return $this->json($permissions401);
  1078.             } elseif ($permissions['success'] === false) {
  1079.                 return $this->json($permissions);
  1080.             }
  1081.             $requiredParameters = ['id'];
  1082.             foreach ($requiredParameters as $param) {
  1083.                 if (!isset($params[$param])) {
  1084.                     $missingParams[] = $param;
  1085.                 }
  1086.             }
  1087.             if (!empty($missingParams)) {
  1088.                 // Throw an exception with a message that includes the missing parameters
  1089.                 $parameterList implode(", "$missingParams);
  1090.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1091.             }
  1092.             $result = [];
  1093.             if (isset($params['id']) && is_array($params['id'])) {
  1094.                 // Multiple IDs
  1095.                 $result $this->customNotificationModel->delMultipleAdvancedCustomNotifications($params$user$this->translator);
  1096.             } else {
  1097.                 // Single ID
  1098.                 $result $this->customNotificationModel->delAdvancedCustomNotifications($params$user$this->translator);
  1099.             }
  1100.             return $this->json($result);
  1101.         } catch (\Exception $ex) {
  1102.             $this->logger->error($ex->getMessage());
  1103.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1104.         }
  1105.     }
  1106.     /**
  1107.      * @Route("/get-advanced-custom-notifications", name="get_advanced_custom_notifications")
  1108.      */
  1109.     public function getAdvancedCustomNotification(Request $requestUserInterface $user)
  1110.     {
  1111.         try {
  1112.             $params json_decode($request->getContent(), true);
  1113.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1114.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1115.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1116.                 return $this->json($permissions401);
  1117.             } elseif ($permissions['success'] === false) {
  1118.                 return $this->json($permissions);
  1119.             }
  1120.             $requiredParameters = ['id'];
  1121.             foreach ($requiredParameters as $param) {
  1122.                 if (!isset($params[$param])) {
  1123.                     $missingParams[] = $param;
  1124.                 }
  1125.             }
  1126.             if (!empty($missingParams)) {
  1127.                 // Throw an exception with a message that includes the missing parameters
  1128.                 $parameterList implode(", "$missingParams);
  1129.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1130.             }
  1131.             $result $this->customNotificationModel->getAdvancedCustomNotifications($params$user$this->translator);
  1132.             return $this->json($result);
  1133.         } catch (\Exception $ex) {
  1134.             $this->logger->error($ex->getMessage());
  1135.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1136.         }
  1137.     }
  1138.     /**
  1139.      * @Route("/assign-users", name="assign_users")
  1140.      */
  1141.     public function assignUsers(Request $requestUserInterface $user)
  1142.     {
  1143.         try {
  1144.             $params json_decode($request->getContent(), true);
  1145.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1146.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1147.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1148.                 return $this->json($permissions401);
  1149.             } elseif ($permissions['success'] === false) {
  1150.                 return $this->json($permissions);
  1151.             }
  1152.             $requiredParameters = ['id'];
  1153.             foreach ($requiredParameters as $param) {
  1154.                 if (!isset($params[$param])) {
  1155.                     $missingParams[] = $param;
  1156.                 }
  1157.             }
  1158.             if (!empty($missingParams)) {
  1159.                 // Throw an exception with a message that includes the missing parameters
  1160.                 $parameterList implode(", "$missingParams);
  1161.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1162.             }
  1163.             $result $this->customNotificationModel->assignUsers($params$user$this->translator);
  1164.             return $this->json($result);
  1165.         } catch (\Exception $ex) {
  1166.             $this->logger->error($ex->getMessage());
  1167.             return $this->json(['success' => false'message' => $this->translator->trans($ex->getMessage())]);
  1168.         }
  1169.     }
  1170.     /**
  1171.      * @Route("/assign-alert-subscription", name="assign_alert_subscription",methods={"POST"})
  1172.      */
  1173.     public function assignAlertSubscription(Request $requestUserInterface $user): JsonResponse
  1174.     {
  1175.         try {
  1176.             $params json_decode($request->getContent(), true);
  1177.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1178.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1179.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1180.                 return $this->json($permissions401);
  1181.             } elseif ($permissions['success'] === false) {
  1182.                 return $this->json($permissions);
  1183.             }
  1184.             $requiredParameters = ['AlertSubscriptionId''UserId'];
  1185.             foreach ($requiredParameters as $param) {
  1186.                 if (!isset($params[$param]) || empty($params[$param])) {
  1187.                     $missingParams[] = $param;
  1188.                 }
  1189.             }
  1190.             if (!empty($missingParams)) {
  1191.                 // Throw an exception with a message that includes the missing parameters
  1192.                 $parameterList implode(", "$missingParams);
  1193.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  1194.             }
  1195.             $result $this->customNotificationService->assignAlertSubscription($user$params$this->translator);
  1196.             return $this->json($result);
  1197.         } catch (\Exception $ex) {
  1198.             $this->logger->error($ex->getMessage());
  1199.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  1200.         }
  1201.     }
  1202.     /**
  1203.      * @Route("/governorates", name="api_ncm_notification_governorates", methods={"POST"})
  1204.      */
  1205.     public function getGovernorateByRegionAction(Request $request): JsonResponse
  1206.     {
  1207.         try {
  1208.             $params json_decode($request->getContent(), true);
  1209.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1210.             $permissions $this->userPermission->validateAPI($request$this->tokenStorageInterface$this->jwtManager$this->translator);
  1211.             if (isset($permissions['code']) && $permissions['code'] === 401) {
  1212.                 return $this->json($permissions401);
  1213.             } elseif ($permissions['success'] === false) {
  1214.                 return $this->json($permissions);
  1215.             }
  1216.             $result $this->ewsNotificationModel->getGovernoratesByRegion($params);
  1217.             return $this->json($result);
  1218.         } catch (\Exception $ex) {
  1219.             $this->logger->error($ex->getMessage());
  1220.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  1221.         }
  1222.     }
  1223.     /**
  1224.      * @Route("/test-ews-user-group-list", methods={"POST"})
  1225.      */
  1226.     public function userGroupList(Request $requestUserInterface $user): JsonResponse
  1227.     {
  1228.         try {
  1229.             $params  json_decode($request->getContent(), true);
  1230.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  1231.             if (!$user) {
  1232.                 return $this->json(['success' => false'message' => $this->translator->trans('user_is_not_authenticated')]);
  1233.             }
  1234.             $result  \App\Lib\Utility::getTestEwsUserGroupEmailList();
  1235.             return $this->json(['success' => true'data' => $result]);
  1236.         } catch (\Exception $ex) {
  1237.             $this->logger->error($ex->getMessage());
  1238.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  1239.         }
  1240.     }
  1241. }