Esempio n. 1
0
        public async Task <string> SendOrderSuccessSMSNotification(CustomerDetails customer, IConfiguration _configuration)
        {
            string status = string.Empty;

            try
            {
                ConfigDataAccess _configAccess = new ConfigDataAccess(_configuration);

                DatabaseResponse smsTemplateResponse = await _configAccess.GetSMSNotificationTemplate(NotificationEvent.OrderSuccess.ToString());

                var notificationMessage = MessageHelper.GetSMSMessage(NotificationEvent.OrderSuccess.ToString(), ((SMSTemplates)smsTemplateResponse.Results).TemplateName, customer.Name, customer.DeliveryEmail, customer.ShippingContactNumber, customer.OrderNumber, customer.SlotDate.ToString("dd MMM yyyy"), new DateTime(customer.SlotFromTime.Ticks).ToString("hh:mm tt") + " to " + new DateTime(customer.SlotToTime.Ticks).ToString("hh:mm tt"));

                DatabaseResponse notificationResponse = await _configAccess.GetConfiguration(ConfiType.Notification.ToString());

                MiscHelper parser = new MiscHelper();

                var notificationConfig = parser.GetNotificationConfig((List <Dictionary <string, string> >)notificationResponse.Results);

                Publisher orderSuccessSMSNotificationPublisher = new Publisher(_configuration, notificationConfig.SNSTopic);

                status = await orderSuccessSMSNotificationPublisher.PublishAsync(notificationMessage);

                LogInfo.Information("SMS send status : " + status + " " + JsonConvert.SerializeObject(notificationMessage));

                return(status);
            }
            catch (Exception ex)
            {
                LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "SMS failure for OrderNumber:" + customer.OrderNumber);

                throw ex;
            }
        }
Esempio n. 2
0
        public async Task <string> SendAdminEmailNotificationOnIDReUpload(string email, string orderNumber)
        {
            string status = string.Empty;

            try
            {
                ConfigDataAccess _configAccess = new ConfigDataAccess(_iconfiguration);

                DatabaseResponse templateResponse = await _configAccess.GetEmailNotificationTemplate(NotificationEvent.ICValidationCustomerUpload.ToString());

                DatabaseResponse notifyEmailResponse = ConfigHelper.GetValueByKey(ConfigKeys.IDReUploadNotifyEmail.ToString(), _iconfiguration);

                if (notifyEmailResponse.ResponseCode == (int)DbReturnValue.RecordExists && notifyEmailResponse.Results != null)
                {
                    var notificationMessage = MessageHelper.GetMessage((string)notifyEmailResponse.Results, "Admin", NotificationEvent.ICValidationCustomerUpload.ToString(), ((EmailTemplate)templateResponse.Results).TemplateName, _iconfiguration, email, orderNumber);

                    DatabaseResponse notificationResponse = await _configAccess.GetConfiguration(ConfiType.Notification.ToString());

                    MiscHelper parser = new MiscHelper();

                    var notificationConfig = parser.GetNotificationConfig((List <Dictionary <string, string> >)notificationResponse.Results);

                    LogInfo.Information("Email Message to send  " + JsonConvert.SerializeObject(notificationResponse));

                    Publisher orderSuccessNotificationPublisher = new Publisher(_iconfiguration, notificationConfig.SNSTopic);

                    try
                    {
                        status = await orderSuccessNotificationPublisher.PublishAsync(notificationMessage);
                    }
                    catch (Exception ex)
                    {
                        LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "publishing :" + status);
                        throw ex;
                    }

                    LogInfo.Information("Email send status : " + status + " " + JsonConvert.SerializeObject(notificationMessage));

                    try
                    {
                        DatabaseResponse notificationLogResponse = await _configAccess.CreateEMailNotificationLogForDevPurpose(

                            new NotificationLogForDevPurpose
                        {
                            EventType = NotificationEvent.OrderSuccess.ToString(),

                            Message = JsonConvert.SerializeObject(notificationMessage)
                        });
                    }
                    catch (Exception ex)
                    {
                        LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "Email send:" + email);
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "IDReUpload:" + email);
                throw ex;
            }

            return(status);
        }
Esempio n. 3
0
        public async Task <string> SendEmailNotification(int CustomerID, int OrderID, IConfiguration _configuration)
        {
            string status = string.Empty;

            try
            {
                ConfigDataAccess _configAccess = new ConfigDataAccess(_configuration);

                BuddyDataAccess _buddyAccess = new BuddyDataAccess();

                CommonDataAccess _commonAccess = new CommonDataAccess(_configuration);

                DatabaseResponse templateResponse = await _configAccess.GetEmailNotificationTemplate(NotificationEvent.OrderSuccess.ToString());

                LogInfo.Information("Email Customer : " + CustomerID);

                // Get Customer Data from CustomerID for email and Name
                CustomerDetails customer = await _commonAccess.GetCustomerDetailByOrder(CustomerID, OrderID);

                LogInfo.Information("Email Customer data : " + JsonConvert.SerializeObject(customer));

                if (customer != null && !string.IsNullOrEmpty(customer.DeliveryEmail))
                {
                    StringBuilder orderedNumbersSb = new StringBuilder();

                    StringBuilder deliveryAddressSb = new StringBuilder();

                    orderedNumbersSb.Append("<table width='100%'>");

                    int counter = 0;

                    foreach (OrderNumber number in customer.OrderedNumbers)
                    {
                        if (counter > 0)
                        {
                            orderedNumbersSb.Append("<tr><td width='100%' colspan='3'> </td></tr>");
                        }
                        orderedNumbersSb.Append("<tr><td width='25%'>MobileNumber :<td width='20%'>");
                        orderedNumbersSb.Append(number.MobileNumber);
                        orderedNumbersSb.Append("</td><td width ='55%'></td></tr>");
                        orderedNumbersSb.Append("<tr><td width='25%'>Plan :<td width='20%'>");
                        orderedNumbersSb.Append(number.PlanMarketingName);
                        orderedNumbersSb.Append("</td><td width ='55%'>");
                        orderedNumbersSb.Append(number.PricingDescription);
                        orderedNumbersSb.Append("</td></tr> ");
                        counter++;
                    }

                    orderedNumbersSb.Append("</table>");

                    if (!string.IsNullOrEmpty(customer.ShippingBuildingNumber))
                    {
                        deliveryAddressSb.Append(customer.ShippingBuildingNumber);
                    }

                    if (!string.IsNullOrEmpty(customer.ShippingStreetName))
                    {
                        if (deliveryAddressSb.ToString() != "")
                        {
                            deliveryAddressSb.Append(" ");
                        }

                        deliveryAddressSb.Append(customer.ShippingStreetName);
                    }

                    deliveryAddressSb.Append("<br />");

                    StringBuilder shippingAddr2 = new StringBuilder();

                    if (!string.IsNullOrEmpty(customer.ShippingFloor))
                    {
                        shippingAddr2.Append(customer.ShippingFloor);
                    }

                    if (!string.IsNullOrEmpty(customer.ShippingUnit))
                    {
                        if (shippingAddr2.ToString() != "")
                        {
                            shippingAddr2.Append(" ");
                        }
                        shippingAddr2.Append(customer.ShippingUnit);
                    }

                    if (!string.IsNullOrEmpty(customer.ShippingBuildingName))
                    {
                        if (shippingAddr2.ToString() != "")
                        {
                            shippingAddr2.Append(" ");
                        }

                        shippingAddr2.Append(customer.ShippingBuildingName);
                    }

                    deliveryAddressSb.Append(shippingAddr2.ToString());

                    deliveryAddressSb.Append("<br />");

                    if (!string.IsNullOrEmpty(customer.ShippingPostCode))
                    {
                        deliveryAddressSb.Append(customer.ShippingPostCode);
                    }

                    string deliveryDate = customer.SlotDate.ToString("dd MMM yyyy") + " " + new DateTime(customer.SlotFromTime.Ticks).ToString("hh:mm tt")
                                          + " to " + new DateTime(customer.SlotToTime.Ticks).ToString("hh:mm tt");

                    var notificationMessage = MessageHelper.GetMessage(customer.ToEmailList, customer.Name,

                                                                       NotificationEvent.OrderSuccess.ToString(),

                                                                       ((EmailTemplate)templateResponse.Results).TemplateName, _configuration, customer.DeliveryEmail,
                                                                       customer.OrderNumber, orderedNumbersSb.ToString(), deliveryAddressSb.ToString(),
                                                                       customer.AlternateRecipientName == null ? customer.Name : customer.AlternateRecipientName,
                                                                       customer.AlternateRecipientContact == null ? customer.ShippingContactNumber : customer.AlternateRecipientContact,
                                                                       string.IsNullOrEmpty(customer.AlternateRecipientEmail) ? customer.DeliveryEmail : customer.AlternateRecipientEmail,
                                                                       deliveryDate, customer.ReferralCode);

                    DatabaseResponse notificationResponse = await _configAccess.GetConfiguration(ConfiType.Notification.ToString());

                    MiscHelper parser = new MiscHelper();

                    var notificationConfig = parser.GetNotificationConfig((List <Dictionary <string, string> >)notificationResponse.Results);

                    LogInfo.Information("Email Message to send  " + JsonConvert.SerializeObject(notificationResponse));

                    Publisher orderSuccessNotificationPublisher = new Publisher(_configuration, notificationConfig.SNSTopic);

                    try
                    {
                        status = await orderSuccessNotificationPublisher.PublishAsync(notificationMessage);
                    }
                    catch (Exception ex)
                    {
                        LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "publishing :" + status);
                        throw ex;
                    }

                    LogInfo.Information("Email send status : " + status + " " + JsonConvert.SerializeObject(notificationMessage));

                    status = await SendOrderSuccessSMSNotification(customer, _configuration);

                    try
                    {
                        DatabaseResponse notificationLogResponse = await _configAccess.CreateEMailNotificationLogForDevPurpose(
                            new NotificationLogForDevPurpose
                        {
                            EventType = NotificationEvent.OrderSuccess.ToString(),
                            Message   = JsonConvert.SerializeObject(notificationMessage)
                        });
                    }
                    catch (Exception ex)
                    {
                        LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "Email send:" + OrderID);
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical) + "OrderID:" + OrderID);
                throw ex;
            }

            return(status);
        }
        public async Task <IActionResult> ProcessSnsNotifications([FromHeader(Name = "x-amz-sns-message-type")] string snsMessageType, [FromHeader(Name = "x-amz-sns-rawdelivery")] bool isRawDelivery, string id = "")
        {
            try
            {
                //LogInfo.Information("1 - ProcessSnsNotification started with {"+ snsMessageType + "} object");

                if (string.IsNullOrEmpty(snsMessageType))
                {
                    LogInfo.Information(SNSNotification.EmptySNSTypeHeader.ToString());

                    return(BadRequest());
                }

                //if (isRawDelivery)
                //{
                //    string msg = "message is raw"; // for testing pupose,
                //}

                SNSSubscription notification = new SNSSubscription();

                string requestBody = "";

                using (var reader = new StreamReader(Request.Body))
                {
                    requestBody = reader.ReadToEndAsync().Result;
                }
                //LogInfo.Information("2 - Now fetching Body with {" + requestBody + "} object");
                var requestMessage = Amazon.SimpleNotificationService.Util.Message.ParseMessage(requestBody);
                //LogInfo.Information("3 - Parsing messages with {" + requestMessage + "} object");
                if (!requestMessage.IsMessageSignatureValid())
                {
                    LogInfo.Information(SNSNotification.InvalidSignature.ToString());

                    return(BadRequest());
                }

                //temporary storage for testing
                string path = ConfigHelper.GetValueByKey("SNSNotificationPath", _iconfiguration).Results.ToString();
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                System.IO.File.WriteAllText(path + $@"/notifications_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.json", requestBody);

                // end temp

                if (string.IsNullOrEmpty(snsMessageType))
                {
                    //LogInfo.Information("4 - snsMessageType is  {" + snsMessageType + "} object");
                    return(BadRequest());
                }

                if (snsMessageType == Amazon.SimpleNotificationService.Util.Message.MESSAGE_TYPE_SUBSCRIPTION_CONFIRMATION && requestMessage.IsSubscriptionType)
                {
                    Subscriber subscriber = new Subscriber(_iconfiguration);

                    var client = subscriber.GetAmazonSimpleNotificationServiceClient();
                    //LogInfo.Information("5 - before snsMessageType is  {" + client + "} object");
                    Amazon.SimpleNotificationService.Model.ConfirmSubscriptionResponse confirmSubscriptionResponse = await client.ConfirmSubscriptionAsync(requestMessage.TopicArn, requestMessage.Token);

                    if (confirmSubscriptionResponse.HttpStatusCode == HttpStatusCode.OK)
                    {
                        LogInfo.Information(SNSNotification.SubscriptionConfirmed.ToString());
                    }
                    //LogInfo.Information("5.1 - before snsMessageType is  {" + client + "} object");
                    return(Ok());
                }

                else if (snsMessageType == Amazon.SimpleNotificationService.Util.Message.MESSAGE_TYPE_UNSUBSCRIPTION_CONFIRMATION && requestMessage.IsUnsubscriptionType)
                {
                    Subscriber subscriber = new Subscriber(_iconfiguration);

                    var client = subscriber.GetAmazonSimpleNotificationServiceClient();

                    Amazon.SimpleNotificationService.Model.UnsubscribeResponse confirmUnSubscriptionResponse = await client.UnsubscribeAsync(notification.TopicArn);

                    //LogInfo.Information("6 - before snsMessageType is  {" + snsMessageType + "} object");
                    return(Ok());
                }

                else if (snsMessageType == Amazon.SimpleNotificationService.Util.Message.MESSAGE_TYPE_NOTIFICATION && requestMessage.IsNotificationType)
                {
                    //LogInfo.Information("7 - before snsMessageType is  {" + snsMessageType + "} object");
                    ConfigDataAccess _configAccess = new ConfigDataAccess(_iconfiguration);

                    MiscHelper helper = new MiscHelper();

                    DatabaseResponse notificationConfigResponse = await _configAccess.GetConfiguration(ConfiType.Notification.ToString());

                    NotificationConfig snsConfig = helper.GetNotificationConfig((List <Dictionary <string, string> >)notificationConfigResponse.Results);

                    Subscriber subscriber = new Subscriber(_iconfiguration, snsConfig.SNSTopic, snsConfig.SQS);

                    var client = subscriber.GetAmazonSimpleNotificationServiceClient();

                    Action <object> notificationSqsProcessor = ProcessNotification;

                    await subscriber.ListenAsync(notificationSqsProcessor);

                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical));

                return(Ok());
            }
        }