コード例 #1
0
ファイル: WidgetController.cs プロジェクト: ASTD/NPS
        // GET: Widget
        public ActionResult Index(string engagementType, string masterCustomerId, string referenceId)
        {
            var shouldView = ShouldView(engagementType, referenceId, masterCustomerId);

            if (!shouldView)
            {
                return(new ContentResult()
                {
                    Content = "", ContentEncoding = Encoding.UTF8, ContentType = "text/html"
                });
            }

            // Create the NPS Survey Entry
            using (var ctx = new DataClassesDataContext())
            {
                // check if entry exists beforehand
                var exists = ctx.Engagement_NetPromoterScores.FirstOrDefault(r => r.MasterCustomerId == masterCustomerId && r.ReferenceId == referenceId && r.EngagementType == engagementType);

                if (exists != null)
                {
                    ViewBag.NpsEngagementId = exists.NPSEngagementId;
                    return(PartialView());
                }

                var nps        = new Engagement_NetPromoterScore();
                var customerId = ctx.Customers.FirstOrDefault(c => c.MasterCustomerId == masterCustomerId);
                nps.CreateDateTime   = DateTime.Now;
                nps.CustomerId       = customerId?.CustomerId;
                nps.CreatedBy        = "WEB";
                nps.EngagementType   = engagementType;
                nps.NPSSendDate      = DateTime.Today;
                nps.NPSScore         = null;
                nps.MasterCustomerId = masterCustomerId;
                nps.ReferenceId      = referenceId;
                ctx.Engagement_NetPromoterScores.InsertOnSubmit(nps);
                ctx.SubmitChanges();
                ViewBag.NpsEngagementId = nps.NPSEngagementId;
            }

            return(PartialView());
        }
コード例 #2
0
ファイル: SlackNotifier.cs プロジェクト: ASTD/NPS
        public void Notify(Engagement_NetPromoterScore item)
        {
            try
            {
                using (var ctx = new NpsCustomerRepositoryDataContext(ConfigurationManager.ConnectionStrings["NpsCustomerRepository"].ConnectionString))
                {
                    var customer = ctx.CUSTOMERs.FirstOrDefault(c => c.MASTER_CUSTOMER_ID == item.MasterCustomerId);
                    if (customer == null)
                    {
                        return;
                    }

                    // build message
                    var sb = new StringBuilder();

                    var message = new SlackMessage()
                    {
                        attachments = new List <Attachment>()
                    };
                    var attachment = new Attachment();
                    attachment.fallback    = sb.ToString();
                    attachment.color       = item.NPSScore > 8 ? "#6c9c2c" : (item.NPSScore > 6 ? "#ff9e01" : "#aa0027");
                    attachment.pretext     = "";
                    attachment.author_name = $"{customer.LABEL_NAME} ({customer.MASTER_CUSTOMER_ID})";
                    attachment.author_link = $"mailto:{customer.PRIMARY_EMAIL_ADDRESS}";
                    attachment.title       = Enumerations.GetEnumDescription(item.GetEngagementTypeByCode());
                    attachment.footer      = $"Reference ID: {item.ReferenceId} | {item.NPSResultDate?.ToString("MM/dd/yyyy hh:MM:ss tt")}";

                    attachment.fallback = $"{customer.LABEL_NAME} ({customer.MASTER_CUSTOMER_ID}), {customer.PRIMARY_EMAIL_ADDRESS}, scored the {Enumerations.GetEnumDescription(item.GetEngagementTypeByCode())} *{item.NPSScore}* out of 10.";
                    attachment.fields   = new List <Field>();

                    if (item.NPSScore.HasValue)
                    {
                        var scoreField = new Field();
                        scoreField.title = "Score";
                        scoreField.Short = true;
                        scoreField.value = $"{item.NPSScore} out of 10";
                        attachment.fields.Add(scoreField);
                    }

                    if (!string.IsNullOrEmpty(item.NPSComments))
                    {
                        var commentsField = new Field();
                        commentsField.title = "Comments";
                        commentsField.Short = true;
                        commentsField.value = $"{item.NPSComments}";
                        attachment.fields.Add(commentsField);
                    }

                    message.attachments.Add(attachment);
                    using (var client = new WebClient())
                    {
                        var json = JsonConvert.SerializeObject(message, Formatting.None, new JsonSerializerSettings()
                        {
                            ContractResolver = new CamelCasePropertyNamesContractResolver()
                        });
                        client.Headers[HttpRequestHeader.ContentType] = "application/json";
                        client.UploadString(new Uri("https://hooks.slack.com/services/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"), "POST", json);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, ex, "Error in Slack Notifier | " + ex.Message + "|" + ex.StackTrace);
            }
        }