Esempio n. 1
0
        public void TestSendEmails()
        {
            var settings = SettingsBuilder.GetSmtpSettingsFromAppSettings();
            var email    = new EmailService(_log, settings, _addressService);

            var actionServiceStub = new Mock <ISystemActionService>();

            actionServiceStub
            .Setup(l => l.GetUnprocessedByType(It.IsAny <IUnitOfWork>(), It.IsAny <SystemActionType>(), null, null))
            .Returns(new List <SystemActionDTO>()
            {
                new SystemActionDTO()
                {
                    InputData = SystemActionHelper.ToStr(new SendEmailInput()
                    {
                        OrderId   = "106-0610855-5158626",
                        EmailType = EmailTypes.NoticeLeft,
                        Args      = null
                    })
                }
            });
            actionServiceStub
            .Setup(l => l.SetResult(It.IsAny <IUnitOfWork>(),
                                    It.IsAny <long>(),
                                    It.IsAny <SystemActionStatus>(),
                                    It.IsAny <ISystemActionOutput>(),
                                    It.IsAny <string>())).Callback((IUnitOfWork db,
                                                                    long actionId,
                                                                    SystemActionStatus status,
                                                                    ISystemActionOutput output,
                                                                    string groupId) =>
            {
                Console.WriteLine(SystemActionHelper.ToStr(output));
            });

            email.ProcessEmailActions(_dbFactory,
                                      _time,
                                      _company,
                                      actionServiceStub.Object);
        }
        protected void DefaultActionUpdateEntitiesAfterResponse(long feedId,
                                                                IList <FeedResultMessage> errorList,
                                                                string itemAdditionalFieldName,
                                                                int maxAttemptCount)
        {
            using (var db = DbFactory.GetRWDb())
            {
                var feedItems       = db.FeedItems.GetAllAsDto().Where(fi => fi.FeedId == feedId).ToList();
                var systemActionIds = feedItems.Select(f => f.ItemId).ToList();
                var systemActions   = db.SystemActions.GetAll().Where(i => systemActionIds.Contains(i.Id)).ToList();
                var itemIds         = systemActions.Select(a => StringHelper.TryGetInt(a.Tag))
                                      .Where(a => a.HasValue)
                                      .Select(a => a.Value)
                                      .ToList();

                //Remove all exist errors
                var dbExistErrors = db.ItemAdditions.GetAll().Where(i => itemIds.Contains(i.ItemId) &&
                                                                    i.Field == itemAdditionalFieldName).ToList();
                foreach (var dbExistError in dbExistErrors)
                {
                    db.ItemAdditions.Remove(dbExistError);
                }

                foreach (var action in systemActions)
                {
                    var feedItem = feedItems.FirstOrDefault(fi => fi.ItemId == action.Id);
                    if (feedItem != null)
                    {
                        var itemErrors            = errorList.Where(e => e.MessageId == feedItem.MessageId).ToList();
                        TextMessageOutput  output = null;
                        SystemActionStatus status = SystemActionStatus.None;
                        if (itemErrors.Any())
                        {
                            action.AttemptNumber++;
                            action.AttemptDate = Time.GetAppNowTime();
                            if (action.AttemptNumber > maxAttemptCount)
                            {
                                action.Status = (int)SystemActionStatus.Fail;
                            }

                            var itemId = StringHelper.TryGetLong(action.Tag);

                            if (itemId.HasValue)
                            {
                                foreach (var itemError in itemErrors)
                                {
                                    db.ItemAdditions.Add(new Core.Entities.Listings.ItemAddition()
                                    {
                                        ItemId     = (int)itemId.Value,
                                        Field      = itemAdditionalFieldName,
                                        Value      = itemError.Message,
                                        CreateDate = Time.GetAppNowTime(),
                                    });
                                }
                            }

                            output = new TextMessageOutput()
                            {
                                IsSuccess = false,
                                Messages  = itemErrors.Select(i => new MessageString()
                                {
                                    Status  = MessageStatus.Error,
                                    Message = i.Message
                                }).ToList()
                            };
                            status = SystemActionStatus.Fail;
                        }
                        else
                        {
                            output = new TextMessageOutput()
                            {
                                IsSuccess = false
                            };
                            status = SystemActionStatus.Done;
                        }
                        action.OutputData = SystemActionHelper.ToStr(output);
                        action.Status     = (int)status;

                        Log.Info("Update action status, actionId=" + action.Id + ", status=" + action.Status);
                    }
                }

                db.Commit();
            }
        }