示例#1
0
        private async Task HandlePendingApprovalEmails(RMA rma, List <RMALineItem> rmaLineItems, HSOrderWorksheet worksheet)
        {
            IEnumerable <RMALineItem> pendingApprovalLineItemsWithNewComments = rmaLineItems
                                                                                .Where(li => li.Status == RMALineItemStatus.Processing)
                                                                                .Where(li => li.Comment != rma.LineItems.FirstOrDefault(item => item.ID == li.ID).Comment);

            if (!pendingApprovalLineItemsWithNewComments.Any())
            {
                return;
            }

            LineItemStatusChanges lineItemStatusChanges = new LineItemStatusChanges()
            {
                Status  = rma.Type == RMAType.Cancellation ? LineItemStatus.CancelRequested : LineItemStatus.ReturnRequested,
                Changes = new List <LineItemStatusChange>()
            };

            foreach (var rmaLineItem in pendingApprovalLineItemsWithNewComments)
            {
                lineItemStatusChanges.Changes.Add(new LineItemStatusChange()
                {
                    ID       = rmaLineItem.ID,
                    Quantity = rmaLineItem.QuantityProcessed,
                    Comment  = rmaLineItem.Comment,
                    QuantityRequestedForRefund = rmaLineItem.QuantityRequested
                });
            }

            List <HSLineItem> lineItemsChanged = worksheet.LineItems.Where(li => lineItemStatusChanges.Changes.Select(li => li.ID).Contains(li.ID)).ToList();

            Supplier supplier = await _oc.Suppliers.GetAsync(rma.SupplierID);

            EmailDisplayText emailText = new EmailDisplayText()
            {
                EmailSubject = $"New message available from {supplier.Name}",
                DynamicText  = $"{supplier.Name} has contacted you regarding your request for {rma.Type.ToString().ToLower()}",
                DynamicText2 = "The following items have new messages"
            };

            await _sendgridService.SendLineItemStatusChangeEmail(worksheet.Order, lineItemStatusChanges, lineItemsChanged, worksheet.Order.FromUser.FirstName, worksheet.Order.FromUser.LastName, worksheet.Order.FromUser.Email, emailText);
        }
示例#2
0
        private async Task HandleLineItemStatusChangeNotification(VerifiedUserType setterUserType, HSOrder buyerOrder, List <string> supplierIDsRelatedToChange, List <HSLineItem> lineItemsChanged, LineItemStatusChanges lineItemStatusChanges)
        {
            try
            {
                var suppliers = await Throttler.RunAsync(supplierIDsRelatedToChange, 100, 5, supplierID => _oc.Suppliers.GetAsync <HSSupplier>(supplierID));

                // currently the only place supplier name is used is when there should be lineitems from only one supplier included on the change, so we can just take the first supplier
                var statusChangeTextDictionary = LineItemStatusConstants.GetStatusChangeEmailText(suppliers.First().Name);

                foreach (KeyValuePair <VerifiedUserType, EmailDisplayText> entry in statusChangeTextDictionary[lineItemStatusChanges.Status])
                {
                    var userType  = entry.Key;
                    var emailText = entry.Value;

                    var firstName = "";
                    var lastName  = "";
                    var email     = "";

                    if (userType == VerifiedUserType.buyer)
                    {
                        firstName = buyerOrder.FromUser.FirstName;
                        lastName  = buyerOrder.FromUser.LastName;
                        email     = buyerOrder.FromUser.Email;
                        await _sendgridService.SendLineItemStatusChangeEmail(buyerOrder, lineItemStatusChanges, lineItemsChanged.ToList(), firstName, lastName, email, emailText);
                    }
                    else if (userType == VerifiedUserType.admin)
                    {
                        // Loop over seller users, pull out THEIR boolean, as well as the List<string> of AddtlRcpts
                        var sellerUsers = await _oc.AdminUsers.ListAsync <HSSellerUser>();

                        var tos = new List <EmailAddress>();
                        foreach (var seller in sellerUsers.Items)
                        {
                            if (seller?.xp?.OrderEmails ?? false)
                            {
                                tos.Add(new EmailAddress(seller.Email));
                            }
                            ;
                            if (seller?.xp?.AddtlRcpts?.Any() ?? false)
                            {
                                foreach (var rcpt in seller.xp.AddtlRcpts)
                                {
                                    tos.Add(new EmailAddress(rcpt));
                                }
                                ;
                            }
                            ;
                        }
                        ;
                        var shouldNotify = !(LineItemStatusConstants.LineItemStatusChangesDontNotifySetter.Contains(lineItemStatusChanges.Status) && setterUserType == VerifiedUserType.admin);
                        if (shouldNotify)
                        {
                            await _sendgridService.SendLineItemStatusChangeEmailMultipleRcpts(buyerOrder, lineItemStatusChanges, lineItemsChanged.ToList(), tos, emailText);
                        }
                    }
                    else
                    {
                        var shouldNotify = !(LineItemStatusConstants.LineItemStatusChangesDontNotifySetter.Contains(lineItemStatusChanges.Status) && setterUserType == VerifiedUserType.supplier);
                        if (shouldNotify)
                        {
                            await Throttler.RunAsync(suppliers, 100, 5, async supplier =>
                            {
                                if (supplier?.xp?.NotificationRcpts?.Any() ?? false)
                                {
                                    var tos = new List <EmailAddress>();
                                    foreach (var rcpt in supplier.xp.NotificationRcpts)
                                    {
                                        tos.Add(new EmailAddress(rcpt));
                                    }
                                    ;
                                    await _sendgridService.SendLineItemStatusChangeEmailMultipleRcpts(buyerOrder, lineItemStatusChanges, lineItemsChanged.ToList(), tos, emailText);
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // track in app insights
                // to find go to Transaction Search > Event Type = Event > Filter by any of these custom properties or event name "Email.LineItemEmailFailed"
                var customProperties = new Dictionary <string, string>
                {
                    { "Message", "Attempt to email line item changes failed" },
                    { "BuyerOrderID", buyerOrder.ID },
                    { "BuyerID", buyerOrder.FromCompanyID },
                    { "UserEmail", buyerOrder.FromUser.Email },
                    { "UserType", setterUserType.ToString() },
                    { "ErrorResponse", JsonConvert.SerializeObject(ex.Message, Newtonsoft.Json.Formatting.Indented) }
                };
                _telemetry.TrackEvent("Email.LineItemEmailFailed", customProperties);
                return;
            }
        }