async def error_masterrecords( message_id: str, user: UKRDCUser = Security(auth.get_user()), errorsdb: Session = Depends(get_errorsdb), jtrace: Session = Depends(get_jtrace), audit: Auditer = Depends(get_auditer), ): """Retreive MasterRecords associated with a specific error message""" message = get_message(errorsdb, message_id, user) # Get masterrecords directly referenced by the error records = (jtrace.query(MasterRecord).filter( MasterRecord.nationalid == message.ni).all()) message_audit = audit.add_event(Resource.MESSAGE, message.id, MessageOperation.READ) for record in records: audit.add_event( Resource.MASTER_RECORD, record.id, AuditOperation.READ, parent=message_audit, ) return records
def get_workitems_related_to_message( jtrace: Session, errorsdb: Session, message_id: str, user: UKRDCUser ) -> Query: """Get a list of WorkItems related via the Patient Number to a given Message Args: jtrace (Session): JTRACE SQLAlchemy session errorsdb (Session): ERRORSDB SQLAlchemy session message_id (str): Message ID user (UKRDCUser): Logged-in user Returns: Query: SQLAlchemy query """ error = get_message(errorsdb, message_id, user) # Get masterrecords directly referenced by the error direct_records: list[MasterRecord] = ( jtrace.query(MasterRecord).filter(MasterRecord.nationalid == error.ni).all() ) # Get workitems related to masterrecords directly referenced by the error return jtrace.query(WorkItem).filter( WorkItem.master_id.in_([record.id for record in direct_records]), WorkItem.status == 1, )
def error_detail( message_id: str, user: UKRDCUser = Security(auth.get_user()), errorsdb: Session = Depends(get_errorsdb), audit: Auditer = Depends(get_auditer), ): """Retreive detailed information about a specific error message""" # For some reason the fastAPI response_model doesn't call our channel_name # validator, meaning we don't get a populated channel name unless we explicitly # call it here. message = get_message(errorsdb, message_id, user) audit.add_event(Resource.MESSAGE, message.id, MessageOperation.READ) return MessageSchema.from_orm(message)
async def error_workitems( message_id: str, user: UKRDCUser = Security(auth.get_user()), errorsdb: Session = Depends(get_errorsdb), jtrace: Session = Depends(get_jtrace), audit: Auditer = Depends(get_auditer), ): """Retreive WorkItems associated with a specific error message""" message = get_message(errorsdb, message_id, user) workitems = get_workitems_related_to_message(jtrace, errorsdb, str(message.id), user).all() message_audit = audit.add_event(Resource.MESSAGE, message.id, MessageOperation.READ) for item in workitems: audit.add_workitem(item, parent=message_audit) return workitems
async def error_source( message_id: str, user: UKRDCUser = Security(auth.get_user()), errorsdb: Session = Depends(get_errorsdb), mirth: MirthAPI = Depends(get_mirth), audit: Auditer = Depends(get_auditer), ): """Retreive detailed information about a specific error message""" error = get_message(errorsdb, message_id, user) if not error.channel_id: raise HTTPException(404, "Channel ID not found in Mirth") message = await mirth.channel(error.channel_id ).get_message(str(error.message_id), include_content=True) if not message: raise HTTPException(404, "Message not found in Mirth") first_connector_message: ConnectorMessageModel = list( message.connector_messages.values())[0] message_data: Optional[ConnectorMessageData] = None # Prioritise encoded message over raw if first_connector_message.encoded: message_data = first_connector_message.encoded elif first_connector_message.raw: message_data = first_connector_message.raw # If no data is available, return a valid but empty MessageSourceSchema if not message_data: return MessageSourceSchema(content=None, content_type=None) audit.add_event(Resource.MESSAGE, error.id, MessageOperation.READ_SOURCE) return MessageSourceSchema(content=message_data.content, content_type=message_data.data_type)
def test_get_error_user_denied(errorsdb_session, test_user): with pytest.raises(PermissionsError): messages.get_message(errorsdb_session, 2, test_user)
def test_get_error_user(errorsdb_session, test_user): error = messages.get_message(errorsdb_session, 1, test_user) assert error assert error.id == 1