Przeglądaj źródła

Created a repository from onu and the service to create a notification in base

Jean Sumara 5 lat temu
rodzic
commit
eca4c7ef42

+ 9 - 1
src/FTTHBundle/Controller/ONUCRUDController.php

@@ -89,7 +89,15 @@ class ONUCRUDController extends CRUDController
         $format = $request->get('format');
 
         $onuExportService = $this->get('onu_export_service');
-        $onuExportService->send($tenancyId, $format);
+
+        $filename = sprintf(
+            'export_%s_%s.%s',
+            strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
+            date('Y_m_d_H_i_s', strtotime('now')),
+            $format
+        );
+
+        $onuExportService->send($tenancyId, $filename, $format, $this->admin->getClass());
 
         return $this->redirect('/admin/ftth/onu/list');
     }

+ 4 - 3
src/FTTHBundle/Controller/REST/ONURESTController.php

@@ -227,9 +227,10 @@ class ONURESTController extends RESTController
      */
     public function onuExportAction(Request $request)
     {
-        $tenancyId = $request->headers->get('tenancyid') != null ? (int) $request->headers->get('tenancyid') : 1;
-        $currentPage = $request->headers->get('currentpage') != null ? (int) $request->headers->get('currentpage') : 1;
-        $limit = $request->headers->get('limit') != null ? (int) $request->headers->get('limit') : 20;
+        $filters = $request->get('filters');
+        $tenancyId = $filters['tenancyId'] != null ? (int) $filters['tenancyId'] : 1;
+        $currentPage = $filters['currentPage'] != null ? (int) $filters['currentPage'] : 1;
+        $limit = $filters['limit'] != null ? (int) $filters['limit'] : 20;
         $onuExportService = $this->get('onu_export_service');
         return JsonResponseUtils::make($onuExportService->getOnusWithCostumer($tenancyId, $currentPage, $limit));
     }

+ 8 - 8
src/FTTHBundle/Repository/ONURepository.php

@@ -37,23 +37,23 @@ class ONURepository extends \Doctrine\ORM\EntityRepository
         $query = $this->createQueryBuilder('o');
         $query
             ->select([
-                'o.serialNumber',
-                'o.ponSerialNumber',
-                'o.currentState',
-                'o.clientId',
+                'o.updated',
+                'o.created',
                 'o.ip',
                 'o.mac',
                 'o.radiusAuth',
-                'olt.name AS oltName',
-                'nap.name AS napName',
                 'nap.slot',
                 'nap.link',
                 'o.position',
                 'o.catv',
                 'o.voip',
                 'o.activationCode',
-                'o.created',
-                'o.updated'
+                'olt.name AS oltName',
+                'nap.name AS napName',
+                'o.currentState',
+                'o.serialNumber',
+                'o.ponSerialNumber',
+                'o.clientId'
             ])
             ->join('o.olt', "olt")
             ->leftJoin('o.nap', "nap")

+ 4 - 0
src/FTTHBundle/Resources/config/services.yml

@@ -149,4 +149,8 @@ services:
 
     onu_export_service:
         class: FTTHBundle\Service\OnuExportService
+        arguments: ["@service_container"]
+
+    notification_service:
+        class: FTTHBundle\Service\NotificationService
         arguments: ["@service_container"]

+ 43 - 0
src/FTTHBundle/Service/NotificationService.php

@@ -0,0 +1,43 @@
+<?php
+
+
+namespace FTTHBundle\Service;
+
+
+use Base\AdminBundle\Checks\CheckParametersAsURL;
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class NotificationService
+{
+
+    private $container;
+
+    /**
+     * OnuExportService constructor.
+     * @param ContainerInterface $container
+     */
+    public function __construct(ContainerInterface $container)
+    {
+        $this->container = $container;
+    }
+
+    public function create($message, $entity, $tenancyId){
+        $baseUrl = CheckParametersAsURL::checkParametersUrl($this->container, 'url_base');
+        if(!is_null($baseUrl)){
+            $ws = $this->container->get('webservice');
+
+            $body = [
+                'message' => $message,
+                'entity' => $entity,
+                'status' => 1,
+                'tenancy' => $tenancyId,
+                'file' => ''
+            ];
+
+            $response = $ws->makeGetRequest($baseUrl.'/api/notifications', HttpRequestInterface::METHOD_POST, $body);
+            return json_decode($response,true);
+        }
+        return null;
+    }
+}

+ 26 - 14
src/FTTHBundle/Service/OnuExportService.php

@@ -24,24 +24,36 @@ class OnuExportService
         $this->container = $container;
     }
 
-    public function send($tenancyId, $format){
+    public function send($tenancyId, $filename, $format, $className){
         $amqpChannel = $this->container->get('amqp.channel');
+        $notificationService = $this->container->get('notification_service');
 
         try{
-            $msgBody = serialize([
-                'tenancyId' => $tenancyId,
-                'format' => $format,
-                'endpointToken' => 'https://'.getenv('HOST_BASE')."/oauth/v2/token",
-                'endpointOnus' => 'https://'.getenv('HOST_FTTH')."/api/onus-export",
-                'params' => [
-                    'client_id' => getenv('OAUTH_CLIENT_ID'),
-                    'client_secret' => getenv('OAUTH_CLIENT_SECRET'),
-                    'grant_type' => 'client_credentials'
-                ]
-            ]);
-            $amqpChannel->basic_publish(new AMQPMessage((string) $msgBody), '','export.ftth');
+            $notification = $notificationService->create('Onus enviado a la cola para procesar', $className, $tenancyId);
+            if(!is_null($notification)){
+                $msgBody = serialize([
+                    'tenancyId' => $tenancyId,
+                    'format' => $format,
+                    'filename' => $filename,
+                    'notificationId' => $notification['id'],
+                    'endpointBase' => 'https://'.getenv('HOST_BASE'),
+                    'endpointOnus' => 'https://'.getenv('HOST_FTTH')."/api/onus-export",
+                    'params' => [
+                        'client_id' => getenv('OAUTH_CLIENT_ID'),
+                        'client_secret' => getenv('OAUTH_CLIENT_SECRET'),
+                        'grant_type' => 'client_credentials'
+                    ]
+                ]);
+                $amqpChannel->basic_publish(new AMQPMessage((string) $msgBody), '','export.ftth');
+            }else{
+                if ($this->container->has('monolog.logger.webservice')) {
+                    $this->container->get('monolog.logger.webservice')->error("Can't create a notification in Base");
+                }
+            }
         }catch (\Exception $ex){
-
+            if ($this->container->has('monolog.logger.webservice')) {
+                $this->container->get('monolog.logger.webservice')->error($ex->getMessage());
+            }
         }
     }