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

View File

@@ -93,6 +93,9 @@ namespace BTCPayServer.Models.AppViewModels
[Display(Name = "Display contribution ranking")]
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")]
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 DisplayPerksRanking { get; set; }
public bool DisplayPerksValue { get; set; }
public bool Enabled { get; set; }
public string ResetEvery { get; set; }
public Dictionary<string, CurrencyData> CurrencyDataPayments { get; set; }
public Dictionary<string, decimal> PerkValue { get; set; }
}
public class ContributeToCrowdfund

View File

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

View File

@@ -6,7 +6,7 @@ namespace BTCPayServer.Services.Apps
{
public string Title { 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? EndDate { get; set; }
@@ -30,13 +30,14 @@ namespace BTCPayServer.Services.Apps
[Obsolete("Use AppData.TagAllInvoices instead")]
public bool UseAllStoreInvoices { get; set; }
public bool DisplayPerksRanking { get; set; }
public bool DisplayPerksValue { get; set; }
public bool SortPerksByPopularity { get; set; }
public string[] AnimationColors { get; set; } = new string[]
public string[] AnimationColors { get; set; } =
{
"#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/doublekill.wav",

View File

@@ -123,6 +123,11 @@
<label asp-for="DisplayPerksRanking" class="form-check-label"></label>
<span asp-validation-for="DisplayPerksRanking" class="text-danger"></span>
</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">
<input asp-for="EnforceTargetAmount" type="checkbox" class="form-check-input" />
<label asp-for="EnforceTargetAmount" class="form-check-label"></label>

View File

@@ -1,12 +1,16 @@
@model BTCPayServer.Models.AppViewModels.ContributeToCrowdfund
@{ var vm = Model.ViewCrowdfundViewModel; }
<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">
@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))
{
@@ -15,7 +19,7 @@
<div class="card-body">
<div class="card-title d-flex align-items-center justify-content-between mb-1">
<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"/>
}
@@ -25,7 +29,7 @@
@if (item.Price.Value > 0)
{
<span>@item.Price.Value</span>
<span>@Model.ViewCrowdfundViewModel.TargetCurrency</span>
<span>@vm.TargetCurrency</span>
if (item.Custom)
{
@@ -40,13 +44,12 @@
</div>
<p class="card-text overflow-hidden">@Safe.Raw(item.Description)</p>
</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)
{
case null:
<span></span>
break;
case int i when i <= 0:
<span>Sold out</span>
@@ -55,19 +58,20 @@
<span>@item.Inventory left</span>
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>
}
@if (Model.ViewCrowdfundViewModel.Started && !Model.ViewCrowdfundViewModel.Ended)
@if (vm.Started && !vm.Ended)
{
<div class="form-group">
<label asp-for="Email" class="form-label"></label>
@@ -78,7 +82,7 @@
<label asp-for="Amount" class="form-label"></label>
<div class="input-group mb-3">
<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>
<span asp-validation-for="Amount" class="text-danger"></span>
</div>

View File

@@ -218,6 +218,7 @@
<div class="col-md-4 col-sm-12" id="crowdfund-body-contribution-container">
<contribute :target-currency="srvModel.targetCurrency"
:display-perks-ranking="srvModel.displayPerksRanking"
:perks-value="srvModel.perksValue"
:active="active"
:loading="loading"
:in-modal="false"
@@ -242,6 +243,7 @@
<contribute :target-currency="srvModel.targetCurrency"
:loading="loading"
:display-perks-ranking="srvModel.displayPerksRanking"
:perks-value="srvModel.perksValue"
:active="active"
:in-modal="false"
:perks="perks">
@@ -301,6 +303,7 @@
:target-currency="targetCurrency"
:active="active"
:display-perks-ranking="displayPerksRanking"
:perks-value="perksValue"
:index="index"
:loading="loading"
:in-modal="inModal">
@@ -365,7 +368,8 @@
<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">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>
</form>
</div>
@@ -379,6 +383,7 @@
:loading="loading"
:in-modal="inModal"
:display-perks-ranking="displayPerksRanking"
:perks-value="perksValue"
:target-currency="targetCurrency"
:active="active">
</perks>

View File

@@ -18,17 +18,17 @@ addLoadEvent(function (ev) {
Vue.use(Toasted);
Vue.component('contribute', {
props: ["targetCurrency", "active", "perks", "inModal", "displayPerksRanking", "loading"],
props: ["targetCurrency", "active", "perks", "inModal", "displayPerksRanking", "perksValue", "loading"],
template: "#contribute-template"
});
Vue.component('perks', {
props: ["perks", "targetCurrency", "active", "inModal","displayPerksRanking", "loading"],
props: ["perks", "targetCurrency", "active", "inModal","displayPerksRanking", "perksValue", "loading"],
template: "#perks-template"
});
Vue.component('perk', {
props: ["perk", "targetCurrency", "active", "inModal", "displayPerksRanking", "index", "loading"],
props: ["perk", "targetCurrency", "active", "inModal", "displayPerksRanking", "perksValue", "index", "loading"],
template: "#perk-template",
data: function () {
return {
@@ -159,6 +159,9 @@ addLoadEvent(function (ev) {
if(this.srvModel.perkCount.hasOwnProperty(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);
}
return result;