Advertisement
Guest User

gis admin take two

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.34 KB | None | 0 0
  1. diff --git a/configuration/shared/rpc.tmpl b/configuration/shared/rpc.tmpl
  2. index b7c625a..62e232a 100644
  3. --- a/configuration/shared/rpc.tmpl
  4. +++ b/configuration/shared/rpc.tmpl
  5. @@ -182,6 +182,13 @@
  6.                  method => 'getPersonDetailsRPC',
  7.                  encoder => 'Cpanel::JSON::XS',
  8.              },
  9. +            'service.agent.token.get_device_data' => {
  10. +                state => 'content',
  11. +                type => 'method',
  12. +                subpack => 'Service::Agent::Session_v1_0',
  13. +                method => 'getBrandAndStoreFromDeviceID',
  14. +                encoder => 'Cpanel::JSON::XS',
  15. +            },
  16.  
  17. diff --git a/server/generic/Generic/Service/Agent/Applications.pm b/server/generic/Generic/Service/Agent/Applications.pm
  18. index a08aaa7..eb2a99c 100644
  19. --- a/server/generic/Generic/Service/Agent/Applications.pm
  20. +++ b/server/generic/Generic/Service/Agent/Applications.pm
  21. @@ -24,7 +24,7 @@ sub serviceGet {
  22.      }
  23.  
  24.      # Parse store id from passed in device id, we will use this to filter the applications returned
  25. -    my $store_info = $self->getBrandAndStoreFromDeviceID($args->{device_id});
  26. +    my $store_info = $self->getBrandAndStoreFromDeviceID({ 'device_id' => $args->{device_id}});
  27.  
  28.      my $allowed_apps_for_store = $self->_getApplicationsForStoreId({store_id => $store_info->{store_id}});
  29.  
  30. diff --git a/server/generic/Generic/Service/Agent/Session_v1_0.pm b/server/generic/Generic/Service/Agent/Session_v1_0.pm
  31. index c6edde3..d608098 100644
  32. --- a/server/generic/Generic/Service/Agent/Session_v1_0.pm
  33. +++ b/server/generic/Generic/Service/Agent/Session_v1_0.pm
  34. @@ -17,7 +17,9 @@ sub jsonSchemaOutPost
  35.  }
  36.  
  37.  sub getDeviceDataFromDB {
  38. -    my ( $self, $device_id ) = @_;
  39. +    my ( $self, $args ) = @_;
  40. +    my $device_id = $args->{device_id};
  41. +    my $show_debug = $args->{show_debug};
  42.  
  43.      my $db = $self->get('DB');
  44.      my $prefix = $db->prefix;
  45. @@ -36,14 +38,59 @@ sub getDeviceDataFromDB {
  46.      my $agent_config = $self->get('Configuration')->appConfig('agent_api');
  47.      my $gis_brand_mapping = $$agent_config{'%gis_brand_mapping'};
  48.  
  49. -    return {
  50. +    my $store_info = {
  51.          brand_id => $gis_brand_mapping->{$row[0]},
  52.          store_id => $store_id,
  53.      };
  54. +    if ($show_debug) {
  55. +        $store_info->{data_source} = 'DB Cache';
  56. +        $store_info->{brand_id_raw} = $row[0];
  57. +    }
  58. +    return $store_info;
  59. +}
  60. +
  61. +sub getDeviceDataFromGis {
  62. +    my ( $self, $args ) = @_;
  63. +    my $device_id = $args->{device_id};
  64. +    my $show_debug = $args->{show_debug};
  65. +    my $store_info = {};
  66. +    my $env_config = Generic::Environment::extract(['gis_device_service']);
  67. +    my $agent_config = $self->get('Configuration')->appConfig('agent_api');
  68. +    my $query_url = $env_config->{server_url} . '?serialnumber=' . $device_id;
  69. +    my $req = HTTP::Request->new( 'POST', $query_url);
  70. +    $req->header('Accept' => 'application/json');
  71. +    my $lwp =  LWP::UserAgent->new;
  72. +
  73. +    $req->authorization_basic($env_config->{username}, $env_config->{password});
  74. +    my $res = $lwp->request($req);
  75. +    if ($res->is_success) {
  76. +        my $response = $res->decoded_content((charset => 'utf8'));
  77. +        my $json = $self->get('Util::JSON');
  78. +        $json->fromJson($response);
  79. +        $response = $json->asJson;
  80. +        $store_info->{store_id} = $response->{'mdmRequestOutput'}{'Door'};
  81. +        # GIS returns brands in an invalid format, we need to translate them per OAB-1843
  82. +        my $gis_brand_mapping = $$agent_config{'%gis_brand_mapping'};
  83. +         if ($show_debug) {
  84. +            $store_info->{data_source} = 'GIS Api';
  85. +            $store_info->{brand_id_raw} = $response->{'mdmRequestOutput'}{'Brand'};
  86. +        }
  87. +        $store_info->{brand_id} = $gis_brand_mapping->{$response->{'mdmRequestOutput'}{'Brand'}};
  88. +    } else {
  89. +        my $error_info = "getDeviceDataFromGis error: \nquery_url: " .$query_url."\nbase url: ". $res->base ."\n".  $res->code ."\n". $res->message ."
  90. \n". $res->as_string();
  91. +        $self->rlog->warn($error_info);
  92. +        if ($show_debug) {
  93. +            $store_info->{gis_error} = $error_info;
  94. +        }
  95. +    }
  96. +
  97. +    return $store_info;
  98.  }
  99.  
  100.  sub getBrandAndStoreFromDeviceID {
  101. -    my ( $self, $device_id ) = @_;
  102. +    my ( $self, $args ) = @_;
  103. +    my $device_id = $args->{device_id};
  104. +    my $show_debug = $args->{show_debug};
  105.      my $store_info = {};
  106.      # Need to support the old method of getting brand and store directly from the device id
  107.      if (length($device_id) > 20) {
  108. @@ -52,33 +99,11 @@ sub getBrandAndStoreFromDeviceID {
  109.          $store_info->{brand_id} =~ s/^\s+|\s+$//g;
  110.      } else {
  111.          # First, try to get the data from the DB (cached data from GIS)
  112. -        $store_info = $self->getDeviceDataFromDB($device_id);
  113. +        $store_info = $self->getDeviceDataFromDB({ 'device_id' => $device_id, 'show_debug' => $show_debug});
  114.          unless ($store_info) {
  115. -            my $env_config = Generic::Environment::extract(['gis_device_service']);
  116. -            my $agent_config = $self->get('Configuration')->appConfig('agent_api');
  117. -            my $query_url = $env_config->{server_url} . '?serialnumber=' . $device_id;
  118. -            my $req = HTTP::Request->new( 'POST', $query_url);
  119. -            $req->header('Accept' => 'application/json');
  120. -            my $lwp =  LWP::UserAgent->new;
  121. -
  122. -            $req->authorization_basic($env_config->{username}, $env_config->{password});
  123. -            my $res = $lwp->request($req);
  124. -            if ($res->is_success) {
  125. -                my $response = $res->decoded_content((charset => 'utf8'));
  126. -                my $json = $self->get('Util::JSON');
  127. -                $json->fromJson($response);
  128. -                $response = $json->asJson;
  129. -                $store_info->{store_id} = $response->{'mdmRequestOutput'}{'Door'};
  130. -                # GIS returns brands in an invalid format, we need to translate them per OAB-1843
  131. -                my $gis_brand_mapping = $$agent_config{'%gis_brand_mapping'};
  132. -                $store_info->{brand_id} = $gis_brand_mapping->{$response->{'mdmRequestOutput'}{'Brand'}};
  133. -            } else {
  134. -                my $error_info = "_getBrandAndStoreFromDeviceID error: \nquery_url: " .$query_url."\nbase url: ". $res->base ."\n".  $res->code ."\n".
  135.  $res->message ."\n". $res->as_string();
  136. -                $self->rlog->warn($error_info);
  137. -            }
  138. +            $store_info = $self->getDeviceDataFromGis({ 'device_id' => $device_id, 'show_debug' => $show_debug});
  139.          }
  140.      }
  141. -
  142.      return $store_info;
  143.  }
  144.  
  145. diff --git a/server/generic/Generic/Service/Agent/Token.pm b/server/generic/Generic/Service/Agent/Token.pm
  146. index 2445584..308141d 100644
  147. --- a/server/generic/Generic/Service/Agent/Token.pm
  148. +++ b/server/generic/Generic/Service/Agent/Token.pm
  149. @@ -20,7 +20,7 @@ sub servicePost {
  150.  
  151.      my $device_id = $args->{'device_id'};
  152.  
  153. -    my $store_info = $self->getBrandAndStoreFromDeviceID($device_id);
  154. +    my $store_info = $self->getBrandAndStoreFromDeviceID({'device_id' => $device_id});
  155.  
  156.      # Set up JSON stuff to validate
  157.      my $validation_schema = $self->get( 'JSON::Schema::ValidationObject', {
  158. diff --git a/server/generic/Generic/Service/CSAPP/Authorize_v1_1.pm b/server/generic/Generic/Service/CSAPP/Authorize_v1_1.pm
  159. index ad16c38..6b3ba98 100644
  160. --- a/server/generic/Generic/Service/CSAPP/Authorize_v1_1.pm
  161. +++ b/server/generic/Generic/Service/CSAPP/Authorize_v1_1.pm
  162. @@ -118,7 +118,7 @@ sub _servicePostPassword {
  163.  
  164.      # get info on device_id
  165.      my $device_id = $args->{device_id};
  166. -    my $store_info = $ts->getBrandAndStoreFromDeviceID($device_id);
  167. +    my $store_info = $ts->getBrandAndStoreFromDeviceID({'device_id' => $device_id});
  168.  
  169.      my $device_id_messaging = '';
  170.      unless ($device_id) {
  171.  
  172. diff --git a/sites/shared/admin/protected/omnichannel/hubaccess.tmpl b/sites/shared/admin/protected/omnichannel/hubaccess.tmpl
  173. index 8347b33..bdf0daf 100644
  174. --- a/sites/shared/admin/protected/omnichannel/hubaccess.tmpl
  175. +++ b/sites/shared/admin/protected/omnichannel/hubaccess.tmpl
  176. @@ -62,6 +62,26 @@
  177.      <div id="store_result" style="height:70px;">
  178.      </div>
  179.    </div>
  180. +
  181. +
  182. +  <div id="device_info_container" style="width: 750px; border: 1px solid black; margin-top: 25px; margin-left:auto; margin-right: auto;">
  183. +    <center>
  184. +      <h2>Device Information</h2>
  185. +      <h3>Enter a device id and see the data returned for it</h3>
  186. +    </center>
  187. +    <div id="device_form">
  188. +      <form action="#">
  189. +        <center>
  190. +        Device ID: <input type="text" class="device_id">
  191. +
  192. +        <input id="device_submit" type="submit" value="Submit">
  193. +        </center>
  194. +      </form>
  195. +    </div>
  196. +    <div id="device_result" style="height:auto; margin-bottom: 15px;">
  197. +    </div>
  198. +  </div>
  199. +
  200.  </body>
  201.  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  202.  <script>
  203. @@ -137,6 +157,38 @@
  204.  
  205.      });
  206.    });
  207. +
  208. +
  209. +  jQuery('#device_submit').click(function(e) {
  210. +    e.preventDefault();
  211. +    var $deviceId = $('#device_info_container input.device_id').val();
  212. +    var html_placeholder = "<center> Fetching data... </center>";
  213. +    $("#device_result").html(html_placeholder);
  214. +    $.ajax({
  215. +      url:      '/rpc/jsonrpc.tmpl?dbgmethod=service.agent.token.get_device_data',
  216. +      dataType: 'json',
  217. +      data: {
  218. +        JSONRPC: JSON.stringify([{
  219. +                method: 'service.agent.token.get_device_data',
  220. +                params: [{
  221. +                    device_id: $deviceId,
  222. +                    show_debug: 1,
  223. +                }]
  224. +            }]),
  225. +        },
  226. +      success: function(data) {
  227. +        var html_out = "<center> \
  228. +            <div><span style='font-weight: bold;'>Raw Brand Info: </span><span>" + data[0].result.value.brand_id_raw + "</span></div> \
  229. +            <div><span style='font-weight: bold;'>Translated Brand ID: </span><span>" + data[0].result.value.brand_id + "</span></div> \
  230. +            <div><span style='font-weight: bold;'>Store ID: </span><span>" + data[0].result.value.store_id + "</span></div> \
  231. +            <div><span style='font-weight: bold;'>Data Source: </span><span>" + data[0].result.value.data_source + "</span></div> \
  232. +            <div><span style='font-weight: bold;'>GIS Error Info (If applicable): </span><span>" + data[0].result.value.gis_error + "</span></div> \
  233. +            </center>";
  234. +        $("#device_result").html(html_out);
  235. +      }
  236. +
  237. +    });
  238. +  });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement