예제 #1
0
        public override void ExecuteTask()
        {
            foreach (ServiceTaskProviderElement provider in ServiceTaskProviderConfiguration.ProviderConfiguration)
            {
                ecomDataProvider = new EcomDataProvider(provider.ConnectionString);

                if (!ecomDataProvider.EagleExistsAsExternalSupplier())
                {
                    Log.LogInfo(
                        "Eagle doesn't exist in Ecom as an external supply partner. None of the businesses will be synced");
                }
                else
                {
                    // Get the last updated timestamp from ECOM for Eagle businesses
                    DateTime mostRecentUpdateTime = ecomDataProvider.GetMostRecentBusinessUpdateTime();

                    Log.LogInfo(string.Format("Getting businesses updated from {0}",
                                              mostRecentUpdateTime.ToString(CultureInfo.InvariantCulture)));

                    // Get all businesses in Eagle modified since this date and are associated to countries for this provider (Eagle.Business.Updated >=) - if first run assume 2013-01-01
                    List<Model.Business.Business> businesses =
                        businessManager.GetLastModifiedBusinesses(mostRecentUpdateTime)
                                       .Where(c => provider.ContainsCountry(c.Country.IsoChar2Code))
                                       .ToList();

                    Log.LogInfo(string.Format("{0} {1} Businesses have been modified on Eagle", businesses.Count,
                                              provider.ProviderSet));
                    int numberOfBusinessesSynced = 0;

                    //  Insert/Update records in the ECOM database
                    if (Log.IsDebugEnabled)
                    {
                        Log.LogDebug(string.Format("Synchronizing {0} businesses to Ecom:", provider.ProviderSet));
                    }
                    Guid updatedByUserId;
                    if (!Guid.TryParse(identity.Name, out updatedByUserId))
                    {
                        Log.LogError("Couldn't get the user identity from the current thread");
                        Log.LogInfo("Synchronizing task finished executing");
                        return;
                    }

                    foreach (Model.Business.Business business in businesses)
                    {
                        if (State == TaskState.Stopping)
                        {
                            Log.LogInfo("Aborting SyncBusinessesToEcom task due to the service being stopped.");
                            return;
                        }

                        List<string> missedChannels;
                        var businessAccount = EcomConverter.EagleBusinessToEcom(business, provider.ChannelSyncCode,
                                                                                out missedChannels);
                        businessAccount.ModifiedByUserId = updatedByUserId;
                        businessAccount.ModifiedDate = DateTime.UtcNow;
                        businessAccount.BusinessAccountStatusTypeId = EcomConverter.ACTIVE_CODE;

                        bool synced = true;
                        EcomBusinessInfo ecomBusinessInfo = null;
                        try
                        {
                            ecomBusinessInfo = ecomDataProvider.SaveBusinessAccount(businessAccount);
                            Log.LogDebug(string.Format("businessId = {0} synced", business.Id));
                        }
                        catch (SqlException ex)
                        {
                            Log.LogError(
                                string.Format("businessId = {0} was not synced because of SQL error.", business.Id),
                                ex: ex);
                            synced = false;
                        }


                        if (Log.IsDebugEnabled && synced)
                        {
                            numberOfBusinessesSynced++;

                            if (ecomBusinessInfo != null)
                            {
                                Log.LogDebug(string.Format("businessId = {0} Channels synced    : {1}", business.Id,
                                                           ecomBusinessInfo.AddedChannels.ToArray().Serialize()));
                                Log.LogDebug(string.Format("businessId = {0} Channels missed    : {1}{2}", business.Id,
                                                           ecomBusinessInfo.MissingChannels.ToArray().Serialize(),
                                                           missedChannels.ToArray().Serialize()));
                            }

                            Log.LogDebug(string.Format("businessId = {0} synced", business.Id));
                        }
                    }

                    Log.LogInfo(string.Format("{0} {1} Businesses have been synced to Ecom", numberOfBusinessesSynced,
                                              provider.ProviderSet));
                }
            }
        }
예제 #2
0
            public void SaveBusinessAccountWhenNoExistingCreates()
            {
                using (new TestDataHelper(TestQueryEcom.POPULATE_BUSINESSES, TestQueryEcom.CLEANUP_TEST_DATA, DbHelper.ECOM_CONNECTION_STRING_NAME))
                {
                    // Arrange
                    const string NON_EXISTING_SHORT_NAME = "noexists";

                    var businessaccount = new EcomBusinessAccount
                    {
                        Id = Guid.NewGuid(),
                        ModifiedDate = DateTime.Now,
                        FullName = "FullName",
                        CreatedByTitle = "Mr",
                        CreatedByFirstname = "CreatedByFirstname",
                        CreatedByLastname = "CreatedByLastname",
                        ModifiedByTitle = "ModifiedByTitle",
                        ModifiedByFirstname = "ModifiedByFirstname",
                        ModifiedByLastname = "ModifiedByLastname",
                        BusinessAccountStatusTypeId = 1,
                        BusinessAccountTypeId = 1,
                        ReferenceCode = "123",
                        ShortName = NON_EXISTING_SHORT_NAME,
                        IsMerchant = true,
                        VatNumber = "123-859-7425",
                        MerchantId = "12345",
                        MerchantPassword = "******",
                        MerchantPasswordExpiry = DateTime.Today,
                        AllowAmex = false,
                        AllowPaypal = true,
                        AllowThreeDSecure = true,
                        ContentId = NON_EXISTING_SHORT_NAME,
                        AddressLine1 = "Test Street 1",
                        City = "London",
                        PostCode = "N1 HT9",
                        Channels = new Collection<EcomChannel>
                                       {
                                           new EcomChannel
                                               {
                                                   ChannelName = "channel1"
                                               },
                                           new EcomChannel
                                               {
                                                   ChannelName = "channel2"
                                               },
                                           new EcomChannel
                                               {
                                                   ChannelName = "channel3"
                                               }
                                       }
                    };

                    var ecomDataProvider = new EcomDataProvider();
                    // Act
                    bool isBusinessInEcomBefore = ecomDataProvider.IsBusinessPresent(businessaccount.ReferenceCode);
                    Assert.IsFalse(isBusinessInEcomBefore, "business is already in database before save");

                    ecomDataProvider.SaveBusinessAccount(businessaccount);

                    // Assert
                    bool isBusinessInEcomAfter = ecomDataProvider.IsBusinessPresent(businessaccount.ShortName);

                    Assert.IsTrue(isBusinessInEcomAfter, "business hasn't been created");
                }
            }