Crowdfund: Display contributions value (#2938)

This commit is contained in:
d11n
2021-10-04 07:54:06 +02:00
committed by GitHub
parent b0814166f5
commit 975ad2f8ab
9 changed files with 67 additions and 30 deletions

View File

@@ -55,6 +55,7 @@ namespace BTCPayServer.Controllers
AppId = appId, AppId = appId,
SearchTerm = app.TagAllInvoices ? $"storeid:{app.StoreDataId}" : $"orderid:{AppService.GetCrowdfundOrderId(appId)}", SearchTerm = app.TagAllInvoices ? $"storeid:{app.StoreDataId}" : $"orderid:{AppService.GetCrowdfundOrderId(appId)}",
DisplayPerksRanking = settings.DisplayPerksRanking, DisplayPerksRanking = settings.DisplayPerksRanking,
DisplayPerksValue = settings.DisplayPerksValue,
SortPerksByPopularity = settings.SortPerksByPopularity, SortPerksByPopularity = settings.SortPerksByPopularity,
Sounds = string.Join(Environment.NewLine, settings.Sounds), Sounds = string.Join(Environment.NewLine, settings.Sounds),
AnimationColors = string.Join(Environment.NewLine, settings.AnimationColors) AnimationColors = string.Join(Environment.NewLine, settings.AnimationColors)
@@ -142,6 +143,7 @@ namespace BTCPayServer.Controllers
AnimationsEnabled = vm.AnimationsEnabled, AnimationsEnabled = vm.AnimationsEnabled,
ResetEveryAmount = vm.ResetEveryAmount, ResetEveryAmount = vm.ResetEveryAmount,
ResetEvery = Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery), ResetEvery = Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery),
DisplayPerksValue = vm.DisplayPerksValue,
DisplayPerksRanking = vm.DisplayPerksRanking, DisplayPerksRanking = vm.DisplayPerksRanking,
SortPerksByPopularity = vm.SortPerksByPopularity, SortPerksByPopularity = vm.SortPerksByPopularity,
Sounds = parsedSounds, Sounds = parsedSounds,

View File

@@ -92,6 +92,9 @@ namespace BTCPayServer.Models.AppViewModels
[Display(Name = "Display contribution ranking")] [Display(Name = "Display contribution ranking")]
public bool DisplayPerksRanking { get; set; } public bool DisplayPerksRanking { get; set; }
[Display(Name = "Display contribution value")]
public bool DisplayPerksValue { get; set; }
[Display(Name = "Sounds to play when a payment is made. One sound per line")] [Display(Name = "Sounds to play when a payment is made. One sound per line")]
public string Sounds { get; set; } public string Sounds { get; set; }

View File

@@ -73,9 +73,11 @@ namespace BTCPayServer.Models.AppViewModels
public bool Ended => EndDate.HasValue && DateTime.UtcNow > EndDate; public bool Ended => EndDate.HasValue && DateTime.UtcNow > EndDate;
public bool DisplayPerksRanking { get; set; } public bool DisplayPerksRanking { get; set; }
public bool DisplayPerksValue { get; set; }
public bool Enabled { get; set; } public bool Enabled { get; set; }
public string ResetEvery { get; set; } public string ResetEvery { get; set; }
public Dictionary<string, CurrencyData> CurrencyDataPayments { get; set; } public Dictionary<string, CurrencyData> CurrencyDataPayments { get; set; }
public Dictionary<string, decimal> PerkValue { get; set; }
} }
public class ContributeToCrowdfund public class ContributeToCrowdfund

View File

@@ -56,7 +56,8 @@ namespace BTCPayServer.Services.Apps
} }
return null; return null;
} }
private async Task<ViewCrowdfundViewModel> GetInfo(AppData appData, string statusMessage = null)
private async Task<ViewCrowdfundViewModel> GetInfo(AppData appData)
{ {
var settings = appData.GetSettings<CrowdfundSettings>(); var settings = appData.GetSettings<CrowdfundSettings>();
var resetEvery = settings.StartDate.HasValue ? settings.ResetEvery : CrowdfundResetEvery.Never; var resetEvery = settings.StartDate.HasValue ? settings.ResetEvery : CrowdfundResetEvery.Never;
@@ -101,7 +102,16 @@ namespace BTCPayServer.Services.Apps
.Where(entity => !string.IsNullOrEmpty(entity.Metadata.ItemCode)) .Where(entity => !string.IsNullOrEmpty(entity.Metadata.ItemCode))
.GroupBy(entity => entity.Metadata.ItemCode) .GroupBy(entity => entity.Metadata.ItemCode)
.ToDictionary(entities => entities.Key, entities => entities.Count()); .ToDictionary(entities => entities.Key, entities => entities.Count());
Dictionary<string, decimal> perkValue = new Dictionary<string, decimal>();
if (settings.DisplayPerksValue)
{
perkValue = paidInvoices
.Where(entity => !string.IsNullOrEmpty(entity.Metadata.ItemCode))
.GroupBy(entity => entity.Metadata.ItemCode)
.ToDictionary(entities => entities.Key, entities =>
entities.Sum(entity => entity.GetPayments(true).Sum(payment => payment.GetCryptoPaymentData().GetValue())));
}
var perks = Parse(settings.PerksTemplate, settings.TargetCurrency); var perks = Parse(settings.PerksTemplate, settings.TargetCurrency);
if (settings.SortPerksByPopularity) if (settings.SortPerksByPopularity)
{ {
@@ -114,7 +124,8 @@ namespace BTCPayServer.Services.Apps
newPerksOrder.AddRange(remainingPerks); newPerksOrder.AddRange(remainingPerks);
perks = newPerksOrder.ToArray(); perks = newPerksOrder.ToArray();
} }
return new ViewCrowdfundViewModel()
return new ViewCrowdfundViewModel
{ {
Title = settings.Title, Title = settings.Title,
Tagline = settings.Tagline, Tagline = settings.Tagline,
@@ -139,6 +150,7 @@ namespace BTCPayServer.Services.Apps
ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery), ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery),
DisplayPerksRanking = settings.DisplayPerksRanking, DisplayPerksRanking = settings.DisplayPerksRanking,
PerkCount = perkCount, PerkCount = perkCount,
PerkValue = perkValue,
NeverReset = settings.ResetEvery == CrowdfundResetEvery.Never, NeverReset = settings.ResetEvery == CrowdfundResetEvery.Never,
Sounds = settings.Sounds, Sounds = settings.Sounds,
AnimationColors = settings.AnimationColors, AnimationColors = settings.AnimationColors,
@@ -148,7 +160,7 @@ namespace BTCPayServer.Services.Apps
.Select(id => _Currencies.GetCurrencyData(id.CryptoCode, true)) .Select(id => _Currencies.GetCurrencyData(id.CryptoCode, true))
.DistinctBy(data => data.Code) .DistinctBy(data => data.Code)
.ToDictionary(data => data.Code, data => data), .ToDictionary(data => data.Code, data => data),
Info = new ViewCrowdfundViewModel.CrowdfundInfo() Info = new CrowdfundInfo
{ {
TotalContributors = paidInvoices.Length, TotalContributors = paidInvoices.Length,
ProgressPercentage = (currentPayments.TotalCurrency / settings.TargetAmount) * 100, ProgressPercentage = (currentPayments.TotalCurrency / settings.TargetAmount) * 100,
@@ -176,7 +188,7 @@ namespace BTCPayServer.Services.Apps
{ {
StoreId = new[] { appData.StoreData.Id }, StoreId = new[] { appData.StoreData.Id },
OrderId = appData.TagAllInvoices ? null : new[] { GetCrowdfundOrderId(appData.Id) }, OrderId = appData.TagAllInvoices ? null : new[] { GetCrowdfundOrderId(appData.Id) },
Status = new string[]{ Status = new[]{
InvoiceState.ToString(InvoiceStatusLegacy.New), InvoiceState.ToString(InvoiceStatusLegacy.New),
InvoiceState.ToString(InvoiceStatusLegacy.Paid), InvoiceState.ToString(InvoiceStatusLegacy.Paid),
InvoiceState.ToString(InvoiceStatusLegacy.Confirmed), InvoiceState.ToString(InvoiceStatusLegacy.Confirmed),
@@ -206,7 +218,7 @@ namespace BTCPayServer.Services.Apps
using (var ctx = _ContextFactory.CreateContext()) using (var ctx = _ContextFactory.CreateContext())
{ {
ctx.Apps.Add(appData); ctx.Apps.Add(appData);
ctx.Entry<AppData>(appData).State = EntityState.Deleted; ctx.Entry(appData).State = EntityState.Deleted;
return await ctx.SaveChangesAsync() == 1; return await ctx.SaveChangesAsync() == 1;
} }
} }
@@ -217,7 +229,7 @@ namespace BTCPayServer.Services.Apps
{ {
return await ctx.UserStore return await ctx.UserStore
.Where(us => .Where(us =>
((allowNoUser && string.IsNullOrEmpty(userId)) || us.ApplicationUserId == userId) && (allowNoUser && string.IsNullOrEmpty(userId) || us.ApplicationUserId == userId) &&
(storeId == null || us.StoreDataId == storeId)) (storeId == null || us.StoreDataId == storeId))
.Join(ctx.Apps, us => us.StoreDataId, app => app.StoreDataId, .Join(ctx.Apps, us => us.StoreDataId, app => app.StoreDataId,
(us, app) => (us, app) =>
@@ -505,8 +517,8 @@ namespace BTCPayServer.Services.Apps
!posDataObj.TryGetValue("cart", out var cartObject)) !posDataObj.TryGetValue("cart", out var cartObject))
return false; return false;
cartItems = cartObject.Select(token => (JObject)token) cartItems = cartObject.Select(token => (JObject)token)
.ToDictionary(o => o.GetValue("id", StringComparison.InvariantCulture).ToString(), .ToDictionary(o => o.GetValue("id", StringComparison.InvariantCulture)?.ToString(),
o => int.Parse(o.GetValue("count", StringComparison.InvariantCulture).ToString(), CultureInfo.InvariantCulture)); o => int.Parse(o.GetValue("count", StringComparison.InvariantCulture)?.ToString() ?? string.Empty, CultureInfo.InvariantCulture));
return true; return true;
} }
} }

View File

@@ -6,7 +6,7 @@ namespace BTCPayServer.Services.Apps
{ {
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; } public string Description { get; set; }
public bool Enabled { get; set; } = false; public bool Enabled { get; set; }
public DateTime? StartDate { get; set; } public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }
@@ -30,13 +30,14 @@ namespace BTCPayServer.Services.Apps
[Obsolete("Use AppData.TagAllInvoices instead")] [Obsolete("Use AppData.TagAllInvoices instead")]
public bool UseAllStoreInvoices { get; set; } public bool UseAllStoreInvoices { get; set; }
public bool DisplayPerksRanking { get; set; } public bool DisplayPerksRanking { get; set; }
public bool DisplayPerksValue { get; set; }
public bool SortPerksByPopularity { get; set; } public bool SortPerksByPopularity { get; set; }
public string[] AnimationColors { get; set; } = new string[] public string[] AnimationColors { get; set; } =
{ {
"#FF6138", "#FFBE53", "#2980B9", "#282741" "#FF6138", "#FFBE53", "#2980B9", "#282741"
}; };
public string[] Sounds { get; set; } = new string[] public string[] Sounds { get; set; } =
{ {
"//github.com/ClaudiuHKS/AdvancedQuakeSounds/raw/master/sound/QuakeSounds/dominating.wav", "//github.com/ClaudiuHKS/AdvancedQuakeSounds/raw/master/sound/QuakeSounds/dominating.wav",
"//github.com/ClaudiuHKS/AdvancedQuakeSounds/raw/master/sound/QuakeSounds/doublekill.wav", "//github.com/ClaudiuHKS/AdvancedQuakeSounds/raw/master/sound/QuakeSounds/doublekill.wav",

View File

@@ -123,6 +123,11 @@
<label asp-for="DisplayPerksRanking" class="form-check-label"></label> <label asp-for="DisplayPerksRanking" class="form-check-label"></label>
<span asp-validation-for="DisplayPerksRanking" class="text-danger"></span> <span asp-validation-for="DisplayPerksRanking" class="text-danger"></span>
</div> </div>
<div class="form-check mb-3">
<input asp-for="DisplayPerksValue" type="checkbox" class="form-check-input" />
<label asp-for="DisplayPerksValue" class="form-check-label"></label>
<span asp-validation-for="DisplayPerksValue" class="text-danger"></span>
</div>
<div class="form-check mb-3"> <div class="form-check mb-3">
<input asp-for="EnforceTargetAmount" type="checkbox" class="form-check-input" /> <input asp-for="EnforceTargetAmount" type="checkbox" class="form-check-input" />
<label asp-for="EnforceTargetAmount" class="form-check-label"></label> <label asp-for="EnforceTargetAmount" class="form-check-label"></label>

View File

@@ -1,12 +1,16 @@
@model BTCPayServer.Models.AppViewModels.ContributeToCrowdfund @model BTCPayServer.Models.AppViewModels.ContributeToCrowdfund
@{ var vm = Model.ViewCrowdfundViewModel; }
<form method="post"> <form method="post">
@foreach (var item in Model.ViewCrowdfundViewModel.Perks) @foreach (var item in vm.Perks)
{ {
var hasCount = vm.PerkCount.ContainsKey(item.Id);
var hasValue = vm.PerkValue.ContainsKey(item.Id);
<div class="card mb-4 perk expanded" id="@item.Id"> <div class="card mb-4 perk expanded" id="@item.Id">
@if (Model.ViewCrowdfundViewModel.DisplayPerksRanking && Model.ViewCrowdfundViewModel.PerkCount.ContainsKey(item.Id)) @if (vm.DisplayPerksRanking && hasCount)
{ {
<span class="btn btn-sm rounded-circle px-0 btn-primary perk-badge">#@(Array.IndexOf(Model.ViewCrowdfundViewModel.Perks, item) + 1)</span> <span class="btn btn-sm rounded-circle px-0 btn-primary perk-badge">#@(Array.IndexOf(vm.Perks, item) + 1)</span>
} }
@if (!string.IsNullOrEmpty(item.Image)) @if (!string.IsNullOrEmpty(item.Image))
{ {
@@ -15,7 +19,7 @@
<div class="card-body"> <div class="card-body">
<div class="card-title d-flex align-items-center justify-content-between mb-1"> <div class="card-title d-flex align-items-center justify-content-between mb-1">
<label class="h5 d-flex align-items-center"> <label class="h5 d-flex align-items-center">
@if (Model.ViewCrowdfundViewModel.Started && !Model.ViewCrowdfundViewModel.Ended && (item.Price.Value > 0 || item.Custom)) @if (vm.Started && !vm.Ended && (item.Price.Value > 0 || item.Custom))
{ {
<input type="radio" asp-for="ChoiceKey" value="@item.Id" class="form-check-input mt-0 me-2"/> <input type="radio" asp-for="ChoiceKey" value="@item.Id" class="form-check-input mt-0 me-2"/>
} }
@@ -25,7 +29,7 @@
@if (item.Price.Value > 0) @if (item.Price.Value > 0)
{ {
<span>@item.Price.Value</span> <span>@item.Price.Value</span>
<span>@Model.ViewCrowdfundViewModel.TargetCurrency</span> <span>@vm.TargetCurrency</span>
if (item.Custom) if (item.Custom)
{ {
@@ -40,13 +44,12 @@
</div> </div>
<p class="card-text overflow-hidden">@Safe.Raw(item.Description)</p> <p class="card-text overflow-hidden">@Safe.Raw(item.Description)</p>
</div> </div>
@if (Model.ViewCrowdfundViewModel.PerkCount.ContainsKey(item.Id) || item.Inventory.HasValue) @if (hasCount || hasValue || item.Inventory.HasValue)
{ {
<div class="card-footer d-flex justify-content-between"> <div class="card-footer d-flex flex-wrap justify-content-between">
@switch (item.Inventory) @switch (item.Inventory)
{ {
case null: case null:
<span></span>
break; break;
case int i when i <= 0: case int i when i <= 0:
<span>Sold out</span> <span>Sold out</span>
@@ -55,19 +58,20 @@
<span>@item.Inventory left</span> <span>@item.Inventory left</span>
break; break;
} }
@if (Model.ViewCrowdfundViewModel.PerkCount.ContainsKey(item.Id)) @if (hasCount)
{ {
<span>@Model.ViewCrowdfundViewModel.PerkCount[item.Id] Contributors</span> var count = vm.PerkCount[item.Id];
<span>@count Contributor@(count == 1 ? "" : "s")</span>
} }
else @if (hasValue)
{ {
<span></span> <span>@vm.PerkValue[item.Id] @vm.TargetCurrency total</span>
} }
</div> </div>
} }
</div> </div>
} }
@if (Model.ViewCrowdfundViewModel.Started && !Model.ViewCrowdfundViewModel.Ended) @if (vm.Started && !vm.Ended)
{ {
<div class="form-group"> <div class="form-group">
<label asp-for="Email" class="form-label"></label> <label asp-for="Email" class="form-label"></label>
@@ -78,7 +82,7 @@
<label asp-for="Amount" class="form-label"></label> <label asp-for="Amount" class="form-label"></label>
<div class="input-group mb-3"> <div class="input-group mb-3">
<input asp-for="Amount" type="number" step="any" class="form-control"/> <input asp-for="Amount" type="number" step="any" class="form-control"/>
<span class="input-group-text">@Model.ViewCrowdfundViewModel.TargetCurrency.ToUpperInvariant()</span> <span class="input-group-text">@vm.TargetCurrency.ToUpperInvariant()</span>
</div> </div>
<span asp-validation-for="Amount" class="text-danger"></span> <span asp-validation-for="Amount" class="text-danger"></span>
</div> </div>

View File

@@ -218,6 +218,7 @@
<div class="col-md-4 col-sm-12" id="crowdfund-body-contribution-container"> <div class="col-md-4 col-sm-12" id="crowdfund-body-contribution-container">
<contribute :target-currency="srvModel.targetCurrency" <contribute :target-currency="srvModel.targetCurrency"
:display-perks-ranking="srvModel.displayPerksRanking" :display-perks-ranking="srvModel.displayPerksRanking"
:perks-value="srvModel.perksValue"
:active="active" :active="active"
:loading="loading" :loading="loading"
:in-modal="false" :in-modal="false"
@@ -242,6 +243,7 @@
<contribute :target-currency="srvModel.targetCurrency" <contribute :target-currency="srvModel.targetCurrency"
:loading="loading" :loading="loading"
:display-perks-ranking="srvModel.displayPerksRanking" :display-perks-ranking="srvModel.displayPerksRanking"
:perks-value="srvModel.perksValue"
:active="active" :active="active"
:in-modal="false" :in-modal="false"
:perks="perks"> :perks="perks">
@@ -301,6 +303,7 @@
:target-currency="targetCurrency" :target-currency="targetCurrency"
:active="active" :active="active"
:display-perks-ranking="displayPerksRanking" :display-perks-ranking="displayPerksRanking"
:perks-value="perksValue"
:index="index" :index="index"
:loading="loading" :loading="loading"
:in-modal="inModal"> :in-modal="inModal">
@@ -365,7 +368,8 @@
<div class="card-footer d-flex justify-content-between" v-if="perk.sold || perk.inventory != null"> <div class="card-footer d-flex justify-content-between" v-if="perk.sold || perk.inventory != null">
<span v-if="perk.inventory != null && perk.inventory > 0" class="text-center text-muted">{{perk.inventory}} left</span> <span v-if="perk.inventory != null && perk.inventory > 0" class="text-center text-muted">{{perk.inventory}} left</span>
<span v-if="perk.inventory != null && perk.inventory <= 0" class="text-center text-muted">Sold out</span> <span v-if="perk.inventory != null && perk.inventory <= 0" class="text-center text-muted">Sold out</span>
<span v-if="perk.sold">{{perk.sold}} Contributor{{perk.sold > 1 ? "s": ""}}</span> <span v-if="perk.sold">{{perk.sold}} Contributor{{perk.sold === 1 ? "": "s"}}</span>
<span v-if="perk.value">{{perk.value}} {{targetCurrency}} total</span>
</div> </div>
</form> </form>
</div> </div>
@@ -379,6 +383,7 @@
:loading="loading" :loading="loading"
:in-modal="inModal" :in-modal="inModal"
:display-perks-ranking="displayPerksRanking" :display-perks-ranking="displayPerksRanking"
:perks-value="perksValue"
:target-currency="targetCurrency" :target-currency="targetCurrency"
:active="active"> :active="active">
</perks> </perks>

View File

@@ -18,17 +18,17 @@ addLoadEvent(function (ev) {
Vue.use(Toasted); Vue.use(Toasted);
Vue.component('contribute', { Vue.component('contribute', {
props: ["targetCurrency", "active", "perks", "inModal", "displayPerksRanking", "loading"], props: ["targetCurrency", "active", "perks", "inModal", "displayPerksRanking", "perksValue", "loading"],
template: "#contribute-template" template: "#contribute-template"
}); });
Vue.component('perks', { Vue.component('perks', {
props: ["perks", "targetCurrency", "active", "inModal","displayPerksRanking", "loading"], props: ["perks", "targetCurrency", "active", "inModal","displayPerksRanking", "perksValue", "loading"],
template: "#perks-template" template: "#perks-template"
}); });
Vue.component('perk', { Vue.component('perk', {
props: ["perk", "targetCurrency", "active", "inModal", "displayPerksRanking", "index", "loading"], props: ["perk", "targetCurrency", "active", "inModal", "displayPerksRanking", "perksValue", "index", "loading"],
template: "#perk-template", template: "#perk-template",
data: function () { data: function () {
return { return {
@@ -159,6 +159,9 @@ addLoadEvent(function (ev) {
if(this.srvModel.perkCount.hasOwnProperty(currentPerk.id)){ if(this.srvModel.perkCount.hasOwnProperty(currentPerk.id)){
currentPerk.sold = this.srvModel.perkCount[currentPerk.id]; currentPerk.sold = this.srvModel.perkCount[currentPerk.id];
} }
if(this.srvModel.perkValue.hasOwnProperty(currentPerk.id)){
currentPerk.value = this.srvModel.perkValue[currentPerk.id];
}
result.push(currentPerk); result.push(currentPerk);
} }
return result; return result;