in src/CustomerSite/Controllers/HomeController.cs [754:832]
public async Task<IActionResult> ChangeSubscriptionQuantity(SubscriptionResult subscriptionDetail)
{
this.logger.Info(HttpUtility.HtmlEncode($"Home Controller / ChangeSubscriptionPlan subscriptionDetail:{JsonSerializer.Serialize(subscriptionDetail)}"));
if (this.User.Identity.IsAuthenticated)
{
try
{
if (subscriptionDetail != null && subscriptionDetail.Id != default && subscriptionDetail.Quantity > 0)
{
try
{
//initiate change quantity
var currentUserId = this.userService.GetUserIdFromEmailAddress(this.CurrentUserEmailAddress);
if (this.subscriptionService.GetPartnerSubscription(this.CurrentUserEmailAddress, subscriptionDetail.Id).FirstOrDefault() == null)
{
this.logger.LogError($"Cannot find subscription or subscription associated to the current user");
return this.RedirectToAction(nameof(this.Index));
}
var jsonResult = await this.apiService.ChangeQuantityForSubscriptionAsync(subscriptionDetail.Id, subscriptionDetail.Quantity).ConfigureAwait(false);
var changeQuantityOperationStatus = OperationStatusEnum.InProgress;
if (jsonResult != null && jsonResult.OperationId != default)
{
int _counter = 0;
while (OperationStatusEnum.InProgress.Equals(changeQuantityOperationStatus) || OperationStatusEnum.NotStarted.Equals(changeQuantityOperationStatus))
{
//loop untill the operation status has moved away from inprogress or notstarted, generally this will be the result of webhooks' action aganist this operation
var changeQuantityOperationResult = await this.apiService.GetOperationStatusResultAsync(subscriptionDetail.Id, jsonResult.OperationId).ConfigureAwait(false);
changeQuantityOperationStatus = changeQuantityOperationResult.Status;
this.logger.Info(HttpUtility.HtmlEncode($"Quantity Change Progress. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: **** OperationId: {jsonResult.OperationId} Operationstatus: { changeQuantityOperationStatus }."));
await this.applicationLogService.AddApplicationLog($"Quantity Change Progress. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: {currentUserId} OperationId: {jsonResult.OperationId} Operationstatus: { changeQuantityOperationStatus }.").ConfigureAwait(false);
//wait and check every 5secs
await Task.Delay(5000);
_counter++;
if (_counter > 100)
{
//if loop has been executed for more than 100 times then break, to avoid infinite loop just in case
break;
}
}
if (changeQuantityOperationStatus == OperationStatusEnum.Succeeded)
{
this.logger.Info(HttpUtility.HtmlEncode($"Quantity Change Success. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: ***** OperationId: {jsonResult.OperationId}."));
await this.applicationLogService.AddApplicationLog($"Quantity Change Success. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: {currentUserId} OperationId: {jsonResult.OperationId}.").ConfigureAwait(false);
}
else
{
this.logger.Info(HttpUtility.HtmlEncode($"Quantity Change Failed. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: ***** OperationId: {jsonResult.OperationId} Operationstatus: { changeQuantityOperationStatus }."));
await this.applicationLogService.AddApplicationLog($"Quantity Change Failed. SubscriptionId: {subscriptionDetail.Id} ToQuantity: {subscriptionDetail.Quantity} UserId: {currentUserId} OperationId: {jsonResult.OperationId} Operationstatus: { changeQuantityOperationStatus }.").ConfigureAwait(false);
throw new MarketplaceException($"Quantity Change operation failed with operation status {changeQuantityOperationStatus}.");
}
}
}
catch (MarketplaceException fex)
{
this.TempData["ErrorMsg"] = fex.Message;
this.logger.LogError($"Message:{fex.Message} :: {fex.InnerException} ");
}
}
return this.RedirectToAction(nameof(this.Subscriptions));
}
catch (Exception ex)
{
this.logger.LogError($"Message:{ex.Message} :: {ex.InnerException} ");
return this.View("Error", ex);
}
}
else
{
return this.RedirectToAction(nameof(this.Index));
}
}