예제 #1
0
    def get(self, p_id=None, order_id=None):  # pylint: disable=unused-argument

        if not p_id:
            raise BadRequest('invalid participant id')
        if not order_id:
            raise BadRequest('must include order ID to retrieve DV orders.')

        pk = {'participant_id': p_id, 'order_id': order_id}
        obj = ObjDict(pk)
        id_ = self.dao.get_id(obj)[0]

        return super(DvOrderApi, self).get(id_=id_, participant_id=p_id)
예제 #2
0
    def _post_supply_delivery(self, resource):
        try:
            fhir = SimpleFhirR4Reader(resource)
            patient = fhir.patient
            pid = patient.identifier
            p_id = from_client_participant_id(pid.value)
            bo_id = fhir.basedOn[0].identifier.value
            _id = self.dao.get_id(
                ObjDict({
                    'participantId': p_id,
                    'order_id': int(bo_id)
                }))
            tracking_status = fhir.extension.get(
                url=DV_FHIR_URL + 'tracking-status').valueString.lower()
        except AttributeError as e:
            raise BadRequest(e.message)
        except Exception as e:
            raise BadRequest(e.message)

        if not _id:
            raise Conflict(
                'Existing SupplyRequest for order required for SupplyDelivery')
        dvo = self.dao.get(_id)
        if not dvo:
            raise Conflict(
                'Existing SupplyRequest for order required for SupplyDelivery')

        merged_resource = None
        # Note: POST tracking status should be either 'enroute/in_transit'. PUT should only be 'delivered'.
        if tracking_status in [
                'in_transit', 'enroute', 'delivered'
        ] and self._to_mayo(fhir) and not dvo.biobankOrderId:
            # Send to mayolink and create internal biobank order
            response = self.dao.send_order(resource, p_id)
            merged_resource = merge_dicts(response, resource)
            merged_resource['id'] = _id
            logging.info(
                'Sending salivary order to biobank for participant: %s', p_id)
            self.dao.insert_biobank_order(p_id, merged_resource)

        response = super(DvOrderApi, self).put(bo_id,
                                               participant_id=p_id,
                                               skip_etag=True,
                                               resource=merged_resource)
        response[2]['Location'] = '/rdr/v1/SupplyDelivery/{}'.format(bo_id)
        response[2]['auth_user'] = resource['auth_user']
        if response[1] == 200:
            created_response = list(response)
            created_response[1] = 201
            return tuple(created_response)
        return response
예제 #3
0
    def _put_supply_request(self, resource, bo_id):

        # handle invalid FHIR documents
        try:
            fhir_resource = SimpleFhirR4Reader(resource)
            barcode_url = None
            if fhir_resource.extension.get(
                    url=VIBRENT_FULFILLMENT_URL).valueString == 'shipped':
                barcode_url = fhir_resource.extension.get(
                    url=VIBRENT_BARCODE_URL).url
            pid = fhir_resource.contained.get(
                resourceType='Patient').identifier.get(
                    system=VIBRENT_FHIR_URL + 'participantId')
            p_id = from_client_participant_id(pid.value)
        except AttributeError as e:
            raise BadRequest(e.message)
        except Exception as e:
            raise BadRequest(e.message)

        merged_resource = None
        if not p_id:
            raise BadRequest(
                'Request must include participant id and must be of type int')
        if str(barcode_url) == VIBRENT_BARCODE_URL:
            _id = self.dao.get_id(
                ObjDict({
                    'participantId': p_id,
                    'order_id': int(bo_id)
                }))
            ex_obj = self.dao.get(_id)
            if not ex_obj.barcode:
                # Send to mayolink and create internal biobank order
                response = self.dao.send_order(resource, p_id)
                merged_resource = merge_dicts(response, resource)
                merged_resource['id'] = _id
                self.dao.insert_biobank_order(p_id, merged_resource)

        if merged_resource:
            response = super(DvOrderApi, self).put(bo_id,
                                                   participant_id=p_id,
                                                   skip_etag=True,
                                                   resource=merged_resource)
        else:
            response = super(DvOrderApi, self).put(bo_id,
                                                   participant_id=p_id,
                                                   skip_etag=True)

        return response
예제 #4
0
    def _post_supply_delivery(self, resource):
        fhir_resource = SimpleFhirR4Reader(resource)
        patient = fhir_resource.patient
        pid = patient.identifier
        p_id = from_client_participant_id(pid.value)
        bo_id = fhir_resource.basedOn[0].identifier.value
        pk = {'participantId': p_id, 'order_id': bo_id}
        obj = ObjDict(pk)
        if not self.dao.get_id(obj):
            raise Conflict(
                'Existing SupplyRequest for order required for SupplyDelivery')

        response = super(DvOrderApi, self).put(bo_id,
                                               participant_id=p_id,
                                               skip_etag=True)
        response[2]['Location'] = '/rdr/v1/SupplyDelivery/{}'.format(bo_id)
        if response[1] == 200:
            created_response = list(response)
            created_response[1] = 201
            return tuple(created_response)
        return response
예제 #5
0
    def _put_supply_delivery(self, resource, bo_id):

        # handle invalid FHIR documents
        try:
            fhir = SimpleFhirR4Reader(resource)
            participant_id = fhir.patient.identifier.value
            p_id = from_client_participant_id(participant_id)
            update_time = dateutil.parser.parse(fhir.occurrenceDateTime)
            carrier_name = fhir.extension.get(url=VIBRENT_FHIR_URL +
                                              'carrier').valueString
            eta = dateutil.parser.parse(
                fhir.extension.get(url=VIBRENT_FHIR_URL +
                                   "expected-delivery-date").valueDateTime)
            tracking_status = fhir.extension.get(url=VIBRENT_FHIR_URL +
                                                 'tracking-status').valueString
        except AttributeError as e:
            raise BadRequest(e.message)
        except Exception as e:
            raise BadRequest(e.message)

        tracking_status_enum = getattr(OrderShipmentTrackingStatus,
                                       tracking_status.upper(),
                                       OrderShipmentTrackingStatus.UNSET)

        biobank_dv_order_id = self.dao.get_id(
            ObjDict({
                'participantId': p_id,
                'order_id': int(bo_id)
            }))
        order = self.dao.get(biobank_dv_order_id)
        order.shipmentLastUpdate = update_time.date()
        order.shipmentCarrier = carrier_name
        order.shipmentEstArrival = eta.date()
        order.shipmentStatus = tracking_status_enum

        response = super(DvOrderApi, self).put(bo_id,
                                               participant_id=p_id,
                                               skip_etag=True)
        return response
예제 #6
0
    def _put_supply_delivery(self, resource, bo_id):
        # handle invalid FHIR documents
        try:
            fhir = SimpleFhirR4Reader(resource)
            participant_id = fhir.patient.identifier.value
            p_id = from_client_participant_id(participant_id)
            update_time = dateutil.parser.parse(fhir.occurrenceDateTime)
            carrier_name = fhir.extension.get(url=DV_FHIR_URL +
                                              'carrier').valueString

            eta = None
            if hasattr(fhir['extension'],
                       DV_FHIR_URL + 'expected-delivery-date'):
                eta = dateutil.parser.parse(
                    fhir.extension.get(url=DV_FHIR_URL +
                                       "expected-delivery-date").valueDateTime)

            tracking_status = fhir.extension.get(url=DV_FHIR_URL +
                                                 'tracking-status').valueString
            if tracking_status:
                tracking_status = tracking_status.lower()
        except AttributeError as e:
            raise BadRequest(e.message)
        except Exception as e:
            raise BadRequest(e.message)

        _id = self.dao.get_id(
            ObjDict({
                'participantId': p_id,
                'order_id': int(bo_id)
            }))
        if not _id:
            raise Conflict(
                'Existing SupplyRequest for order required for SupplyDelivery')
        dvo = self.dao.get(_id)
        if not dvo:
            raise Conflict(
                'Existing SupplyRequest for order required for SupplyDelivery')

        tracking_status_enum = \
          getattr(OrderShipmentTrackingStatus, tracking_status.upper(), OrderShipmentTrackingStatus.UNSET)

        dvo.shipmentLastUpdate = update_time.date()
        dvo.shipmentCarrier = carrier_name
        if eta:
            dvo.shipmentEstArrival = eta.date()
        dvo.shipmentStatus = tracking_status_enum
        if not p_id:
            raise BadRequest('Request must include participant id')

        merged_resource = None
        # Note: PUT tracking status should only be 'delivered'. POST should be either 'enroute/in_transit'.
        if tracking_status in [
                'in_transit', 'enroute', 'delivered'
        ] and self._to_mayo(fhir) and not dvo.biobankOrderId:
            # Send to mayolink and create internal biobank order
            response = self.dao.send_order(resource, p_id)
            merged_resource = merge_dicts(response, resource)
            merged_resource['id'] = _id
            logging.info(
                'Sending salivary order to biobank for participant: %s', p_id)
            self.dao.insert_biobank_order(p_id, merged_resource)

        response = super(DvOrderApi, self).put(bo_id,
                                               participant_id=p_id,
                                               skip_etag=True,
                                               resource=merged_resource)
        return response