Ejemplo n.º 1
0
        private Task SendNotification(List <string> UsersName, object payload, string token, string eventCode)
        {
            try
            {
                return(Task.Run(() =>
                {
                    var json = JsonConvert.SerializeObject(payload);
                    string url = string.Empty;

                    List <Dictionary <string, string> > dicPayload = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(json);

                    Notification notification = new Notification(UsersName, eventCode, dicPayload);

                    // No need to wait for notification to complete
                    InterServiceCommunication.PostAsync(ServiceAddress.NotificationsUrl, token, notification);

                    return;
                }));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception " + e.Message);
            }

            return(null);
        }
Ejemplo n.º 2
0
        private async Task AddOtherValues(AppointmentModel model, string token)
        {
            if (model == null)
            {
                return;
            }

            model.AppointmentType = await this.ApptTypeSet.SingleOrDefaultAsync(apType => apType.Id == model.SubTypeId);

            model.Reason = await this.ReasonSet.SingleOrDefaultAsync(rsn => rsn.Id == model.ReasonId);

            string respString = await InterServiceCommunication.GetAsync(ServiceAddress.ClientSearchUrl + model.ClientId, token);

            if (respString != null)
            {
                var data = (JsonConvert.DeserializeObject(respString) as dynamic)?["data"];
                model.ClientName     = data?["client_name"] != null ? data["client_name"] : "Unknown";
                model.ClientImageUrl = data?["img_url"];
                model.ClientEmailId  = data?["email"];
            }
            else
            {
                model.ClientName = "Unknown";
            }

            respString = await InterServiceCommunication.GetAsync("http://52.168.38.130/api/patients/" + model.PatientId, token);

            if (respString != null)
            {
                var data = (JsonConvert.DeserializeObject(respString) as dynamic)?["data"];
                model.PatientName     = data?["patient_name"] != null ? data["patient_name"] : "Unknown";
                model.PatientImageUrl = data?["img_url"];
            }
            else
            {
                model.PatientName = "Unknown";
            }

            respString = await InterServiceCommunication.GetAsync(ServiceAddress.ResourceSearchUrl + model.DoctorId, token);

            if (respString != null)
            {
                var data = (JsonConvert.DeserializeObject(respString) as dynamic)?["data"];
                model.DoctorName = data?["resource_name"] != null ? data["resource_name"] : "Unknown";
            }
            else
            {
                model.DoctorName = "Unknown";
            }
        }
Ejemplo n.º 3
0
        private async Task SendEmailNotification(string fromEmail, int toId, string subject, string body, string token, string ccEmailAddress = "")
        {
            try
            {
                var clientResult = await InterServiceCommunication.GetAsync(ServiceAddress.ClientSearchUrl + toId.ToString(), token);

                if (clientResult != null)
                {
                    var data = (JsonConvert.DeserializeObject(clientResult) as dynamic)?["data"];
                    if (data?["email"] != null)
                    {
                        string          email        = data["email"];
                        SendMailRequest notification = new SendMailRequest("", ccEmailAddress, email, subject, body, "", "");
                        // No need to wait for notification to complete
                        InterServiceCommunication.PostAsync(ServiceAddress.EmailNotificationUrl, token, notification);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception " + e.Message);
            }
        }
Ejemplo n.º 4
0
        private async Task NotifyResource(int resourceId, object payload, string token, string operation)
        {
            try
            {
                var resourceResult = await InterServiceCommunication.GetAsync(ServiceAddress.ResourceSearchUrl + resourceId.ToString(), token);

                if (resourceResult != null)
                {
                    var data = (JsonConvert.DeserializeObject(resourceResult) as dynamic)?["data"];
                    if (data?["email"] != null)
                    {
                        string email = data["email"];
                        SendNotification(new List <string>()
                        {
                            email
                        }, payload, token, operation);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception " + e.Message);
            }
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <AppointmentModel> > AddAllOtherValues(IEnumerable <AppointmentModel> apptList, string token)
        {
            if (apptList == null || !apptList.Any())
            {
                return(null);
            }

            HashSet <int> clientIdList = new HashSet <int>(), patientIdList = new HashSet <int>(), resourceIdList = new HashSet <int>();
            HashSet <int> apptTypeIdList = new HashSet <int>(), reasonIdList = new HashSet <int>();

            foreach (var appt in apptList)
            {
                appt.ClientName  = "Unknown";
                appt.DoctorName  = "Unknown";
                appt.PatientName = "Unknown";

                clientIdList.Add(appt.ClientId);
                patientIdList.Add(appt.PatientId);
                resourceIdList.Add(appt.DoctorId);
                apptTypeIdList.Add(appt.SubTypeId);
                reasonIdList.Add(appt.ReasonId);
            }

            List <Task> tasks = new List <Task>();

            Dictionary <int, AppointmentTypeModel> apptTypeMap = null;
            Dictionary <int, ReasonCodeModel>      reasonMap   = null;
            Dictionary <int, dynamic> clientsMap  = new Dictionary <int, dynamic>();
            Dictionary <int, dynamic> patientsMap = new Dictionary <int, dynamic>();
            Dictionary <int, dynamic> doctorsMap  = new Dictionary <int, dynamic>();

            tasks.Add(Task.Run(() =>
            {
                apptTypeMap = this.ApptTypeSet.ToDictionary(o => o.Id);
                reasonMap   = this.ReasonSet.ToDictionary(o => o.Id);
            }));

            tasks.Add(Task.Run(async() =>
            {
                var clientResult = await InterServiceCommunication.PostAsync(ServiceAddress.ClientSearchUrl + "searchall", token, clientIdList);

                if (clientResult != null)
                {
                    var data = (JsonConvert.DeserializeObject(clientResult) as dynamic)?["data"];

                    if (data != null)
                    {
                        foreach (dynamic obj in data)
                        {
                            clientsMap.Add(Convert.ToInt32(obj["id"]), obj);
                        }
                    }
                }
            }));

            tasks.Add(Task.Run(async() =>
            {
                var patientResult = await InterServiceCommunication.PostAsync("http://52.168.38.130/api/patients/searchall", token, patientIdList);

                if (patientResult != null)
                {
                    var data = (JsonConvert.DeserializeObject(patientResult) as dynamic)?["data"];

                    if (data != null)
                    {
                        foreach (dynamic obj in data)
                        {
                            patientsMap.Add(Convert.ToInt32(obj["id"]), obj);
                        }
                    }
                }
            }));

            tasks.Add(Task.Run(async() =>
            {
                var doctorResult = await InterServiceCommunication.PostAsync(ServiceAddress.ResourceSearchUrl + "searchall", token, resourceIdList);

                if (doctorResult != null)
                {
                    var data = (JsonConvert.DeserializeObject(doctorResult) as dynamic)?["data"];

                    if (data != null)
                    {
                        foreach (dynamic obj in data)
                        {
                            doctorsMap.Add(Convert.ToInt32(obj["id"]), obj);
                        }
                    }
                }
            }));

            await Task.WhenAll(tasks);

            foreach (var appt in apptList)
            {
                appt.AppointmentType = apptTypeMap[appt.SubTypeId];
                appt.Reason          = reasonMap[appt.ReasonId];

                if (clientsMap.ContainsKey(appt.ClientId))
                {
                    appt.ClientName     = clientsMap[appt.ClientId]["name"];
                    appt.ClientImageUrl = clientsMap[appt.ClientId]["imageUrl"];
                }

                if (patientsMap.ContainsKey(appt.PatientId))
                {
                    appt.PatientName     = patientsMap[appt.PatientId]["name"];
                    appt.PatientImageUrl = patientsMap[appt.PatientId]["imageUrl"];
                }

                if (doctorsMap.ContainsKey(appt.DoctorId))
                {
                    appt.DoctorName = doctorsMap[appt.DoctorId]["resourceName"];
                }
            }

            return(apptList);
        }
Ejemplo n.º 6
0
        public async Task <MarsResponse> UpdateStatusWithNotification(AppointmentModel item, string eventCode, string token)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var mod = await this.ModelSet.FirstOrDefaultAsync(c => c.Id == item.Id);

            if (mod != null)
            {
                if (item.Status != 0)
                {
                    mod.Status = item.Status;
                }
                if (item.WorkflowState != 0)
                {
                    mod.WorkflowState = item.WorkflowState;
                }

                await this.SaveChangesAsync();
            }

            stopwatch.Stop();
            this.logHandler.LogMetric(this.GetType().ToString() + " UpdateStatus", stopwatch.ElapsedMilliseconds);

            if (mod != null)
            {
                NotificationPayload payload = new NotificationPayload(mod.Id);

                string email = string.Empty;
                string name  = string.Empty;

                if (mod.DoctorId > 0 &&
                    (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0 ||
                     eventCode.CompareTo(NotificationEvent.RESPOND) == 0 ||
                     eventCode.CompareTo(NotificationEvent.CLAIM) == 0))
                {
                    var resourceResult = await InterServiceCommunication.GetAsync(ServiceAddress.ResourceSearchUrl + mod.DoctorId.ToString(), token);

                    if (resourceResult != null)
                    {
                        var data = (JsonConvert.DeserializeObject(resourceResult) as dynamic)?["data"];
                        if (data?["email"] != null)
                        {
                            email = data["email"];
                        }
                        if (data?["resource_name"] != null)
                        {
                            name = data["resource_name"];
                        }
                    }
                }

                if (eventCode.CompareTo(NotificationEvent.RESPOND) == 0)
                {
                    payload.SenderEmail = email;
                    payload.SenderName  = name;
                    if (!string.IsNullOrEmpty(item.Response))
                    {
                        payload.Message = item.Response;
                    }
                    else
                    {
                        payload.Message = string.Empty;
                    }
                }
                else if (eventCode.CompareTo(NotificationEvent.CLAIM) == 0)
                {
                    payload.SenderEmail = email;
                }
                else if (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0)
                {
                    payload.SenderEmail = email;
                }

                if (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0 &&
                    !string.IsNullOrEmpty(email))
                {
                    List <string> objEmailList = new List <string>();
                    objEmailList.Add(email);

                    SendNotification(objEmailList, new List <NotificationPayload>()
                    {
                        payload
                    }, token, NotificationEvent.CHECKIN_NOTIFY);
                }

                SendNotification(null, new List <NotificationPayload>()
                {
                    payload
                }, token, eventCode);
            }

            return(MarsResponse.GetResponse(mod, mod == null ? HttpStatusCode.NotFound : HttpStatusCode.OK));
        }
        public async Task <MarsResponse> SearchAvailabilityAsync(AvailabilitySerachModel availabilitySearch, String token)
        {
            string errMsg = string.Empty;

            //Validate input
            errMsg = await ValidateAvailabilitySearchCriteriaAsync(availabilitySearch.PatientId, availabilitySearch.AppointmentDate);

            if (!string.IsNullOrEmpty(errMsg))
            {
                var errResp = MarsResponse.GetResponse(string.Empty, HttpStatusCode.BadRequest);
                errResp.Error = new MarsException(errMsg);
                return(errResp);
            }

            //Fetch prefered doctor for given patient id from marsstorage.[dbo].[Patients] tablle.
            string patientsUrl = ServiceAddress.PatientsBaseUrl + availabilitySearch.PatientId;
            var    patient     = await InterServiceCommunication.GetAsync(patientsUrl, token);

            int prefDoctorID = 0;

            if (patient != null)
            {
                dynamic patientData = JsonConvert.DeserializeObject(patient);
                var     data        = patientData["data"];

                if (data["pref_doctor"] != null)
                {
                    prefDoctorID = data["pref_doctor"];
                }
            }

            //Fetch doctor availability from marspoc.dbo.[Availability]
            string resourcesUrl = ServiceAddress.AvailabilityBaseUrl;   //"http://localhost:5000/api/resource/availability/";   //
            var    availability = string.Empty;

            if (prefDoctorID > 0)
            {
                var body = new AvailabilityRequest
                {
                    Id         = 0,
                    Date       = availabilitySearch.AppointmentDate.Date,
                    StartTime  = new TimeSpan(),
                    EndTime    = new TimeSpan(),
                    ResourceId = prefDoctorID
                };

                availability = await InterServiceCommunication.PostAsync(resourcesUrl, token, body);
            }
            dynamic availabilityData = JsonConvert.DeserializeObject(availability);
            dynamic avData           = null;

            if (!string.IsNullOrEmpty(availability))
            {
                avData = availabilityData["data"];
            }

            if (prefDoctorID == 0 || avData.First == null)
            {
                availability = await InterServiceCommunication.GetAsync(resourcesUrl, token);

                availabilityData = JsonConvert.DeserializeObject(availability);
                if (!string.IsNullOrEmpty(availability))
                {
                    avData = availabilityData["data"];
                }
            }

            DateTime startTime;// = new DateTime();
            DateTime endTime;
            String   doctorName = string.Empty;
            int      docId      = 0;
            //DateTime bookingTime;
            TimeSpan hospitalStartTime;
            TimeSpan hospitalEndTime;

            TimeSpan.TryParse(configuration.GetSection("Hospital").GetSection("StartTime").Value, out hospitalStartTime);
            TimeSpan.TryParse(configuration.GetValue <string>("Hospital:EndTime"), out hospitalEndTime);
            List <AvailabilityModel> timeslots = new List <AvailabilityModel>();

            if (!string.IsNullOrEmpty(availability) && avData.First != null)
            {
                //Fetch default time for given appointment type from master data(mars_appointments.[dbo].[appointment_types])
                int defaultDuration = availabilitySearch.SubTypeId > 0? GetDefaultDurationOfApptType(availabilitySearch.SubTypeId) : 30;

                if (defaultDuration > 0)
                {
                    //dynamic availabilityData = JsonConvert.DeserializeObject(availability);
                    //dynamic data = availabilityData["data"];
                    //List<AvailabilityModel> availableResources = JsonConvert.DeserializeObject<List<AvailabilityModel>>(JsonConvert.SerializeObject(data));

                    foreach (dynamic d in avData)
                    {
                        if (timeslots.Count > 0)
                        {
                            break;
                        }

                        //startTime = ((DateTime)d["date"]).Add(((TimeSpan)d["startTime"] > hospitalStartTime && (TimeSpan)d["startTime"] < hospitalEndTime) ? (TimeSpan)d["startTime"] : hospitalStartTime);
                        //endTime = ((DateTime)d["date"]).Add( (TimeSpan)d["endTime"] );
                        startTime  = ((DateTime)d["date"]).Add((TimeSpan)d["startTime"]);
                        endTime    = ((DateTime)d["date"]).Add((TimeSpan)d["endTime"]);
                        docId      = d["resourceId"];
                        doctorName = d["name"];

                        //bookingTime = new DateTime(1970, 1, 1).AddMilliseconds(availabilitySearch.ClientReqTime).ToLocalTime();// .AddHours(-7);   //setting to PST time   // .ToLocalTime();
                        DateTime bookingTime = RoundUp(DateTime.Parse(DateTime.Now.ToString()), TimeSpan.FromMinutes(30));
                        if (bookingTime >= endTime)
                        {
                            continue;
                        }
                        else if (bookingTime > startTime && bookingTime < endTime)
                        {
                            //string s = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
                            //string st = String.Format("{0:s}", DateTime.Now);
                            //string st = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:00");
                            startTime = Convert.ToDateTime(bookingTime.ToString("yyyy-MM-ddTHH:mm:00"));   //startTime = startTime.Add(DateTime.Now.AddSeconds(-DateTime.Now.Second).TimeOfDay);
                            if (endTime.Subtract(startTime).TotalMinutes < defaultDuration)
                            {
                                continue;
                            }
                        }

                        //Fetch Appointments for given resource from mars_appointment.[dbo].[appointments] table
                        AppointmentModel appModel = new AppointmentModel
                        {
                            DoctorId = docId    //prefDoctorID > 0 ? prefDoctorID : docId
                        };
                        var appointments = this.appointmentContext.SearchModelByResourceIdAndDate(appModel, startTime.Date);
                        if (appointments.ToList().Count > 1)
                        {
                            appointments = appointments.Where(a => a.EndTime >= startTime && a.StartTime <= endTime).OrderBy(a => a.StartTime);
                        }


                        //Find availability based on default duration of appointment type and already booked appointments if any.
                        if (availabilitySearch.DefaultSlot)
                        {
                            timeslots = FindDefaultTimeslot(appointments.ToList(), startTime, endTime, defaultDuration);
                        }
                        else
                        {
                            timeslots = FindAllAvailableTimeslots(appointments.ToList(), startTime, endTime, defaultDuration);
                        }


                        if (timeslots != null && timeslots.Count > 0)
                        {
                            foreach (var t in timeslots)
                            {
                                t.DoctorId        = docId;
                                t.DoctorName      = doctorName;
                                t.AppointmentDate = startTime.Date;
                            }
                            //if (availabilitySearch.DefaultSlot || timeslots.Count == 1)
                            //{
                            //    timeslots.FirstOrDefault().DoctorId = prefDoctorID;
                            //    timeslots.FirstOrDefault().AppointmentDate = availabilitySearch.AppointmentDate.Date;
                            //    timeslots.FirstOrDefault().DoctorName = doctorName;
                            //}
                        }
                    }
                }
            }

            MarsResponse resp = MarsResponse.GetResponse(timeslots);

            if (timeslots == null)
            {
                resp.Code = HttpStatusCode.NotAcceptable;
            }
            else if (timeslots.Count() == 0)
            {
                //resp.Code = HttpStatusCode.Accepted;
                resp.Error = new MarsException("Time slots not found.");
            }

            return(resp);
        }