public async Task <SolutionManagerOperationResult> UpdateSubscriptionWithAdminNameAsync(string administratorName,
                                                                                                Guid newSkuId)
        {
            var customer = await this.GetCustomerAsync(administratorName);

            var subscription =
                await this.context.CustomerSubscriptions.FirstOrDefaultAsync(s => s.CustomerId == customer.Id);

            if (subscription == default)
            {
                return(SolutionManagerOperationResult.Failed(new SolutionManagementError
                {
                    Description = $"No subscription is found for customer {customer.CustomerName}."
                }));
            }

            var sku = await this.context.Skus.FirstOrDefaultAsync(s => s.Id == newSkuId);

            var result = await this.CheckAndRunAzureSubscriptionOperation(
                sku.Id,
                async() => await this.fulfillmentManager.RequestUpdateSubscriptionAsync(subscription.SubscriptionId,
                                                                                        sku.Name));

            if (!result.Succeeded)
            {
                return(result);
            }

            subscription.Sku               = sku;
            subscription.SkuId             = newSkuId;
            subscription.LastOperationTime = DateTimeOffset.UtcNow;
            await this.context.SaveChangesAsync();

            return(result);
        }
        private async Task <SolutionManagerOperationResult> CheckAndRunAzureSubscriptionOperation(Guid skuId,
                                                                                                  Func <Task <FulfillmentManagerOperationResult> > operation)
        {
            var sku = await this.context.Skus.FirstOrDefaultAsync(s => s.Id == skuId);

            if (sku.SalesChannel != SalesChannelEnum.Azure)
            {
                return(SolutionManagerOperationResult.Failed(new SolutionManagementError
                {
                    Description = $"Sku {sku.Id} is not an Azure SKU"
                }));
            }

            var result = await operation();

            if (!result.Succeeded)
            {
                return(SolutionManagerOperationResult.Failed(
                           new SolutionManagementError {
                    Description = result.ToString()
                }));
            }

            return(SolutionManagerOperationResult.Success);
        }
        public async Task <SolutionManagerOperationResult> ProvisionSubscriptionWithAdminAsync(string userName,
                                                                                               CustomerRegionEnum?customerRegion,
                                                                                               string fullName,
                                                                                               Guid planId,
                                                                                               Guid subscrptionId)
        {
            var customer = new Customer
            {
                Id           = Guid.NewGuid(),
                Users        = new List <CustomerUser>(),
                CustomerName = userName.GetDomainNameFromEmail()
            };

            var customerUser = new CustomerUser
            {
                Id             = Guid.NewGuid(),
                UserName       = userName,
                CustomerName   = userName.GetDomainNameFromEmail(),
                CustomerRegion = customerRegion,
                FullName       = fullName
            };

            customer.Users.Add(customerUser);

            // We are Contoso's commerce engine part, we simply create a new subscription.
            // We distinguish the Contoso or Azure Marketplace subscription with the SKU
            var subscription = new Subscription
            {
                CustomerId        = customer.Id,
                Customer          = customer,
                SkuId             = planId,
                State             = SubscriptionState.Complete,
                SubscriptionId    = subscrptionId,
                SubscriptionName  = userName.GetDomainNameFromEmail(),
                LastOperationTime = DateTimeOffset.UtcNow,
                CreatedTime       = DateTimeOffset.UtcNow
            };

            var existingSubscription =
                await this.context.CustomerSubscriptions.FirstOrDefaultAsync(s =>
                                                                             s.CustomerId == subscription.CustomerId);

            if (existingSubscription != default)
            {
                // Sample limitation, a customer can only have one subscription
                return(SolutionManagerOperationResult.Failed(
                           new SolutionManagementError {
                    Description = "A customer can have one subscription only."
                }));
            }

            await this.context.CustomerSubscriptions.AddAsync(subscription);

            await this.context.SaveChangesAsync();

            return(SolutionManagerOperationResult.Success);
        }
        public async Task <SolutionManagerOperationResult> DeleteCustomerUserAsync(CustomerUser customerUser)
        {
            var foundCustomerUser = await this.context.CustomerUsers.FirstOrDefaultAsync(
                u => u.UserName == customerUser.UserName);

            if (foundCustomerUser != default(CustomerUser))
            {
                this.context.CustomerUsers.Remove(foundCustomerUser);
                await this.context.SaveChangesAsync();

                return(SolutionManagerOperationResult.Success);
            }

            return(SolutionManagerOperationResult.Failed());
        }
        public async Task <SolutionManagerOperationResult> ReactivateSubscriptionAsync(Guid subscriptionId)
        {
            var customerSubscription =
                await this.context.CustomerSubscriptions.FirstOrDefaultAsync(s => s.SubscriptionId == subscriptionId);

            if (customerSubscription != default)
            {
                customerSubscription.State             = SubscriptionState.Complete;
                customerSubscription.LastOperationTime = DateTimeOffset.UtcNow;
                await this.context.SaveChangesAsync();

                return(SolutionManagerOperationResult.Success);
            }

            return(SolutionManagerOperationResult.Failed(new SolutionManagementError
            {
                Description = "Subscription not found."
            }));
        }
        public async Task <SolutionManagerOperationResult> SuspendSubscriptionAsync(Guid subscriptionId)
        {
            var subscription =
                await this.context.CustomerSubscriptions.FirstOrDefaultAsync(s => s.SubscriptionId == subscriptionId);

            if (subscription == default)
            {
                return(SolutionManagerOperationResult.Failed(new SolutionManagementError
                {
                    Description = $"No subscription is found with subscription ID {subscriptionId}."
                }));
            }

            subscription.State             = SubscriptionState.Suspended;
            subscription.LastOperationTime = DateTimeOffset.UtcNow;
            await this.context.SaveChangesAsync();

            return(SolutionManagerOperationResult.Success);
        }
        public async Task <SolutionManagerOperationResult> UpdateSubscriptionAsync(Guid subscriptionId, string planName)
        {
            var subscription = await this.context.CustomerSubscriptions.Include(s => s.Customer)
                               .FirstOrDefaultAsync(s => s.SubscriptionId == subscriptionId);

            if (subscription == default)
            {
                return(SolutionManagerOperationResult.Failed(new SolutionManagementError
                {
                    Description = $"No subscription is found with subscription ID {subscriptionId}."
                }));
            }

            var sku = await this.context.Skus.FirstOrDefaultAsync(s => s.Name == planName);

            subscription.Sku               = sku;
            subscription.SkuId             = sku.Id;
            subscription.LastOperationTime = DateTimeOffset.UtcNow;
            await this.context.SaveChangesAsync();

            return(SolutionManagerOperationResult.Success);
        }
 private static SolutionManagerOperationResult GenerateSingleError(string message)
 {
     return(SolutionManagerOperationResult.Failed(new SolutionManagementError {
         Description = message
     }));
 }
 private static SolutionManagerOperationResult ReturnFail(Exception ex)
 {
     return(SolutionManagerOperationResult.Failed(new SolutionManagementError {
         Description = ex.ToString()
     }));
 }