// Owner Identities
        public static ReplicatedSiteIdentity GetIdentity(string webAlias)
        {
            webAlias = webAlias.ToUpper();
            var cacheKey = "{0}-OwnerIdentity-{1}".FormatWith(GlobalSettings.Exigo.Api.CompanyKey, webAlias);

            var identity = HttpContext.Current.Cache[cacheKey] as ReplicatedSiteIdentity;

            if (identity == null)
            {
                try
                {
                    // Get the customer site data
                    var customerSite = Exigo.WebService().GetCustomerSite(new GetCustomerSiteRequest()
                    {
                        WebAlias = webAlias
                    });

                    var customerID = customerSite.CustomerID;

                    // bind the data to the identity model
                    identity = new ReplicatedSiteIdentity();
                    identity.CustomerID = customerID;
                    identity.WebAlias = customerSite.WebAlias;
                    identity.FirstName = customerSite.FirstName;
                    identity.LastName = customerSite.LastName;
                    identity.Company = customerSite.Company;
                    identity.Email = customerSite.Email;
                    identity.Phone = customerSite.Phone;
                    identity.Phone2 = customerSite.Phone2;
                    identity.Fax = customerSite.Fax;

                    identity.Notes1 = customerSite.Notes1;
                    identity.Notes2 = customerSite.Notes2;
                    identity.Notes3 = customerSite.Notes3;
                    identity.Notes4 = customerSite.Notes4;

                    // Get the remaining customer data
                    var customer = Exigo.OData().Customers
                        .Where(c => c.CustomerID == customerID)
                        .Select(c => new
                        {
                            CreatedDate = c.CreatedDate,
                            HighestAchievedRankID = c.RankID,
                            CustomerTypeID = c.CustomerTypeID,
                            CustomerStatusID = c.CustomerStatusID,
                            DefaultWarehouseID = c.DefaultWarehouseID,
                            EnrollerID = c.EnrollerID,
                            Address = c.MainAddress1,
                            Address2 = c.MainAddress2,
                            City = c.MainCity,
                            State = c.MainState,
                            Zip = c.MainZip,
                            Country = c.MainCountry
                        }).FirstOrDefault();

                    // Address
                    identity.Address1 = customer.Address;
                    identity.Address2 = customer.Address2;
                    identity.City = customer.City;
                    identity.State = customer.State;
                    identity.Zip = customer.Zip;
                    identity.Country = customer.Country;

                    // Bind the additional data
                    identity.CreatedDate = customer.CreatedDate;
                    identity.HighestAchievedRankID = customer.HighestAchievedRankID;
                    identity.CustomerTypeID = customer.CustomerTypeID;
                    identity.CustomerStatusID = customer.CustomerStatusID;
                    identity.WarehouseID = (customer.DefaultWarehouseID != 0) ? customer.DefaultWarehouseID : Warehouses.Default;
                    identity.EnrollerID = Convert.ToInt32(customer.EnrollerID);

                    // Save the identity
                    HttpContext.Current.Cache.Insert(cacheKey,
                        identity,
                        null,
                        DateTime.Now.AddMinutes(Settings.IdentityRefreshInterval),
                        System.Web.Caching.Cache.NoSlidingExpiration,
                        System.Web.Caching.CacheItemPriority.Normal,
                        null);
                }
                catch
                {
                    return null;
                }
            }

            return identity;
        }
Esempio n. 2
0
        public ReplicatedSiteIdentity GetSiteOwnerIdentity(string webAlias)
        {
            var replicatedsiteidentity = new ReplicatedSiteIdentity();

            using (var context = Exigo.Sql())
            {
                var customerID = context.Query <int>(@"
                    SELECT cs.CustomerID FROM CustomerSites cs WHERE cs.WebAlias = @webalias
                ", new
                {
                    webalias = webAlias
                }).FirstOrDefault();

                // Logic to handle default web alias missing from database, which would cause an infinite redirect loop
                if (customerID == 0 && webAlias.Equals(GlobalSettings.ReplicatedSites.DefaultWebAlias, System.StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new System.Exception("Default user missing. Web Alias: {0}".FormatWith(GlobalSettings.ReplicatedSites.DefaultWebAlias));
                }

                if (customerID > 0)
                {
                    replicatedsiteidentity = context.Query <ReplicatedSiteIdentity>(@"
                    ; WITH cte_SocialNetworks AS (
	                    SELECT 
		                    CustomerID
		                    , [1] as FacebookURL
		                    , [2] as GooglePlusURL
		                    , [3] AS TwitterURL
		                    , [4] AS BlogURL
		                    , [5] AS LinkedInURL
		                    , [6] AS MySpaceURL
		                    , [7] AS YouTubeURL
		                    , [8] AS PinterestURL
		                    , [9] AS InstagramURL
	                    FROM 
	                    (SELECT 
			                    SocialNetworkID
			                , Customerid
			                , Url 
	                     FROM CustomerSocialNetworks
	                     WHERE CustomerID = @customerid) AS SourceTable
	                    PIVOT
	                    (
	                    MAX(url)
	                    for SocialNetworkID in ([1],[2],[3],[4],[5],[6],[7],[8],[9])
	                    ) as PivotTable
                    )
                    SELECT
                        c.CustomerID
                        , c.CustomerTypeID
                        , c.CustomerStatusID
	                    , c.CreatedDate
                        , ISNULL(c.RankID, 0) AS 'HighestAchievedRankID'    
	                    , ISNULL(c.DefaultWarehouseID, @defaultwarehouseid) AS 'WarehouseID'    
                        , cs.WebAlias
                        , FirstName = CASE WHEN cs.FirstName = '' THEN c.FirstName ELSE cs.FirstName END
                        , LastName = CASE WHEN cs.LastName = '' THEN c.LastName ELSE cs.LastName END
                        , cs.Company
                        , cs.Email
                        , cs.Phone
                        , cs.Phone2
                        , cs.Fax
                        , cs.Address1
                        , cs.Address2
                        , cs.City
                        , cs.State
                        , cs.Zip
                        , cs.Country
                        , cs.Notes1
                        , cs.Notes2
                        , cs.Notes3
                        , cs.Notes4
                        , ISNULL(s.FacebookUrl, '')	  AS 'FacebookUrl'
	                    , ISNULL(s.GooglePlusUrl, '') AS 'GooglePlusUrl'
	                    , ISNULL(s.TwitterUrl, '')	  AS 'TwitterUrl'
	                    , ISNULL(s.BlogUrl, '')		  AS 'BlogUrl'
	                    , ISNULL(s.LinkedInUrl, '')	  AS 'LinkedInUrl' 
	                    , ISNULL(s.MyspaceUrl, '')	  AS 'MyspaceUrl'
	                    , ISNULL(s.YouTubeUrl, '')	  AS 'YouTubeUrl'
	                    , ISNULL(s.PinterestUrl, '')  AS 'PinterestUrl'
	                    , ISNULL(s.InstagramUrl, '')  AS 'InstagramUrl'

                    FROM CustomerSites cs
                        LEFT JOIN Customers c
                            ON c.CustomerID = cs.CustomerID 
	                    LEFT JOIN cte_SocialNetworks s
		                    ON c.CustomerID = s.CustomerID 
								      
                    WHERE cs.CustomerID = @customerid
                ", new
                    {
                        defaultwarehouseid = Warehouses.Default,
                        customerid         = customerID
                    }).FirstOrDefault();
                }
            }

            // Webservice fail safe that will handle any recent enrollees if the sql hasn't caught up yet
            if (replicatedsiteidentity.CustomerID > 0)
            {
                return(replicatedsiteidentity);
            }
            else
            {
                var context      = Exigo.WebService();
                var customerSite = new GetCustomerSiteResponse();

                // Check to see if customer site for the provided webalias exists. Failure is thrown if not sso do this in a try/catch
                try
                {
                    customerSite = context.GetCustomerSite(new GetCustomerSiteRequest()
                    {
                        WebAlias = webAlias
                    });
                }
                catch (System.Exception)
                {
                    return(null);
                }

                var customer = new Customer();

                if (customerSite.CustomerID > 0)
                {
                    customer = (Customer)context.GetCustomers(new GetCustomersRequest()
                    {
                        CustomerID = customerSite.CustomerID
                    }).Customers.FirstOrDefault();

                    var socialNetworksResponse = context.GetCustomerSocialNetworks(new GetCustomerSocialNetworksRequest()
                    {
                        CustomerID = customerSite.CustomerID
                    });

                    if (socialNetworksResponse.CustomerSocialNetwork.Count() > 0)
                    {
                        foreach (var network in socialNetworksResponse.CustomerSocialNetwork)
                        {
                            switch (network.SocialNetworkID)
                            {
                            case SocialNetworkTypes.Facebook:
                                replicatedsiteidentity.FacebookUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.GooglePlus:
                                replicatedsiteidentity.GooglePlusUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.Twitter:
                                replicatedsiteidentity.TwitterUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.Blog:
                                replicatedsiteidentity.BlogUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.LinkedIn:
                                replicatedsiteidentity.LinkedInUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.MySpace:
                                replicatedsiteidentity.MySpaceUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.YouTube:
                                replicatedsiteidentity.YouTubeUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.Pinterest:
                                replicatedsiteidentity.PinterestUrl = network.Url;
                                continue;

                            case SocialNetworkTypes.Instagram:
                                replicatedsiteidentity.InstagramUrl = network.Url;
                                continue;

                            default:
                                continue;
                            }
                        }
                    }

                    // customer
                    replicatedsiteidentity.CustomerID            = customer.CustomerID;
                    replicatedsiteidentity.CustomerTypeID        = customer.CustomerTypeID;
                    replicatedsiteidentity.CustomerStatusID      = customer.CustomerStatusID;
                    replicatedsiteidentity.HighestAchievedRankID = customer.RankID;
                    replicatedsiteidentity.CreatedDate           = customer.CreatedDate;
                    replicatedsiteidentity.WarehouseID           = (customer.DefaultWarehouseID != 0) ? customer.DefaultWarehouseID : Warehouses.Default;

                    // customer site
                    replicatedsiteidentity.WebAlias  = customerSite.WebAlias;
                    replicatedsiteidentity.FirstName = customerSite.FirstName;
                    replicatedsiteidentity.LastName  = customerSite.LastName;
                    replicatedsiteidentity.Company   = customerSite.Company;
                    replicatedsiteidentity.Email     = customerSite.Email;
                    replicatedsiteidentity.Phone     = customerSite.Phone;
                    replicatedsiteidentity.Phone2    = customerSite.Phone2;
                    replicatedsiteidentity.Fax       = customerSite.Fax;
                    replicatedsiteidentity.Address1  = customerSite.Address1;
                    replicatedsiteidentity.Address2  = customerSite.Address2;
                    replicatedsiteidentity.City      = customerSite.City;
                    replicatedsiteidentity.State     = customerSite.State;
                    replicatedsiteidentity.Zip       = customerSite.Zip;
                    replicatedsiteidentity.Country   = customerSite.Country;
                    replicatedsiteidentity.Notes1    = customerSite.Notes1;
                    replicatedsiteidentity.Notes2    = customerSite.Notes2;
                    replicatedsiteidentity.Notes3    = customerSite.Notes3;
                    replicatedsiteidentity.Notes4    = customerSite.Notes4;

                    return(replicatedsiteidentity);
                }

                return(null);
            }
        }