public static bool ParseCsv(HttpPostedFileBase upload, long pharmacyId)
        {
            //Check if valid file was uploaded
            if (upload != null && upload.ContentLength > 0 && upload.FileName.EndsWith(".csv"))
            {
                //Convert binary file received from request into text
                var textdata = string.Empty;
                using (BinaryReader b = new BinaryReader(upload.InputStream)) {
                    var binData = b.ReadBytes(upload.ContentLength);
                    textdata = Encoding.UTF8.GetString(binData);
                }

                //Get list of patients from database for comparison
                var patients    = new Dictionary <string, Patient>();
                var patientlist = DatabasePatientService.GetAll(pharmacyId);
                foreach (var p in patientlist)
                {
                    p.LoadUserData();
                    patients.Add(p.PersonCode, p);
                }

                //Interate over each line of text in the file
                var text = new StringReader(textdata);
                var line = string.Empty;

                //Remove headers from file
                text.ReadLine();

                while ((line = text.ReadLine()) != null)
                {
                    var row = line.Split(',');

                    //Check if patient exists
                    try {
                        var dateNow = DateTime.Now;

                        Patient patient = new Patient()
                        {
                            PersonCode  = row[0],
                            FirstName   = row[1],
                            LastName    = row[2],
                            DateOfBirth = DateTime.ParseExact(row[3], "yyyyMMdd", null),
                            Phone       = row[5],
                            Email       = row[6],
                            PharmacyId  = pharmacyId
                        };
                        if (patients.ContainsKey(row[0]))
                        {
                            //Update patient

                            var oldPatient = patients[row[0]];

                            patient.UserId              = oldPatient.UserId;
                            patient.PatientId           = patients[row[0]].PatientId;
                            patient.Type                = oldPatient.Type;
                            patient.UserLogin           = oldPatient.UserLogin;
                            patient.ContactMethod       = oldPatient.ContactMethod;
                            patient.PreferedContactTime = oldPatient.PreferedContactTime;
                            patient.SendBirthdayMessage = oldPatient.SendBirthdayMessage;
                            patient.SendRefillMessage   = oldPatient.SendRefillMessage;
                            DatabaseUserService.Update(patient);
                            DatabasePatientService.Update(patient);
                            DatabaseUserService.Enable(patient.PatientId);
                            DatabasePatientService.Enable(patient.PatientId);
                        }
                        else
                        {
                            //Create patient
                            patient.PreferedContactTime = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 15, 0, 0);
                            patient.UserId    = DatabaseUserService.Insert(patient);
                            patient.PatientId = DatabasePatientService.Insert(patient);
                        }

                        //Check if prescription exists
                        try {
                            var prescriptionId = Convert.ToInt32(row[8]);
                            var prescription   = new Prescription()
                            {
                                PrescriptionDateFilled = DateTime.ParseExact(row[7], "yyyyMMdd", null),
                                PrescriptionNumber     = prescriptionId,
                                PrescriptionId         = prescriptionId,
                                PrescriptionDaysSupply = Convert.ToInt32(row[9]),
                                PrescriptionRefills    = Convert.ToInt32(row[10]),
                                PrescriptionUpc        = row[11],
                                PrescriptionName       = row[12],
                                PatientId = patient.PatientId
                            };

                            DatabasePrescriptionService.InsertOrUpdate(prescription);

                            if (DatabaseRefillService.GetByPrescriptionId(prescription.PrescriptionId) == null)
                            {
                                var refill = new Refill(prescription)
                                {
                                };
                                DatabaseRefillService.Insert(refill);
                            }
                        } catch (Exception e) {
                            //Ignore prescriptions that fail the model building
                        }
                    } catch (Exception e) {
                        //Do not add patients which fail the model building
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        /**
         * Compiles a notification into an email. Fills template placeholders
         * with actual data, sets proper email parameters, and renders the body
         * of the email based on notification type.
         *
         * @param - Notifiation - the notification to compile
         * @returns - MailMessage - the compiled email with rendered body
         */
        public static MailMessage Build(Notification notification)
        {
            //Get necessary data to build and format the email
            var patient  = DatabasePatientService.GetById(notification.PatientId);
            var user     = DatabaseUserService.GetById(patient.UserId);
            var pharmacy = DatabasePharmacyService.GetById(patient.PharmacyId);

            var message = new MailMessage();

            message.To.Add(new MailAddress(user.Email));
            message.From = new MailAddress(ConfigurationManager.AppSettings["SendEmailAddress"]);

            //Emails always get style sheet and header
            var body       = EmailHtmlLoader.TemplateHtml;
            var content    = "";
            var emailtitle = "";

            //Set email subject and body based on type of email
            switch (notification.Type)
            {
            case Notification.NotificationType.Birthday:
                message.Subject = "Happy Birthday, from " + pharmacy.PharmacyName + "!";
                content        += EmailHtmlLoader.BirthdayHtml;
                emailtitle      = "Happy Birthday!";
                break;

            case Notification.NotificationType.Ready:
                message.Subject = "Your Refill is ready to be picked up";
                content        += EmailHtmlLoader.ReadyHtml;
                emailtitle      = "You have a refill ready to be picked up";
                break;

            case Notification.NotificationType.Recall:
                message.Subject  = "A Prescription you received has been recalled!";
                message.Priority = MailPriority.High;
                content         += EmailHtmlLoader.RecallHtml;
                emailtitle       = "There has been a recall on a prescription you received";
                break;

            case Notification.NotificationType.Refill:
                message.Subject = "Your medication is up for refill";
                content        += EmailHtmlLoader.RefillHtml;
                emailtitle      = "Would you like to refill your medication with us?";
                break;

            case Notification.NotificationType.Reset:
                break;

            default:
                message.Subject = "Unknown Notification Type";
                break;
            }

            //Set contact reason message
            var reason = "You are receiving this email because ";

            if (notification.Type == Notification.NotificationType.Recall)
            {
                reason += "this is a mandatory email from your pharmacy. " +
                          "If you have any questions please call" + pharmacy.PharmacyPhone +
                          " to speak with your pharmacist.";
            }
            else
            {
                reason += "of your personal contact preferences. If you wish to unsubscribe" +
                          " from all future emails, please click the button below or contact" +
                          " your pharmacist at " + pharmacy.PharmacyPhone + ".";
            }
            body = body.Replace("{{ContactReason}}", reason);

            //Replace html template placeholder with renderbody
            body = body.Replace("{{EmailBody}}", content);
            body = body.Replace("{{EmailTitle}}", emailtitle);
            body = body.Replace("{{MessageText}}", notification.NotificationMessage);

            //Replace sentinels in email with personalized data
            body = body.Replace("{{PharmacyName}}", pharmacy.PharmacyName);
            body = body.Replace("{{PharmacyPhone}}", pharmacy.PharmacyPhone);
            body = body.Replace("{{PharmacyAddress}}", pharmacy.PharmacyAddress);
            body = body.Replace("{{Name}}", patient.GetFullName());
            body = body.Replace("{{FirstName}}", patient.FirstName);
            body = body.Replace("{{LastName}}", patient.LastName);
            body = body.Replace("{{Phone}}", patient.Phone);
            body = body.Replace("{{Email}}", patient.Email);
            body = body.Replace("{{DOBShort}}", patient.DateOfBirth.ToShortDateString());
            body = body.Replace("{{DOBLong}}", patient.DateOfBirth.ToLongDateString());
            body = body.Replace("{{ContactTimeShort}}", patient.PreferedContactTime.ToShortTimeString());
            body = body.Replace("{{ContactTimeLong}}", patient.PreferedContactTime.ToLongTimeString());

            //Set up links
            body = body.Replace("{{OtpCode}}", OTPService.GenerateEmailOtp(notification).Code);
            body = body.Replace("{{PatientId}}", patient.PatientId.ToString());
            body = body.Replace("{{RespondLink}}", "http://localhost:50082/email/respond");
            body = body.Replace("{{UnsubscribeLink}}", "http://localhost:50082/email/unsubscribe");

            message.Body       = body;
            message.IsBodyHtml = true;

            return(message);
        }