Exemple #1
0
        public ActionResult QueueEmails(MassEmailer m)
        {
            m.Body = GetBody(m.Body);
            if (UsesGrammarly(m))
            {
                return Json(new { error = GrammarlyNotAllowed });
            }

            if (TooLarge(m))
            {
                return Json(new { error = TooLargeError });
            }

            if (!m.Subject.HasValue() || !m.Body.HasValue())
            {
                return Json(new { id = 0, error = "Both subject and body need some text." });
            }

            if (!User.IsInRole("Admin") && m.Body.Contains("{createaccount}", ignoreCase: true))
            {
                return Json(new { id = 0, error = "Only Admin can use {createaccount}." });
            }

            if (Util.SessionTimedOut())
            {
                Session["massemailer"] = m;
                return Content("timeout");
            }

            DbUtil.LogActivity("Emailing people");

            if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0)
            {
                return Json(new { id = 0, error = "No email address to send from." });
            }

            m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text;

            int id;
            try
            {
                var eq = m.CreateQueue();
                if (eq == null)
                {
                    throw new Exception("No Emails to send (tag does not exist)");
                }

                id = eq.Id;

                // If there are additional recipients, add them to the queue
                if (m.AdditionalRecipients != null)
                {
                    foreach (var pid in m.AdditionalRecipients)
                    {
                        // Protect against duplicate PeopleIDs ending up in the queue
                        var q3 = from eqt in CurrentDatabase.EmailQueueTos
                                 where eqt.Id == eq.Id
                                 where eqt.PeopleId == pid
                                 select eqt;
                        if (q3.Any())
                        {
                            continue;
                        }
                        CurrentDatabase.EmailQueueTos.InsertOnSubmit(new EmailQueueTo
                        {
                            Id = eq.Id,
                            PeopleId = pid,
                            OrgId = eq.SendFromOrgId,
                            Guid = Guid.NewGuid(),
                        });
                    }
                    CurrentDatabase.SubmitChanges();
                }

                if (m.RecipientIds != null)
                {
                    foreach (var pid in m.RecipientIds)
                    {
                        // Protect against duplicate PeopleIDs ending up in the queue
                        var q3 = from eqt in CurrentDatabase.EmailQueueTos
                                 where eqt.Id == eq.Id
                                 where eqt.PeopleId == pid
                                 select eqt;
                        if (q3.Any())
                        {
                            continue;
                        }
                        CurrentDatabase.EmailQueueTos.InsertOnSubmit(new EmailQueueTo
                        {
                            Id = eq.Id,
                            PeopleId = pid,
                            OrgId = eq.SendFromOrgId,
                            Guid = Guid.NewGuid(),
                        });
                    }
                    CurrentDatabase.SubmitChanges();
                }

                if (eq.SendWhen.HasValue)
                {
                    return Json(new { id = 0, content = "Emails queued to be sent." });
                }
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                return Json(new { id = 0, error = ex.Message });
            }

            var host = CurrentDatabase.Host;
            var currorgid = CurrentDatabase.CurrentSessionOrgId;
            // save these from HttpContext to set again inside thread local storage
            var userEmail = Util.UserEmail;
            var isInRoleEmailTest = User.IsInRole("EmailTest");
            var isMyDataUser = User.IsInRole("Access") == false;

            try
            {
                ValidateEmailReplacementCodes(CurrentDatabase, m.Body, new MailAddress(m.FromAddress));
            }
            catch (Exception ex)
            {
                return Json(new { error = ex.Message });
            }

            var onlyProspects = m.OnlyProspects;

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                try
                {
                    var db = CurrentDatabase.Copy();
                    var cul = CurrentDatabase.Setting("Culture", "en-US");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul);
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul);
                    // set these again inside thread local storage
                    db.SetCurrentOrgId(currorgid);
                    Util.UserEmail = userEmail;
                    Util.IsInRoleEmailTest = isInRoleEmailTest;
                    Util.IsMyDataUser = isMyDataUser;
                    db.SendPeopleEmail(id, onlyProspects);
                }
                catch (Exception ex)
                {
                    var ex2 = new Exception($"Emailing error for queueid {id} on {host}\n{ex.Message}", ex);
                    var cb = new SqlConnectionStringBuilder(Util.ConnectionString) { InitialCatalog = "ELMAH" };
                    var errorLog = new SqlErrorLog(cb.ConnectionString) { ApplicationName = "BVCMS" };
                    errorLog.Log(new Error(ex2));

                    var db = CMSDataContext.Create(host);
                    var equeue = db.EmailQueues.Single(ee => ee.Id == id);
                    equeue.Error = ex.Message.Truncate(200);
                    db.SubmitChanges();
                }
            });

            return Json(new { id = id });
        }
        public ActionResult EditPerson(string data)
        {
            // Authenticate first
            if (!Auth())
            {
                return(Message.createErrorReturn("Authentication failed, please try again", Message.API_ERROR_INVALID_CREDENTIALS));
            }

            Message message = Message.createFromString(data);

            Models.CheckInAPIv2.Person person = JsonConvert.DeserializeObject <Models.CheckInAPIv2.Person>(message.data);    // new data
            person.clean();

            var NewContext = CurrentDatabase.Copy();

            CmsData.Person p = NewContext.LoadPersonById(person.id);   // existing data

            if (p == null)
            {
                return(Message.createErrorReturn("Person not found", Message.API_ERROR_PERSON_NOT_FOUND));
            }

            person.fillPerson(p);
            BasicPersonInfo m = new BasicPersonInfo()
            {
                Id = person.id
            };

            p.CopyProperties2(m);

            m.CellPhone = new CellPhoneInfo()
            {
                Number      = person.mobilePhone,
                ReceiveText = p.ReceiveSMS
            };
            m.Gender        = new Code.CodeInfo(p.GenderId, p.Gender.Description);
            m.MaritalStatus = new Code.CodeInfo(p.MaritalStatusId, p.MaritalStatus.Description);
            m.EmailAddress  = new EmailInfo()
            {
                Address = p.EmailAddress,
                Send    = p.SendEmailAddress1.GetValueOrDefault(true)
            };
            m.EmailAddress2 = new EmailInfo()
            {
                Address = p.EmailAddress2,
                Send    = p.SendEmailAddress2.GetValueOrDefault(true)
            };

            m.UpdatePerson(CurrentDatabase);
            DbUtil.LogPersonActivity($"Update Basic Info for: {m.person.Name}", m.Id, m.person.Name);

            CmsData.Family f = NewContext.Families.First(fam => fam.FamilyId == p.FamilyId);
            p.SetRecReg().MedicalDescription = person.allergies;
            p.SetRecReg().Emcontact          = person.emergencyName;
            p.SetRecReg().Emphone            = person.emergencyPhone.Truncate(50);

            var fsb = new List <ChangeDetail>();

            f.UpdateValue(fsb, "AddressLineOne", person.address);
            f.UpdateValue(fsb, "AddressLineTwo", person.address2);
            f.UpdateValue(fsb, "CityName", person.city);
            f.UpdateValue(fsb, "StateCode", person.state);
            f.UpdateValue(fsb, "ZipCode", person.zipCode);
            f.UpdateValue(fsb, "CountryName", person.country);
            f.LogChanges(NewContext, fsb, p.PeopleId, CurrentDatabase.UserPeopleId ?? 0);
            person.fillFamily(f);
            NewContext.SubmitChanges();

            AddEditPersonResults results = new AddEditPersonResults {
                familyID   = f.FamilyId,
                peopleID   = p.PeopleId,
                positionID = p.PositionInFamilyId,
                barcodeID  = CmsData.Person.Barcode(CurrentDatabase, p.PeopleId)
            };

            Message response = new Message();

            response.setNoError();
            response.count = 1;
            response.data  = SerializeJSON(results);

            return(response);
        }