public async Task <IActionResult> Create(CreateBTCPayEmailReceiptsViewModel viewModel)
        {
            var services = await GetServices();

            viewModel.BTCPayServices = new SelectList(services.btcPayServices, nameof(ExternalServiceData.Id),
                                                      nameof(ExternalServiceData.Name));
            viewModel.SMTPServices = new SelectList(services.smtpServices, nameof(ExternalServiceData.Id),
                                                    nameof(ExternalServiceData.Name));

            if (!viewModel.SendToCustomer && string.IsNullOrEmpty(viewModel.AlternateEmail))
            {
                ModelState.AddModelError(nameof(viewModel.SendToCustomer),
                                         "You need to send the email to either the customer or the BCC email address");
                ModelState.AddModelError(nameof(viewModel.AlternateEmail),
                                         "You need to send the email to either the customer or the BCC email address");
            }


            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            return(await SetItUp(viewModel));
        }
        private async Task <IActionResult> SetItUp(CreateBTCPayEmailReceiptsViewModel vm)
        {
            var presetName = $"Generated_BTCPayEmailReceipts_{vm.Status}";

            var recipe = new Recipe()
            {
                Name        = presetName,
                Description = "Generated from a preset",
                UserId      = _userManager.GetUserId(User),
                Enabled     = false
            };
            await _recipeManager.AddOrUpdateRecipe(recipe);

            var recipeTrigger = new RecipeTrigger()
            {
                ExternalServiceId = vm.SelectedBTCPayExternalServiceId,
                TriggerId         = new InvoiceStatusChangedTrigger().Id,
                RecipeId          = recipe.Id
            };

            recipeTrigger.Set(new InvoiceStatusChangedTriggerParameters()
            {
                Conditions = new List <InvoiceStatusChangeCondition>()
                {
                    new InvoiceStatusChangeCondition()
                    {
                        Status            = vm.Status,
                        ExceptionStatuses = InvoiceStatusChangedController.AllowedExceptionStatus.Select(item => item.Value).ToList()
                    }
                }
            });
            await _recipeManager.AddOrUpdateRecipeTrigger(recipeTrigger);

            var recipeActionGroup = new RecipeActionGroup()
            {
                RecipeId = recipe.Id
            };
            await _recipeManager.AddRecipeActionGroup(recipeActionGroup);

            var recipeActionGroupIndex = 0;

            if (vm.SendToCustomer)
            {
                var sendEmailAction = new RecipeAction()
                {
                    RecipeId            = recipe.Id,
                    RecipeActionGroupId = recipeActionGroup.Id,
                    ActionId            = new SendEmailDataActionHandler().ActionId,
                    ExternalServiceId   = vm.SelectedSMTPExternalServiceId,
                    Order    = recipeActionGroupIndex,
                    DataJson = JsonConvert.SerializeObject(new SendEmailData()
                    {
                        IsHTML  = true,
                        Body    = vm.HTMLBody,
                        Subject = vm.Subject,
                        From    = vm.From,
                        To      = "{{TriggerData.Invoice.Buyer.email}}"
                    })
                };
                await _recipeManager.AddOrUpdateRecipeAction(sendEmailAction);

                recipeActionGroupIndex++;
            }
            if (!string.IsNullOrEmpty(vm.AlternateEmail))
            {
                var sendEmailAction = new RecipeAction()
                {
                    RecipeId            = recipe.Id,
                    RecipeActionGroupId = recipeActionGroup.Id,
                    ActionId            = new SendEmailDataActionHandler().ActionId,
                    ExternalServiceId   = vm.SelectedSMTPExternalServiceId,
                    Order    = recipeActionGroupIndex,
                    DataJson = JsonConvert.SerializeObject(new SendEmailData()
                    {
                        IsHTML  = true,
                        Body    = vm.HTMLBody,
                        Subject = vm.Subject,
                        From    = vm.From,
                        To      = vm.AlternateEmail
                    })
                };
                await _recipeManager.AddOrUpdateRecipeAction(sendEmailAction);

                recipeActionGroupIndex++;
            }


            return(RedirectToAction("EditRecipe", "Recipes", new
            {
                id = recipe.Id,
                statusMessage =
                    "Preset generated. Recipe is currently disabled for now. Please verify details are correct before enabling!"
            }));
        }