private void UpdateAllowedTags()
        {
            lbAllowedTags.DataSource   = null;
            lbAllowedTags.SelectedItem = null;

            NotificationTemplateType templateType = (NotificationTemplateType)cbTemplateType.SelectedItem;

            if (NotificationTypeTag.NotificationTags.ContainsKey(templateType))
            {
                var allowedTags     = NotificationTypeTag.NotificationTags[templateType];
                var allowedTagnames = allowedTags.Select(a => NotificationTypeTag.TagName(a)).OrderBy(a => a).ToArray();
                lbAllowedTags.DataSource   = allowedTagnames;
                lbAllowedTags.SelectedItem = null;
            }
        }
Пример #2
0
        private void SendNotificationSMS(Customer bdayCustomer)
        {
            if (!String.IsNullOrWhiteSpace(bdayCustomer.CellNumber))
            {
                string smsText = "Happy birthday " + bdayCustomer.CustomerFullName + ". We hope you have a wonderful day. Regards Astrodon";

                var smsTemplate = _Context.NotificationTemplateSet.FirstOrDefault(a => a.TemplateType == NotificationTemplateType.Birthday);
                if (smsTemplate != null)
                {
                    smsText = smsTemplate.MessageText;

                    var supportedTags = NotificationTypeTag.NotificationTags[NotificationTemplateType.Birthday];

                    foreach (var tag in supportedTags)
                    {
                        switch (tag)
                        {
                        case NotificationTagType.CustomerFullName:
                            smsText = smsText.Replace(NotificationTypeTag.TagName(tag), bdayCustomer.CustomerFullName);
                            break;

                        default:
                            break;
                        }
                    }
                }

                string number = bdayCustomer.CellNumber;
                var    sms    = new SMS();
                string status;
                string batchId;

                sms.SendSMS(number, smsText, out status, out batchId);

                bdayCustomer.LastBirthdayNotificationSent = DateTime.Now;
                bdayCustomer.BirthdaySMSText   = smsText;
                bdayCustomer.BirthDaySMSStatus = status;
                bdayCustomer.BirthDaySMSBatch  = batchId;

                _Context.SaveChanges();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txTemplateName.Text))
            {
                Controller.HandleError("Name is required", "Validation Error");
                return;
            }

            if (String.IsNullOrWhiteSpace(txTemplateText.Text))
            {
                Controller.HandleError("Text is required", "Validation Error");
                return;
            }

            //if not General then can only have one

            NotificationTemplateType templateType = (NotificationTemplateType)cbTemplateType.SelectedItem;

            if (templateType != NotificationTemplateType.General)
            {
                var existing = _Context.NotificationTemplateSet.Where(a => a.TemplateType == templateType).FirstOrDefault();
                if (existing != null)
                {
                    if (_Item == null || _Item.id != existing.id)
                    {
                        Controller.HandleError("A template is already defined for " + templateType.ToString() + " please edit the existing tempalte", "Validation Error");
                        return;
                    }
                }

                //validate that all tags are valid

                var tags        = NotificationTemplate.GetAllTags(txTemplateText.Text);
                var allowedTags = NotificationTypeTag.NotificationTags[templateType];

                var allowedTagnames = allowedTags.Select(a => NotificationTypeTag.TagName(a)).ToArray();

                var tagsNotAllowed = tags.Where(a => !allowedTagnames.Contains(a)).ToList();
                if (tagsNotAllowed.Count() > 0)
                {
                    string errText = string.Empty;
                    foreach (var s in tagsNotAllowed)
                    {
                        errText += s + Environment.NewLine;
                    }

                    Controller.HandleError("The following tags are invalid for this message type " + Environment.NewLine +
                                           errText, "Validation Error");
                    return;
                }
            }

            if (_Item == null)
            {
                _Item = new NotificationTemplate();
                _Context.NotificationTemplateSet.Add(_Item);
            }

            _Item.TemplateName = txTemplateName.Text;
            _Item.MessageText  = txTemplateText.Text;
            _Item.TemplateType = (NotificationTemplateType)cbTemplateType.SelectedItem;

            try
            {
                bool isNew = _Item.id == 0;
                _Context.SaveChanges();

                if (isNew)
                {
                    _Data.Insert(0, _Item);
                }
                BindDataGrid();
                GotoReadOnly();
            }
            catch (DbUpdateException)
            {
                Controller.HandleError("Possible duplicate record detected", "Database Error");
            }
            catch (Exception ex2)
            {
                Controller.HandleError(ex2.Message);
            }
        }