Esempio n. 1
0
        /// <summary>
        /// Creates a subscription within a plan within an offer.
        /// </summary>
        /// <param name="subscription">The subscription to create.</param>
        /// <returns>The created subscription.</returns>
        public async Task <Subscription> CreateAsync(Subscription subscription)
        {
            if (subscription is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Subscription).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            if (await ExistsAsync(subscription.SubscriptionId))
            {
                throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(Subscription).Name,
                                                                                                  subscription.SubscriptionId.ToString()));
            }
            _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName, payload: JsonSerializer.Serialize(subscription)));

            var offerParameters = await _offerParameterService.GetAllAsync(subscription.OfferName);

            foreach (var param in offerParameters)
            {
                // Check if value of all offer parameters are provided with correct type
                if (subscription.InputParameters.Where(x => x.Name.Equals(param.ParameterName) && x.Type.Equals(param.ValueType)).Count() < 1)
                {
                    throw new LunaBadRequestUserException($"Value of parameter {param.ParameterName} is not provided, or the type doesn't match.", UserErrorCode.ParameterNotProvided);
                }
            }

            // Get the offer associated with the offerName provided
            var offer = await _offerService.GetAsync(subscription.OfferName);


            // Get the plan associated with the planUniqueName provided
            var plan = await _planService.GetAsync(subscription.OfferName, subscription.PlanName);

            // Set the FK to offer
            subscription.OfferId = offer.Id;

            // Set the FK to plan
            subscription.PlanId = plan.Id;

            // Always set quantity to 1 to walkaround a marketplace service bug
            subscription.Quantity = 1;

            // Set the created time
            subscription.CreatedTime = DateTime.UtcNow;

            subscription.Status = nameof(FulfillmentState.PendingFulfillmentStart);

            subscription.ProvisioningStatus = nameof(ProvisioningState.ProvisioningPending);

            subscription.ProvisioningType = nameof(ProvisioningType.Subscribe);

            subscription.RetryCount = 0;

            List <CustomMeter> customMeterList = await _customMeterService.GetAllAsync(offer.OfferName);

            using (var transaction = await _context.BeginTransactionAsync())
            {
                // Add subscription to db
                _context.Subscriptions.Add(subscription);
                await _context._SaveChangesAsync();

                // Add subscription parameters
                foreach (var param in subscription.InputParameters)
                {
                    param.SubscriptionId = subscription.SubscriptionId;
                    _context.SubscriptionParameters.Add(param);
                }
                await _context._SaveChangesAsync();

                foreach (var meter in customMeterList)
                {
                    _context.SubscriptionCustomMeterUsages.Add(new SubscriptionCustomMeterUsage(meter.Id, subscription.SubscriptionId));
                }

                await _context._SaveChangesAsync();

                transaction.Commit();
            }
            _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName));

            return(subscription);
        }
Esempio n. 2
0
 public async Task <ActionResult> GetAllAsync(string offerName)
 {
     // all users can call this API
     _logger.LogInformation($"Get all offer parameters from offer {offerName}.");
     return(Ok(await _offerParameterService.GetAllAsync(offerName)));
 }