public static async Task <object> Run([HttpTrigger("post")] HttpRequestMessage req, TraceWriter log) { log.Info($"{nameof(PatientAdmissionFunc)} triggered on {DateTime.UtcNow}"); using (var patientTelemetry = new PatientTelemetry(Environment.GetEnvironmentVariable(CommonConstants.InstumentationKey, EnvironmentVariableTarget.Process), nameof(PatientAdmissionFunc))) { try { var userIdentity = req.GetRequestContext().Principal.Identity as ClaimsIdentity; var authorization = new ADAuthorization(userIdentity); patientTelemetry.Caller(authorization.GetCallerName()); if (false == await authorization.Authorize(CommonConstants.CareLineManagerRole)) { patientTelemetry.Unauthorized(); return(req.CreateResponse(HttpStatusCode.Unauthorized, new { result = $"Unauthorized {HttpStatusCode.Unauthorized}" })); } // Get request data dynamic data = await req.Content.ReadAsAsync <object>(); if (data == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { result = "bad request" })); } // convert the input to the corresponding objects InPatient inPatient = data.patient.ToObject <InPatient>(); Encounter encounter = data.encounter.ToObject <Encounter>(); List <Observation> observations = data.observations.ToObject <List <Observation> >(); List <Condition> conditions = data.conditions.ToObject <List <Condition> >(); //get resource from key vault var appResources = await Settings.GetAppResources(); new SqlEncryptionHelper(); //get patient data from FHIR data var patient = FHIRMapping.PopulatePatientData(inPatient, encounter, observations, conditions); try { var predictLengthOfStayService = new PredictLengthOfStayService(appResources.PredictLengthOfStayServiceEndpoint, appResources.PredictLengthOfStayServiceApiKey); patientTelemetry.DependencyStarted(); patient.PredictedLengthOfStay = await predictLengthOfStayService.PredictLengthOfStay(patient); patientTelemetry.DependencyCompleted(nameof(PredictLengthOfStayService), "PredictLengthOfStay", true); } catch (PredictLengthOfStayServiceException ex) { patientTelemetry.DependencyCompleted(nameof(PredictLengthOfStayService), "PredictLengthOfStay", false); patientTelemetry.Error(ex); } var hospital = new Hospital(appResources.PatientDbConnectionString); var id = await hospital.AdmitPatient(patient); if (id == -1) { return(req.CreateResponse(HttpStatusCode.OK, new { result = "invalid encounter id" })); } var existingPatient = await hospital.GetPatient(id); patientTelemetry.Success(); return(req.CreateResponse(HttpStatusCode.OK, new { encounterId = existingPatient.Eid, name = $"{existingPatient.FirstName} {existingPatient.MiddleName} {existingPatient.LastName}", gender = existingPatient.Gender, admittedOn = existingPatient.Vdate })); } catch (AuthorizationException ex) { patientTelemetry.Error(ex); return(req.CreateResponse(HttpStatusCode.ExpectationFailed, new { error = ex.Message })); } catch (Exception ex) { patientTelemetry.Error(ex); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { result = "failed" })); } } }
public static async Task <object> Run([HttpTrigger("put")] HttpRequestMessage req, TraceWriter log) { log.Info($"{nameof(PatientDischargeFunc)} triggered on {DateTime.UtcNow}"); using (var patientTelemetry = new PatientTelemetry( Environment.GetEnvironmentVariable(CommonConstants.InstumentationKey, EnvironmentVariableTarget.Process), nameof(PatientDischargeFunc))) { try { //authorize user var userIdentity = req.GetRequestContext().Principal.Identity as ClaimsIdentity; var authorization = new ADAuthorization(userIdentity); patientTelemetry.Caller(authorization.GetCallerName()); if (false == await authorization.Authorize(CommonConstants.CareLineManagerRole)) { patientTelemetry.Unauthorized(); return(req.CreateResponse(HttpStatusCode.Unauthorized, new { result = $"Unauthorized {HttpStatusCode.Unauthorized}" })); } // Get request data dynamic data = await req.Content.ReadAsAsync <object>(); if (data == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { result = "bad request" })); } // convert the input to the corresponding objects InPatient inPatient = data.patient.ToObject <InPatient>(); Encounter encounter = data.encounter.ToObject <Encounter>(); int patientEid = encounter.Id; //get resources from key vault var appResources = await Settings.GetAppResources(); new SqlEncryptionHelper(); patientTelemetry.DischargingPatient(); var hospital = new Hospital(appResources.PatientDbConnectionString); var id = await hospital.DischargePatient(patientEid, encounter.Period.End); if (id == -1) { return(req.CreateResponse(HttpStatusCode.OK, new { error = "invalid encounter id" })); } var existingPatient = await hospital.GetPatient(id); patientTelemetry.PatientDischarged(); patientTelemetry.Success(); return(req.CreateResponse(HttpStatusCode.OK, new { encounterId = existingPatient.Eid, name = $"{existingPatient.FirstName} {existingPatient.MiddleName} {existingPatient.LastName}", dischargedOn = existingPatient.Discharged, daysStayed = existingPatient.Lengthofstay })); } catch (AuthorizationException ex) { patientTelemetry.Error(ex); return(req.CreateResponse(HttpStatusCode.ExpectationFailed, new { ex.Message })); } catch (Exception ex) { patientTelemetry.Error(ex); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { result = "failed" })); } } }