Пример #1
0
        private static void VerifyWhiteDeliveryDiscrepancies(IDiscrepanciesService service, Mapping provider, int daysCreated)
        {
            var result = service.GetNotEngagedMailableItemsAsync(provider.ProviderId, provider.SubscriberId, daysCreated).Result;

            // Assert
            if (result.Any())
            {
                result.Take(MaxAccountsToVerify).ToList().ForEach(x =>
                {
                    if (x.Account.HasValue)
                    {
                        using (var ctx = new EngagerContext())
                        {
                            var id = x.Account.Value.AccountId;
                            var account = ctx.Accounts.FirstOrDefault(acc => acc.AccountId == id);

                            Assert.IsNotNull(account);
                            Assert.IsFalse(account.IsActive);
                        }

                        Assert.AreEqual(x.Description, SeedDiscrepancy.NotMailableSeedMessage);
                    }
                    else
                    {
                        using (var ctx = new EngagerContext())
                        {
                            var oldestDateCreated = DateTime.Today.AddDays(-daysCreated);
                            var account = ctx.Accounts.FirstOrDefault(acc => (acc.DateAccountCreated >= oldestDateCreated) && (acc.EmailAddress == x.EmailAddress));

                            Assert.IsNull(account);
                        }

                        Assert.AreEqual(x.Description, string.Format(SeedDiscrepancy.MissingSeedMessage, "Engager"));
                    }
                });
            }
        }
Пример #2
0
        private static Mock<IDiscrepanciesService> CreateFakeService()
        {
            var service = new Mock<IDiscrepanciesService>();
            
            service
                .Setup(x => x.GetMetadataForProviderAsync(It.IsAny<int>()))
                .Returns<int>(async subscriberId =>
                {
                    using (var ctx = new OenContext())
                    {
                        var linkData = await ctx.Lists.FirstOrDefaultAsync(x => x.enabled && (x.clientKey_id == subscriberId) && (x.emailCount > 0));

                        return new WD40.Domain.Engagement.Providers.Metadata
                        {
                            DatabaseName = linkData.dataBaseName,
                            TableName = linkData.tableName
                        };
                    }
                });

            service
                .Setup(x => x.GetNotMailableEngagedItemsAsync(It.IsAny<ProviderType>(), It.IsAny<int>(), It.IsAny<int>()))
                .Returns<ProviderType, int, int>(async (pId, sId, days) =>
                {
                    var result = (new SeedDiscrepancy[] { }).AsEnumerable();

                    using (var ctx = new EngagerContext())
                    {
                        var oldestDateCreated = DateTime.Today.AddDays(-days);
                        var accounts = await (from x in ctx.Accounts 
                                              where x.IsActive && (x.ProviderId == (int)pId) && (x.DateAccountCreated >= oldestDateCreated)
                                              select x).ToArrayAsync();

                        if (accounts.Any())
                        {
                            var metadata = await service.Object.GetMetadataForProviderAsync(sId);

                            using (var subscribersCtx = new SubscribersContext())
                            {
                                subscribersCtx.Database.Connection.ConnectionString += string.Format(";initial catalog={0};", metadata.DatabaseName);
                                var subscribers = subscribersCtx.FromStore(metadata.DatabaseName, metadata.TableName).GetSubscribers(subscribersCtx.AllSubscribersSpecification).Result;

                                if (subscribers.Any())
                                {
                                    result = from x in accounts
                                             join y in subscribers on x.EmailAddress equals y.email.Trim() into all
                                             from r in all.DefaultIfEmpty()
                                             where (r == null) || (r.send == 0)
                                             select SeedDiscrepancy.FromEngager(x, r);
                                }
                            }
                        }

                        return result.AsQueryable();
                    }
                });

            service
                .Setup(x => x.GetNotEngagedMailableItemsAsync(It.IsAny<ProviderType>(), It.IsAny<int>(), It.IsAny<int>()))
                .Returns<ProviderType, int, int>(async (pId, sId, days) =>
                {
                    var result = (new SeedDiscrepancy[] { }).AsEnumerable();
                    var metadata = await service.Object.GetMetadataForProviderAsync(sId);

                    using (var subscribersCtx = new SubscribersContext())
                    {
                        var subscribers = await subscribersCtx.FromStore(metadata.DatabaseName, metadata.TableName).GetSubscribers(subscribersCtx.MailableSubscribersSpecification);

                        if (subscribers.Any())
                        {
                            var oldestDateCreated = DateTime.Today.AddDays(-days);
                            using (var ctx = new EngagerContext())
                            {
                                var accounts = await (from x in ctx.Accounts
                                                      where (x.ProviderId == (int)pId) && (x.DateAccountCreated >= oldestDateCreated)
                                                      select x).ToArrayAsync();

                                result = from x in subscribers
                                         join y in accounts on x.email.Trim() equals y.EmailAddress into all
                                         from r in all.DefaultIfEmpty()
                                         where (r == null) || !r.IsActive
                                         select SeedDiscrepancy.FromWhiteDelivery(r, x);
                            }
                        }

                        return result.AsQueryable();
                    }
                });

            service
                .Setup(x => x.Resolve(It.IsAny<SeedDiscrepancy>()))
                .Returns<SeedDiscrepancy>(async sd =>
                {
                    if (!sd.IsValid)
                    {
                        return SeedDiscrepancyResolution.Failed(sd).WithMessage(SeedDiscrepancy.InvalidDiscrepancyMessage);
                    }
                    
                    var result = SeedDiscrepancyResolution.Failed(sd).WithMessage(SeedDiscrepancy.NotImplementedFeatureMessage);

                    if (sd.IsEngagerDiscrepancy)
                    {
                        using (var subscribersCtx = new SubscribersContext())
                        {
                            var provider = WD40.Domain.Engagement.Providers.Registry.Instance[sd.Account.Value.ProviderId];
                            var metadata = await service.Object.GetMetadataForProviderAsync(provider.SubscriberId);

                            if (!sd.Subscriber.HasValue)
                            {
                                try
                                {
                                    result.Succeeded = await subscribersCtx.FromStore(metadata.DatabaseName, metadata.TableName).AddSubscriber(sd);
                                    result.WithMessage(SeedDiscrepancy.ResolvedMessage);
                                }
                                catch (Exception e)
                                {
                                    result.WithMessage(e.Message);
                                }
                            }
                            else
                            {
                                try
                                {
                                    result.Succeeded = await subscribersCtx.FromStore(metadata.DatabaseName, metadata.TableName).EnableSubscriber(sd);
                                    result.WithMessage(SeedDiscrepancy.ResolvedMessage);
                                }
                                catch (Exception e)
                                {
                                    result.WithMessage(e.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!sd.Account.HasValue)
                        {
                            // TODO: Add subscriber to Engager
                            throw new NotImplementedException(result.Messages);
                        }
                        else
                        {
                            // TODO: Update account in Engager with active = true
                            throw new NotImplementedException(result.Messages);
                        }
                    }

                    return result;
                });

            return service;
        }