From 36f2e4ebdd240b34c3791e9e421c5c8973edb2ad Mon Sep 17 00:00:00 2001 From: Mark Williams Date: Tue, 29 Oct 2024 13:56:09 +0000 Subject: [PATCH] LIMS-1490: Use callback URL for incoming dewars via shipping service --- .../Controllers/AuthenticationController.php | 3 +- api/src/Page/Shipment.php | 84 +++++++++++++++++-- .../src/js/templates/shipment/shipment.html | 2 + 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/api/src/Controllers/AuthenticationController.php b/api/src/Controllers/AuthenticationController.php index 0db43e3f3..bbdbf18ff 100644 --- a/api/src/Controllers/AuthenticationController.php +++ b/api/src/Controllers/AuthenticationController.php @@ -119,7 +119,8 @@ private function checkAuthRequiredForSpecificSituations($parts): bool ($parts[0] == 'shipment' && $parts[1] == 'containers' && $parts[2] == 'history' && in_array($_SERVER["REMOTE_ADDR"], $bcr)) || # Allow shipping service to update dewar status - ($parts[0] == 'shipment' && $parts[1] == 'dewars' && $parts[2] == 'confirmdispatch') + ($parts[0] == 'shipment' && $parts[1] == 'dewars' && $parts[2] == 'confirmdispatch') || + ($parts[0] == 'shipment' && $parts[1] == 'dewars' && $parts[2] == 'confirmpickup') ) { $need_auth = false; diff --git a/api/src/Page/Shipment.php b/api/src/Page/Shipment.php index fdb44b725..a9c8d3fb6 100644 --- a/api/src/Page/Shipment.php +++ b/api/src/Page/Shipment.php @@ -146,6 +146,7 @@ class Shipment extends Page 'TOKEN' => '\w+', 'tracking_number' => '\w+', 'AWBURL' => '[\w\:\/\.\-]+', + 'pickup_confirmation_code' => '\w+', 'manifest' => '\d', 'currentuser' => '\d', @@ -200,6 +201,7 @@ class Shipment extends Page array('/dewars/transfer', 'post', '_transfer_dewar'), array('/dewars/dispatch', 'post', '_dispatch_dewar'), array('/dewars/confirmdispatch/did/:did/token/:TOKEN', 'post', '_dispatch_dewar_confirmation'), + array('/dewars/confirmpickup/sid/:sid/token/:TOKEN', 'post', '_pickup_dewar_confirmation'), array('/dewars/tracking(/:DEWARID)', 'get', '_get_dewar_tracking'), @@ -1428,7 +1430,70 @@ function _dispatch_dewar_confirmation() $this->_output(1); } + function _pickup_dewar_confirmation() + { + if (!$this->has_arg('sid')) + $this->_error('No shipment specified'); + if (!$this->has_arg('TOKEN')) + $this->_error('No token specified'); + if (!$this->has_arg('tracking_number')) + $this->_error('No tracking number specified'); + + // Check token against each dewar + $dewars = $this->db->pq( + "SELECT d.dewarid, + json_unquote(json_extract(d.extra, '$.token')) as token + FROM dewar d + WHERE d.shippingid=:1", + array($this->arg('sid')) + ); + + foreach ($dewars as $dew) { + if ($this->arg('TOKEN') !== $dew['TOKEN']) { + $this->_error('Incorrect token'); + } + } + + $this->db->pq("UPDATE shipping set shippingstatus='awb created' WHERE shippingid=:1", array($this->arg('sid'))); + + foreach ($dewars as $dew) { + // Update the dewar status and storage location + $this->db->pq( + "UPDATE dewar + set dewarstatus='awb created', storagelocation='off-site', trackingnumbertosynchrotron=:2 + WHERE dewarid=:1", + array($dew['DEWARID'], $this->arg('tracking_number')) + ); + + // Update dewar transport history + $this->db->pq( + "INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate) + VALUES (s_dewartransporthistory.nextval,:1,'awb created','off-site',CURRENT_TIMESTAMP) + RETURNING dewartransporthistoryid INTO :id", + array($dew['DEWARID']) + ); + } + + if ($this->has_arg('pickup_confirmation_code')) { + + $this->db->pq("UPDATE shipping set shippingstatus='pickup booked' WHERE shippingid=:1", array($this->arg('sid'))); + + foreach ($dewars as $dew) { + // Update the dewar status + $this->db->pq("UPDATE dewar set dewarstatus='pickup booked' WHERE dewarid=:1", array($dew['DEWARID'])); + + // Update dewar transport history (plus 1s so history appears in order) + $this->db->pq( + "INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate) + VALUES (s_dewartransporthistory.nextval,:1,'pickup booked','off-site',CURRENT_TIMESTAMP+1) + RETURNING dewartransporthistoryid INTO :id", + array($dew['DEWARID']) + ); + } + } + $this->_output(1); + } function _get_dewar_tracking() { @@ -3058,20 +3123,27 @@ function($package, $index) {return array("piecenumber" => $index+1, "licenseplat function _create_shipment_shipment_request($shipment, array $dewars): int { - // if (!is_null($shipment['EXTERNALSHIPPINGIDTOSYNCHROTRON'])) { - // return $shipment['EXTERNALSHIPPINGIDTOSYNCHROTRON']; - // } + $shipping_id = (int) $shipment['SHIPPINGID']; + + $token = md5(uniqid()); + $this->db->pq( + "UPDATE dewar SET extra = JSON_SET(IFNULL(extra, '{}'), '$.token', :1 ) WHERE shippingid=:2", + array($token, $shipping_id) + ); + + $callback_url = "/api/shipment/dewars/confirmpickup/sid/{$shipping_id}/token/{$token}"; $external_shipping_id = $this->_create_dewars_shipment_request( $dewars, $shipment['PROP'], - (int) $shipment['SHIPPINGID'], - (int) $shipment['SHIPPINGID'] + $shipping_id, + $shipping_id, + $callback_url ); $this->db->pq( "UPDATE shipping SET externalShippingIdToSynchrotron=:1 WHERE shippingId=:2", - array($external_shipping_id, $shipment['SHIPPINGID']) + array($external_shipping_id, $shipping_id) ); return $external_shipping_id; } diff --git a/client/src/js/templates/shipment/shipment.html b/client/src/js/templates/shipment/shipment.html index a58058375..b22f522fd 100644 --- a/client/src/js/templates/shipment/shipment.html +++ b/client/src/js/templates/shipment/shipment.html @@ -70,6 +70,7 @@

Shipment: <%-SHIPPI Safety Level <%-SAFETYLEVEL%> + <% if (!EXTERNALSHIPPINGIDTOSYNCHROTRON || !app.options.get("shipping_service_app_url_incoming")) { %>
  • Courier <%-DELIVERYAGENT_AGENTNAME%> @@ -126,6 +127,7 @@

    Shipment: <%-SHIPPI Estimated Delivery Date <%-DELIVERYAGENT_DELIVERYDATE%>

  • + <% } %> <% DYNAMIC=(typeof DYNAMIC !== 'undefined')? DYNAMIC : 'No'; REMOTEORMAILIN=(typeof REMOTEORMAILIN !== 'undefined')? REMOTEORMAILIN : null;