Implement discount tiers for pricing settings and enhance related functionalities
CI / Backend (build + test) (push) Successful in 1m20s
CI / Frontend (lint + typecheck + build) (push) Successful in 32s

- Introduced a new `DiscountTierDto` to represent volume discount tiers, allowing roles with a config quota at or above specified thresholds to receive discounts on pricing.
- Updated the `PricingSettingsDto` to include a list of discount tiers, enhancing the pricing model to support more flexible pricing strategies.
- Modified the `GetPricingSettingsQueryHandler` and `UpdatePricingSettingsCommandHandler` to handle discount tiers, ensuring they are correctly retrieved and updated in the database.
- Enhanced validation in `UpdatePricingSettingsCommandValidator` to enforce uniqueness and progressive discount tiers, preventing invalid configurations.
- Updated frontend components to support the new discount tier functionality, including forms for adding and managing discount tiers in the admin interface.
- Revised API documentation to reflect the new discount tier features and their usage in pricing settings.
This commit is contained in:
Leonid Pershin
2026-07-19 15:51:01 +03:00
parent 6a2d2d2318
commit 0dcaf1203f
27 changed files with 1645 additions and 43 deletions
@@ -0,0 +1,61 @@
using PnvPanel.Application.Admin.Pricing;
using PnvPanel.Application.Common.Interfaces;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Pricing;
public class UpdatePricingSettingsCommandValidatorTests
{
private readonly UpdatePricingSettingsCommandValidator _validator = new();
private static UpdatePricingSettingsCommand Command(params DiscountTierDto[] tiers) =>
new(500, 450, 400, tiers);
[Fact]
public void Validate_WithEmptyTiers_IsValid()
{
var result = _validator.Validate(Command());
Assert.True(result.IsValid);
}
[Fact]
public void Validate_WithProgressiveTiers_IsValid()
{
var result = _validator.Validate(Command(new DiscountTierDto(3, 5), new DiscountTierDto(6, 10)));
Assert.True(result.IsValid);
}
[Fact]
public void Validate_WithDuplicateThreshold_IsInvalid()
{
var result = _validator.Validate(Command(new DiscountTierDto(3, 5), new DiscountTierDto(3, 10)));
Assert.False(result.IsValid);
}
[Fact]
public void Validate_WhenHigherThresholdHasSmallerDiscount_IsInvalid()
{
var result = _validator.Validate(Command(new DiscountTierDto(3, 10), new DiscountTierDto(6, 5)));
Assert.False(result.IsValid);
}
[Fact]
public void Validate_WithDiscountPercentOutOfRange_IsInvalid()
{
var result = _validator.Validate(Command(new DiscountTierDto(3, 100)));
Assert.False(result.IsValid);
}
[Fact]
public void Validate_WithNonPositiveMinConfigs_IsInvalid()
{
var result = _validator.Validate(Command(new DiscountTierDto(0, 5)));
Assert.False(result.IsValid);
}
}
@@ -65,6 +65,38 @@ public class CreatePaymentRequestCommandHandlerTests
Assert.Equal(PaymentRequestStatus.AwaitingPayment, result.Value.Status);
}
[Fact]
public async Task Handle_WithApplicableDiscountTier_AppliesDiscountToAmount()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var pricing = PricingSettings.CreateDefault();
pricing.Update(500, 450, 400);
dbContext.PricingSettings.Add(pricing);
dbContext.PricingDiscountTiers.Add(PricingDiscountTier.Create(pricing.Id, minConfigs: 3, discountPercent: 10));
dbContext.PricingDiscountTiers.Add(PricingDiscountTier.Create(pricing.Id, minConfigs: 6, discountPercent: 20));
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, maxConfigs: 5));
var handler = new CreatePaymentRequestCommandHandler(
dbContext,
_identityService,
FakeCurrentUser.Authenticated(userId)
);
var result = await handler.Handle(
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
CancellationToken.None
);
Assert.True(result.IsSuccess);
// 500 * 5 * 3 = 7500, скидка 10% (порог 6 не достигнут при 5 конфигах) → 6750.
Assert.Equal(6750, result.Value.AmountSnapshot);
}
[Fact]
public async Task Handle_WhenBillingNotEnabled_ReturnsNotEnabled()
{
@@ -0,0 +1,64 @@
using PnvPanel.Domain.Pricing;
using Xunit;
namespace PnvPanel.Domain.Tests.Pricing;
public class PricingDiscountTests
{
private static PricingDiscountTier Tier(int minConfigs, int percent) =>
PricingDiscountTier.Create(Guid.NewGuid(), minConfigs, percent);
[Fact]
public void ResolvePercent_WhenNoTiers_ReturnsZero()
{
var percent = PricingDiscount.ResolvePercent([], maxConfigs: 10);
Assert.Equal(0, percent);
}
[Fact]
public void ResolvePercent_WhenBelowAllThresholds_ReturnsZero()
{
var tiers = new[] { Tier(3, 5), Tier(6, 10) };
var percent = PricingDiscount.ResolvePercent(tiers, maxConfigs: 2);
Assert.Equal(0, percent);
}
[Fact]
public void ResolvePercent_PicksHighestApplicableTier_NotCumulative()
{
var tiers = new[] { Tier(3, 5), Tier(6, 10), Tier(12, 15) };
var percent = PricingDiscount.ResolvePercent(tiers, maxConfigs: 8);
Assert.Equal(10, percent);
}
[Fact]
public void ResolvePercent_ExactlyOnThreshold_Applies()
{
var tiers = new[] { Tier(3, 5) };
var percent = PricingDiscount.ResolvePercent(tiers, maxConfigs: 3);
Assert.Equal(5, percent);
}
[Fact]
public void Apply_WithZeroPercent_ReturnsAmountUnchanged()
{
var amount = PricingDiscount.Apply(1000, 0);
Assert.Equal(1000, amount);
}
[Fact]
public void Apply_WithPercent_RoundsToNearestInteger()
{
var amount = PricingDiscount.Apply(999, 10);
Assert.Equal(899, amount);
}
}