Store rule emails in HTML + Test rule emails (#4843)

* Store rule emails in HTML + Test rule emails

* Store rule emails in HTML + Test rule emails

* Update BTCPayServer/Controllers/UIStoresController.Email.cs

Thanks !

Co-authored-by: Andrew Camilleri <evilkukka@gmail.com>

* change in StoreEmails() to work properly with add commande

* CanSetupEmailServer() : Remove Bosy sendkeys that doesn't work with new HTML control

* Update test for new HTML control

* better command treatment

---------

Co-authored-by: nisaba <infos@nisaba.solutions>
Co-authored-by: Andrew Camilleri <evilkukka@gmail.com>
This commit is contained in:
Nisaba
2023-04-04 03:52:42 +02:00
committed by GitHub
parent 60cfea9f94
commit 57544068e9
3 changed files with 72 additions and 15 deletions

View File

@@ -467,7 +467,7 @@ namespace BTCPayServer.Tests
s.Driver.FindElement(By.Id("Rules_0__To")).SendKeys("test@gmail.com"); s.Driver.FindElement(By.Id("Rules_0__To")).SendKeys("test@gmail.com");
s.Driver.FindElement(By.Id("Rules_0__CustomerEmail")).Click(); s.Driver.FindElement(By.Id("Rules_0__CustomerEmail")).Click();
s.Driver.FindElement(By.Id("Rules_0__Subject")).SendKeys("Thanks!"); s.Driver.FindElement(By.Id("Rules_0__Subject")).SendKeys("Thanks!");
s.Driver.FindElement(By.Id("Rules_0__Body")).SendKeys("Your invoice is settled"); s.Driver.FindElement(By.ClassName("note-editable")).SendKeys("Your invoice is settled");
s.Driver.FindElement(By.Id("SaveEmailRules")).Click(); s.Driver.FindElement(By.Id("SaveEmailRules")).Click();
Assert.Contains("Store email rules saved", s.FindAlertMessage().Text); Assert.Contains("Store email rules saved", s.FindAlertMessage().Text);
} }

View File

@@ -43,10 +43,16 @@ namespace BTCPayServer.Controllers
public async Task<IActionResult> StoreEmails(string storeId, StoreEmailRuleViewModel vm, string command) public async Task<IActionResult> StoreEmails(string storeId, StoreEmailRuleViewModel vm, string command)
{ {
vm.Rules ??= new List<StoreEmailRule>(); vm.Rules ??= new List<StoreEmailRule>();
int index = 0;
var indSep = command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
if (indSep > 0)
{
var item = command[(indSep + 1)..];
index = int.Parse(item, CultureInfo.InvariantCulture);
}
if (command.StartsWith("remove", StringComparison.InvariantCultureIgnoreCase)) if (command.StartsWith("remove", StringComparison.InvariantCultureIgnoreCase))
{ {
var item = command[(command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 1)..];
var index = int.Parse(item, CultureInfo.InvariantCulture);
vm.Rules.RemoveAt(index); vm.Rules.RemoveAt(index);
} }
else if (command == "add") else if (command == "add")
@@ -61,9 +67,46 @@ namespace BTCPayServer.Controllers
} }
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
return NotFound(); return NotFound();
var blob = store.GetStoreBlob(); var blob = store.GetStoreBlob();
if (command.StartsWith("test", StringComparison.InvariantCultureIgnoreCase))
{
var rule = vm.Rules[index];
if (string.IsNullOrEmpty(rule.Subject) || string.IsNullOrEmpty(rule.Body) || string.IsNullOrEmpty(rule.To))
{
TempData.SetStatusMessageModel(new StatusMessageModel
{
Severity = StatusMessageModel.StatusSeverity.Warning,
Message = "Please fill all required fields before testing"
});
}
else
{
try
{
var emailSettings = blob.EmailSettings;
using var client = await emailSettings.CreateSmtpClient();
var message = emailSettings.CreateMailMessage(MailboxAddress.Parse(rule.To), "(test) " + rule.Subject, rule.Body, true);
await client.SendAsync(message);
await client.DisconnectAsync(true);
TempData[WellKnownTempData.SuccessMessage] = $"Rule email saved and sent to {rule.To}. Please verify you received it.";
blob.EmailRules = vm.Rules;
store.SetStoreBlob(blob);
await _Repo.UpdateStore(store);
}
catch (Exception ex)
{
TempData[WellKnownTempData.ErrorMessage] = "Error: " + ex.Message;
}
}
}
else
{
// UPDATE
blob.EmailRules = vm.Rules; blob.EmailRules = vm.Rules;
store.SetStoreBlob(blob); store.SetStoreBlob(blob);
await _Repo.UpdateStore(store); await _Repo.UpdateStore(store);
@@ -72,6 +115,7 @@ namespace BTCPayServer.Controllers
Severity = StatusMessageModel.StatusSeverity.Success, Severity = StatusMessageModel.StatusSeverity.Success,
Message = "Store email rules saved" Message = "Store email rules saved"
}); });
}
return RedirectToAction("StoreEmails", new { storeId }); return RedirectToAction("StoreEmails", new { storeId });
} }

View File

@@ -5,6 +5,11 @@
Layout = "../Shared/_NavLayout.cshtml"; Layout = "../Shared/_NavLayout.cshtml";
ViewData.SetActivePage(StoreNavPages.Emails, "Email Rules", Context.GetStoreData().Id); ViewData.SetActivePage(StoreNavPages.Emails, "Email Rules", Context.GetStoreData().Id);
} }
@section PageHeadContent {
<link href="~/vendor/summernote/summernote-bs5.css" rel="stylesheet" asp-append-version="true" />
}
<form class="row" asp-action="StoreEmails" method="post" asp-route-storeId="@Context.GetStoreData().Id"> <form class="row" asp-action="StoreEmails" method="post" asp-route-storeId="@Context.GetStoreData().Id">
<div class="col-xxl-constrain"> <div class="col-xxl-constrain">
<div class="d-flex align-items-center justify-content-between mt-n1 mb-3"> <div class="d-flex align-items-center justify-content-between mt-n1 mb-3">
@@ -33,10 +38,16 @@
<div class="form-group"> <div class="form-group">
<div class="d-flex align-items-center justify-content-between gap-3"> <div class="d-flex align-items-center justify-content-between gap-3">
<label asp-for="Rules[index].Trigger" class="form-label" data-required></label> <label asp-for="Rules[index].Trigger" class="form-label" data-required></label>
<div class="align-items-right">
<button name="command" type="submit" value="test:@index" class="d-inline-block btn text-info btn-link p-0">
<span class="fa fa-send"></span> Test this email rule
</button>
&nbsp;
<button name="command" type="submit" value="remove:@index" class="d-inline-block btn text-danger btn-link p-0"> <button name="command" type="submit" value="remove:@index" class="d-inline-block btn text-danger btn-link p-0">
<span class="fa fa-times"></span> Remove this email rule <span class="fa fa-times"></span> Remove this email rule
</button> </button>
</div> </div>
</div>
<select asp-for="Rules[index].Trigger" asp-items="@Html.GetEnumSelectList<WebhookEventType>()" class="form-select" required></select> <select asp-for="Rules[index].Trigger" asp-items="@Html.GetEnumSelectList<WebhookEventType>()" class="form-select" required></select>
<span asp-validation-for="Rules[index].Trigger" class="text-danger"></span> <span asp-validation-for="Rules[index].Trigger" class="text-danger"></span>
<div class="form-text">Choose what event sends the email.</div> <div class="form-text">Choose what event sends the email.</div>
@@ -59,7 +70,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="Rules[index].Body" class="form-label" ></label> <label asp-for="Rules[index].Body" class="form-label" ></label>
<textarea asp-for="Rules[index].Body" class="form-control" rows="4"></textarea> <textarea asp-for="Rules[index].Body" class="form-control richtext" rows="4"></textarea>
<span asp-validation-for="Rules[index].Body" class="text-danger"></span> <span asp-validation-for="Rules[index].Body" class="text-danger"></span>
</div> </div>
</li> </li>
@@ -74,6 +85,8 @@
} }
</div> </div>
</form> </form>
@section PageFootContent { @section PageFootContent {
<partial name="_ValidationScriptsPartial" /> <partial name="_ValidationScriptsPartial" />
<script src="~/vendor/summernote/summernote-bs5.js" asp-append-version="true"></script>
} }