예제 #1
0
        private async Task <IActionResult> SetItUp(CreateDCAViewModel vm)
        {
            var presetName = $"Generated_DCA";

            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()
            {
                TriggerId = new TimerTrigger().Id,
                RecipeId  = recipe.Id
            };

            recipeTrigger.Set(new TimerTriggerParameters()
            {
                StartOn            = vm.StartOn,
                TriggerEvery       = vm.TriggerEvery,
                TriggerEveryAmount = vm.TriggerEveryAmount
            });
            await _recipeManager.AddOrUpdateRecipeTrigger(recipeTrigger);

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

            await _recipeManager.AddRecipeActionGroup(recipeActionGroup);

            var tradeAction = new RecipeAction()
            {
                RecipeId            = recipe.Id,
                RecipeActionGroupId = recipeActionGroup.Id,
                ActionId            = new PlaceOrderDataActionHandler().ActionId,
                ExternalServiceId   = vm.SelectedExchangeServiceId,
                Order    = 0,
                DataJson = JsonConvert.SerializeObject(new PlaceOrderData()
                {
                    Amount       = vm.FiatAmount.ToString(CultureInfo.InvariantCulture),
                    IsBuy        = vm.IsBuy,
                    MarketSymbol = vm.MarketSymbol,
                    OrderType    = OrderType.Market
                })
            };

            await _recipeManager.AddOrUpdateRecipeAction(tradeAction);


            return(RedirectToAction("EditRecipe", "Recipes", new
            {
                id = recipe.Id,
                statusMessage =
                    "Preset generated. Recipe is currently disabled for now. Please verify details are correct before enabling!"
            }));
        }
예제 #2
0
        private async Task <bool> UpdateTxToRecipeTrigger(TransactionResult transactionResult,
                                                          RecipeTrigger recipeTrigger, NBXplorerNewTransactionTriggerParameters parameters)
        {
            if (parameters.Transactions == null)
            {
                parameters.Transactions = new List <TransactionResult>();
            }
            var matchedIndex = parameters.Transactions.FindIndex(i =>
                                                                 i.TransactionHash == transactionResult.TransactionHash);
            var confirmations = transactionResult.Confirmations;

            if (matchedIndex != -1)
            {
                parameters.Transactions.RemoveAt(matchedIndex);
            }

            var result = parameters.ConfirmationsRequired < confirmations;

            if (!result)
            {
                parameters.Transactions.Add(transactionResult);
            }

            if (!result || matchedIndex != -1)
            {
                recipeTrigger.Set(parameters);
                await _recipeManager.AddOrUpdateRecipeTrigger(recipeTrigger);
            }

            return(result);
        }
        protected override Task <(RecipeTrigger ToSave, DynamicServiceMarkerTriggerParametersViewModel showViewModel)> BuildModel(
            DynamicServiceMarkerTriggerParametersViewModel viewModel, RecipeTrigger mainModel)
        {
            mainModel.Set(viewModel);

            return(Task.FromResult <(RecipeTrigger ToSave, DynamicServiceMarkerTriggerParametersViewModel showViewModel)>((mainModel,
                                                                                                                           null)));
        }
        private void SetValues(InvoiceStatusChangedTriggerViewModel from, RecipeTrigger to)
        {
            to.ExternalServiceId = from.ExternalServiceId;
            to.RecipeId          = from.RecipeId;
            var currentData = to.Get <InvoiceStatusChangedTriggerParameters>();

            currentData.Status = from.Status;
            to.Set(currentData);
        }
예제 #5
0
        protected override Task <(RecipeTrigger ToSave, TimerTriggerViewModel showViewModel)> BuildModel(
            TimerTriggerViewModel viewModel, RecipeTrigger mainModel)
        {
            if (viewModel.TriggerEveryAmount <= 0)
            {
                ModelState.AddModelError(nameof(TimerTriggerViewModel.TriggerEveryAmount),
                                         "Amount needs to be at least 1");
            }

            if (!ModelState.IsValid)
            {
                return(Task.FromResult <(RecipeTrigger ToSave, TimerTriggerViewModel showViewModel)>((null, viewModel)));
            }

            mainModel.Set((TimerTriggerParameters)viewModel);

            return(Task.FromResult <(RecipeTrigger ToSave, TimerTriggerViewModel showViewModel)>((mainModel, null)));
        }
예제 #6
0
        BuildModel(
            InvoiceStatusChangedTriggerViewModel viewModel, RecipeTrigger mainModel)
        {
            if (viewModel.Action != "EditData" || !ModelState.IsValid)
            {
                var btcPayServices = await _externalServiceManager.GetExternalServicesData(
                    new ExternalServicesDataQuery()
                {
                    Type   = new[] { BtcPayServerService.BtcPayServerServiceType },
                    UserId = GetUserId()
                });

                viewModel.ExternalServices = new SelectList(btcPayServices, nameof(ExternalServiceData.Id),
                                                            nameof(ExternalServiceData.Name), viewModel.ExternalServiceId);


                if (viewModel.Action.StartsWith("add-condition", StringComparison.InvariantCultureIgnoreCase))
                {
                    viewModel.Conditions.Add(new InvoiceStatusChangeCondition()
                    {
                        Status            = GetAvailableStatuses(viewModel.Conditions.Select(condition => condition.Status).ToArray()).FirstOrDefault()?.Value ?? Invoice.STATUS_NEW,
                        ExceptionStatuses = new List <string>()
                        {
                            Invoice.EXSTATUS_FALSE
                        }
                    });
                }

                if (viewModel.Action.StartsWith("remove-condition", StringComparison.InvariantCultureIgnoreCase))
                {
                    var index = int.Parse(viewModel.Action.Substring(viewModel.Action.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 1));
                    viewModel.Conditions.RemoveAt(index);
                }

                return(null, viewModel);
            }

            mainModel.ExternalServiceId = viewModel.ExternalServiceId;
            mainModel.Set((InvoiceStatusChangedTriggerParameters)viewModel);
            return(mainModel, null);
        }
예제 #7
0
        protected override Task <(RecipeTrigger ToSave, ReceiveWebRequestTriggerViewModel showViewModel)> BuildModel(
            ReceiveWebRequestTriggerViewModel viewModel, RecipeTrigger mainModel)
        {
            if (!string.IsNullOrEmpty(viewModel.Body) &&
                viewModel.BodyComparer == ReceiveWebRequestTriggerParameters.FieldComparer.None)
            {
                ModelState.AddModelError(nameof(ReceiveWebRequestTriggerViewModel.BodyComparer),
                                         "Please choose a comparison type or leave field blank");
            }

            if (!ModelState.IsValid)
            {
                return(Task.FromResult <(RecipeTrigger ToSave, ReceiveWebRequestTriggerViewModel showViewModel)>((null,
                                                                                                                  viewModel)));
            }

            mainModel.Set((ReceiveWebRequestTriggerParameters)viewModel);

            return(Task.FromResult <(RecipeTrigger ToSave, ReceiveWebRequestTriggerViewModel showViewModel)>((mainModel,
                                                                                                              null)));
        }
        BuildModel(
            InvoiceStatusChangedTriggerViewModel viewModel, RecipeTrigger mainModel)
        {
            if (!ModelState.IsValid)
            {
                var btcPayServices = await _externalServiceManager.GetExternalServicesData(
                    new ExternalServicesDataQuery()
                {
                    Type   = new[] { BtcPayServerService.BtcPayServerServiceType },
                    UserId = GetUserId()
                });


                viewModel.Statuses         = new SelectList(AllowedStatuses, "Value", "Text", viewModel.Status);
                viewModel.ExternalServices = new SelectList(btcPayServices, nameof(ExternalServiceData.Id),
                                                            nameof(ExternalServiceData.Name), viewModel.ExternalServiceId);
                return(null, viewModel);
            }

            mainModel.ExternalServiceId = viewModel.ExternalServiceId;
            mainModel.Set((InvoiceStatusChangedTriggerParameters)viewModel);
            return(mainModel, null);
        }
        private async Task <IActionResult> SetItUp(CreateFiatExchangeConversionViewModel vm)
        {
            var presetName = $"Generated_FiatExchangeConversion";

            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
            };

            foreach (var condition in vm.Conditions)
            {
                await _recipeManager.AddRecipeActionGroup(recipeActionGroup);

                var recipeActionGroupIndex = 0;
                var getPayments            = new RecipeAction()
                {
                    RecipeId            = recipe.Id,
                    RecipeActionGroupId = recipeActionGroup.Id,
                    ActionId            = new GetPaymentsFromInvoiceDataActionHandler().ActionId,
                    ExternalServiceId   = vm.SelectedBTCPayExternalServiceId,
                    Order    = recipeActionGroupIndex,
                    DataJson = JsonConvert.SerializeObject(new GetPaymentsFromInvoiceData()
                    {
                        CryptoCode  = condition.CryptoCode,
                        InvoiceId   = "{{TriggerData.Invoice.Id}}",
                        PaymentType = ""
                    })
                };
                recipeActionGroupIndex++;
                await _recipeManager.AddOrUpdateRecipeAction(getPayments);

                var tradeAction = new RecipeAction()
                {
                    RecipeId            = recipe.Id,
                    RecipeActionGroupId = recipeActionGroup.Id,
                    ActionId            = new PlaceOrderDataActionHandler().ActionId,
                    ExternalServiceId   = condition.ExchangeServiceId,
                    Order    = recipeActionGroupIndex,
                    DataJson = JsonConvert.SerializeObject(new PlaceOrderData()
                    {
                        Amount       = "{{PreviousAction.Sum(" + nameof(InvoicePaymentInfo.Value) + ")}}",
                        IsBuy        = condition.IsBuy,
                        MarketSymbol = condition.MarketSymbol,
                        OrderType    = OrderType.Market
                    })
                };

                recipeActionGroupIndex++;
                await _recipeManager.AddOrUpdateRecipeAction(tradeAction);
            }

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


            Assert.False(await triggerHandler.IsTriggered(new TestTrigger(), new RecipeTrigger()
            {
                TriggerId = triggerHandler.TriggerId
            }));

            Assert.False(await triggerHandler.IsTriggered(new TestTrigger(), new RecipeTrigger()
            {
                TriggerId = Guid.NewGuid().ToString()
            }));

            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                }
            }, new RecipeTrigger()
            {
                TriggerId = triggerHandler.TriggerId
            }));

            var recipeTrigger = new RecipeTrigger()
            {
                TriggerId = triggerHandler.TriggerId,
            };

            recipeTrigger.Set(new ReceiveWebRequestTriggerParameters()
            {
                RelativeUrl = "x",
                Method      = ""
            });
            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "test"
                }
            }, recipeTrigger));

            Assert.True(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x"
                }
            }, recipeTrigger));

            recipeTrigger.Set(new ReceiveWebRequestTriggerParameters()
            {
                RelativeUrl  = "x",
                Body         = "hello",
                Method       = "",
                BodyComparer = ReceiveWebRequestTriggerParameters.FieldComparer.Contains
            });
            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Body        = "sdas"
                }
            }, recipeTrigger));

            Assert.True(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Body        = "ads hello xasda"
                }
            }, recipeTrigger));


            recipeTrigger.Set(new ReceiveWebRequestTriggerParameters()
            {
                RelativeUrl  = "x",
                Body         = "hello",
                BodyComparer = ReceiveWebRequestTriggerParameters.FieldComparer.Equals,
                Method       = ""
            });
            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Body        = "sdas"
                }
            }, recipeTrigger));

            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Body        = "ads hello xasda"
                }
            }, recipeTrigger));
            Assert.True(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Body        = "hello"
                }
            }, recipeTrigger));

            recipeTrigger.Set(new ReceiveWebRequestTriggerParameters()
            {
                RelativeUrl = "",
                Method      = HttpMethods.Post
            });

            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "x",
                    Method      = HttpMethods.Post
                }
            }, recipeTrigger));

            Assert.True(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "",
                    Method      = HttpMethods.Post
                }
            }, recipeTrigger));
            Assert.False(await triggerHandler.IsTriggered(new ReceiveWebRequestTrigger()
            {
                Data = new ReceiveWebRequestTriggerData()
                {
                    RelativeUrl = "",
                    Method      = HttpMethods.Put
                }
            }, recipeTrigger));
        }
 private void SetValues(ReceivedEmailViewModel from, RecipeTrigger to)
 {
     to.ExternalServiceId = from.ExternalServiceId;
     to.RecipeId          = from.RecipeId;
     to.Set((ReceivedEmailTriggerParameters)from);
 }
        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!"
            }));
        }