/// <summary>
        /// Stores an instance of <see cref="CustomerPrincipal"/> in the private bot data associated with the user.
        /// </summary>
        /// <param name="context">The context for the bot.</param>
        /// <param name="principal">An instance of <see cref="CustomerPrincipal"/> associated with the authenticated user.</param>
        public static void StoreCustomerPrincipal(this IBotContext context, CustomerPrincipal principal)
        {
            context.AssertNotNull(nameof(context));
            principal.AssertNotNull(nameof(principal));

            context.PrivateConversationData.SetValue(BotConstants.CustomerPrincipalKey, principal);
        }
Example #2
0
        /// <summary>
        /// Ensures the given <see cref="CustomerPrincipal"/> has a valid customer context.
        /// </summary>
        /// <param name="principalToValidate">An instance of <see cref="CustomerPrincipal"/> to validate.</param>
        /// <param name="message">The message to report in the exception.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="principalToValidate"/> is null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="principalToValidate"/> does not contain a valid customer identifier.
        /// </exception>
        public static void AssertValidCustomerContext(this CustomerPrincipal principalToValidate, string message)
        {
            principalToValidate.AssertNotNull(nameof(principalToValidate));

            if (string.IsNullOrEmpty(principalToValidate.Operation.CustomerId))
            {
                throw new InvalidOperationException(message);
            }
        }
        /// <summary>
        /// Gets the specified subscription.
        /// </summary>
        /// <param name="principal">Security principal for the calling user.</param>
        /// <returns>An instance of <see cref="Subscription"/> that represents the specified subscription.</returns>
        /// <exception cref="ArgumentException">
        /// The operation context from <paramref name="principal"/> does not contain a valid customer identifier.
        /// or
        /// The operation context from <paramref name="principal"/> does not contain a valid subscription identifier.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="principal"/> is null.
        /// </exception>
        public async Task <Subscription> GetSubscriptionAsync(CustomerPrincipal principal)
        {
            DateTime startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            Guid         correlationId;
            IPartner     operations;
            Subscription subscription = null;

            principal.AssertNotNull(nameof(principal));
            principal.Operation.CustomerId.AssertNotEmpty(nameof(principal.Operation.CustomerId));
            principal.Operation.SubscriptionId.AssertNotEmpty(nameof(principal.Operation.SubscriptionId));

            try
            {
                startTime     = DateTime.Now;
                correlationId = Guid.NewGuid();
                operations    = await GetAppOperationsAsync(correlationId).ConfigureAwait(false);

                try
                {
                    subscription = await operations.Customers.ById(principal.Operation.CustomerId)
                                   .Subscriptions.ById(principal.Operation.SubscriptionId).GetAsync().ConfigureAwait(false);
                }
                catch (PartnerException ex)
                {
                    if (ex.ErrorCategory != PartnerErrorCategory.NotFound)
                    {
                        throw;
                    }
                }

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds }
                };

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "CustomerId", principal.CustomerId },
                    { "ObjectId", principal.ObjectId },
                    { "ParternCenterCorrelationId", correlationId.ToString() }
                };

                provider.Telemetry.TrackEvent("GetSubscriptionAsync", eventProperties, eventMetrics);

                return(subscription);
            }
            finally
            {
                eventMetrics    = null;
                eventProperties = null;
                operations      = null;
            }
        }
        /// <summary>
        /// Gets the available subscriptions.
        /// </summary>
        /// <param name="principal">Security principal for the calling user.</param>
        /// <returns>A list of available subscriptions.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="principal"/> is null.
        /// </exception>
        public async Task <List <Subscription> > GetSubscriptionsAsync(CustomerPrincipal principal)
        {
            DateTime startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            Guid     correlationId;
            IPartner operations;
            ResourceCollection <Subscription> subscriptions;

            principal.AssertNotNull(nameof(principal));

            try
            {
                startTime     = DateTime.Now;
                correlationId = Guid.NewGuid();
                operations    = await GetAppOperationsAsync(correlationId).ConfigureAwait(false);

                if (principal.CustomerId.Equals(provider.Configuration.PartnerCenterAccountId))
                {
                    principal.AssertValidCustomerContext(Resources.InvalidCustomerContextException);
                    subscriptions = await operations.Customers
                                    .ById(principal.Operation.CustomerId).Subscriptions.GetAsync().ConfigureAwait(false);
                }
                else
                {
                    subscriptions = await operations.Customers.ById(principal.CustomerId).Subscriptions.GetAsync().ConfigureAwait(false);
                }

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds },
                    { "NumberOfSubscriptions", subscriptions.TotalCount }
                };

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "CustomerId", principal.CustomerId },
                    { "Name", principal.Name },
                    { "ParternCenterCorrelationId", correlationId.ToString() }
                };

                provider.Telemetry.TrackEvent("GetCustomersAsync", eventProperties, eventMetrics);

                return(new List <Subscription>(subscriptions.Items));
            }
            finally
            {
                eventMetrics    = null;
                eventProperties = null;
                operations      = null;
                principal       = null;
                subscriptions   = null;
            }
        }
        /// <summary>
        /// Gets the specified customer.
        /// </summary>
        /// <param name="principal">Security principal for the calling user.</param>
        /// <param name="customerId">Identifier for the customer.</param>
        /// <returns>An instance of <see cref="Customer"/> that represents the specified customer.</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="customerId"/> is empty or null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="principal"/> is null.
        /// </exception>
        public async Task <Customer> GetCustomerAsync(CustomerPrincipal principal, string customerId)
        {
            Customer customer;
            DateTime startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            Guid     correlationId;
            IPartner operations;

            customerId.AssertNotEmpty(nameof(customerId));
            principal.AssertNotNull(nameof(principal));

            try
            {
                startTime     = DateTime.Now;
                correlationId = Guid.NewGuid();
                operations    = await GetAppOperationsAsync(correlationId).ConfigureAwait(false);

                if (principal.CustomerId.Equals(provider.Configuration.PartnerCenterAccountId))
                {
                    customer = await operations.Customers.ById(customerId).GetAsync().ConfigureAwait(false);
                }
                else
                {
                    customer = await operations.Customers.ById(principal.CustomerId).GetAsync().ConfigureAwait(false);
                }

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds }
                };

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "CustomerId", principal.CustomerId },
                    { "Name", principal.Name },
                    { "ParternCenterCorrelationId", correlationId.ToString() }
                };

                provider.Telemetry.TrackEvent(nameof(GetCustomerAsync), eventProperties, eventMetrics);

                return(customer);
            }
            finally
            {
                eventMetrics    = null;
                eventProperties = null;
                operations      = null;
                principal       = null;
            }
        }
        /// <summary>
        /// Gets the available customers.
        /// </summary>
        /// <param name="principal">Security principal for the calling user.</param>
        /// <returns>A list of available customers.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="principal"/> is null.
        /// </exception>
        public async Task <List <Customer> > GetCustomersAsync(CustomerPrincipal principal)
        {
            Customer customer;
            DateTime startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            Guid     correlationId;
            IPartner operations;
            IResourceCollectionEnumerator <SeekBasedResourceCollection <Customer> > customersEnumerator;
            List <Customer> customers;
            SeekBasedResourceCollection <Customer> seekCustomers;

            principal.AssertNotNull(nameof(principal));

            try
            {
                startTime     = DateTime.Now;
                correlationId = Guid.NewGuid();
                operations    = await GetAppOperationsAsync(correlationId).ConfigureAwait(false);

                customers = new List <Customer>();

                if (principal.CustomerId.Equals(provider.Configuration.PartnerCenterAccountId))
                {
                    seekCustomers = await operations.Customers.GetAsync().ConfigureAwait(false);

                    customersEnumerator = operations.Enumerators.Customers.Create(seekCustomers);

                    while (customersEnumerator.HasValue)
                    {
                        customers.AddRange(customersEnumerator.Current.Items);
                        await customersEnumerator.NextAsync().ConfigureAwait(false);
                    }
                }
                else
                {
                    customer = await operations.Customers.ById(principal.CustomerId).GetAsync().ConfigureAwait(false);

                    customers.Add(customer);
                }

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds },
                    { "NumberOfCustomers", customers.Count }
                };

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "CustomerId", principal.CustomerId },
                    { "Name", principal.Name },
                    { "ParternCenterCorrelationId", correlationId.ToString() }
                };

                provider.Telemetry.TrackEvent(nameof(GetCustomersAsync), eventProperties, eventMetrics);

                return(customers);
            }
            finally
            {
                customersEnumerator = null;
                eventMetrics        = null;
                eventProperties     = null;
                operations          = null;
                principal           = null;
                seekCustomers       = null;
            }
        }