public async Task <IActionResult> RenewApiKey(Guid subscriptionId)
        {
            var ukprn = _contextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.portal.com/ukprn")?.Value;

            try
            {
                var externalApiSubscriptions = await GetExternalApiSubscriptions(_webConfiguration.AzureApiAuthentication.ProductId, ukprn);

                var subscription = externalApiSubscriptions?.Where(p => p.Id == subscriptionId.ToString()).FirstOrDefault();

                if (subscription == null)
                {
                    throw new Exception($"The subscription {subscriptionId} is invalid or does not belong to organsiation identified by {ukprn}");
                }

                var viewModel = new RenewApiKeyViewModel
                {
                    SubscriptionId   = subscription.Id,
                    CurrentKey       = subscription.PrimaryKey,
                    LastRenewedDate  = subscription.CreatedDate,
                    LastRenewedTicks = subscription.CreatedDate.Ticks
                };

                return(View(viewModel));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to renew API key");
            }

            return(RedirectToAction(nameof(OrganisationDetails), nameof(OrganisationController).RemoveController(), "api-subscription"));
        }
        public async Task <IActionResult> RenewApiKey(RenewApiKeyViewModel vm)
        {
            var ukprn = _contextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.portal.com/ukprn")?.Value;

            try
            {
                var externalApiSubscriptions = await GetExternalApiSubscriptions(_webConfiguration.AzureApiAuthentication.ProductId, ukprn);

                var subscription = externalApiSubscriptions?.FirstOrDefault(p => p.Id == vm.SubscriptionId.ToString());

                if (subscription == null || !subscription.CreatedDate.Ticks.Equals(vm.LastRenewedTicks) || !subscription.PrimaryKey.Equals(vm.CurrentKey))
                {
                    TempData.SetAlert(new Alert {
                        Message = "Your API key could not be renewed, please check the current value and retry if necessary.", Type = AlertType.Warning
                    });
                    return(RedirectToAction(nameof(RenewApiKey), nameof(OrganisationController).RemoveController(), "api-subscription"));
                }

                // delete and re-subscribe so that the created date can be used to track a 'renewed' key
                if (await _externalApiClient.DeleteSubscriptionAndResubscribe(ukprn, subscription.Id))
                {
                    TempData.SetAlert(new Alert {
                        Message = "Your API key has been renewed", Type = AlertType.Success
                    });
                }
                else
                {
                    TempData.SetAlert(new Alert {
                        Message = "Your API key could not be renewed, please check the current value and retry if necessary.", Type = AlertType.Warning
                    });
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to renew API key");
            }

            return(RedirectToAction(nameof(OrganisationDetails), nameof(OrganisationController).RemoveController(), "api-subscription"));
        }