Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ public virtual Task<RecurringPayment> GetRecurringPaymentByInitialOrderId(string
return repository.GetOneAsync(x => x.InitialOrderId == initialOrderId);
}

public virtual async Task<IPagedList<RecurringPayment>> GetCustomerRecurringPayments(string customerId,
string storeId, int pageIndex = 0, int pageSize = int.MaxValue)
{
if (string.IsNullOrEmpty(customerId) || string.IsNullOrEmpty(storeId))
return new PagedList<RecurringPayment>([], pageIndex, pageSize);

var query = repository.Table
.Where(x => x.CustomerId == customerId && x.StoreId == storeId)
.OrderByDescending(x => x.CreatedOnUtc);

return await repository.PagedAsync(query, pageIndex, pageSize);
}

public virtual async Task<bool> HasCustomerRecurringPayments(string customerId, string storeId)
{
if (string.IsNullOrEmpty(customerId) || string.IsNullOrEmpty(storeId))
return false;

return await repository.AnyAsync(repository.Table
.Where(x => x.CustomerId == customerId && x.StoreId == storeId));
}

public virtual async Task<bool> HasRecurringProducts(IEnumerable<ShoppingCartItem> cart)
{
ArgumentNullException.ThrowIfNull(cart);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Grand.Domain.Orders;

namespace Grand.Business.Core.Extensions;

public static class RecurringPaymentExtensions
{
/// <summary>
/// Whether a recurring payment in this status has ended: a Cancelled or Completed one charges nothing more and
/// cannot be cancelled
/// </summary>
public static bool HasEnded(this RecurringPaymentStatus status)
{
return status is RecurringPaymentStatus.Cancelled or RecurringPaymentStatus.Completed;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Utilities.Checkout;
using Grand.Domain;
using Grand.Domain.Orders;

namespace Grand.Business.Core.Interfaces.Checkout.Orders;
Expand Down Expand Up @@ -52,6 +53,18 @@ Task<CancelRecurringPaymentResult> Cancel(string recurringPaymentId, string canc
/// </summary>
Task<RecurringPayment> GetRecurringPaymentByInitialOrderId(string initialOrderId);

/// <summary>
/// Gets a customer's recurring payments in a store, newest first; nothing for an empty customer or store
/// </summary>
Task<IPagedList<RecurringPayment>> GetCustomerRecurringPayments(string customerId, string storeId,
int pageIndex = 0, int pageSize = int.MaxValue);

/// <summary>
/// Gets a value indicating whether a customer has a recurring payment in a store; false for an empty customer
/// or store
/// </summary>
Task<bool> HasCustomerRecurringPayments(string customerId, string storeId);

/// <summary>
/// Gets a value indicating whether the cart holds a product billed in cycles
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Grand.Domain.Catalog;

namespace Grand.Business.Core.Utilities.Checkout;

/// <summary>
/// The words for a recurring payment's cycle: "Every month" for one, "Every 3 months" for more; the enum
/// resources are plural, so "Every {0} {1}" reads "Every 1 Months"
/// </summary>
public static class RecurringCycleText
{
/// <summary>
/// Gets the resource key of the cycle; the resource takes the cycle length as {0}
/// </summary>
public static string ResourceKey(int cycleLength, RecurringCyclePeriod period)
{
var (one, many) = period switch {
RecurringCyclePeriod.Weeks => ("Week", "Weeks"),
RecurringCyclePeriod.Months => ("Month", "Months"),
RecurringCyclePeriod.Years => ("Year", "Years"),
_ => ("Day", "Days")
};
return $"RecurringPayment.Cycle.{(cycleLength == 1 ? one : many)}";
}
}
5 changes: 5 additions & 0 deletions src/Core/Grand.Domain/Customers/CustomerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public class CustomerSettings : ISettings
/// </summary>
public bool HideSubAccountsTab { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to hide 'Recurring payments' tab on 'My account' page
/// </summary>
public bool HideRecurringPaymentsTab { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to validate user when downloading products
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ await dbContext.CreateIndex(_serviceProvider.GetRequiredService<IRepository<Paym
//the recurring payment of an order, found by the order the customer placed; unique, so an order never has two
await dbContext.CreateIndex(_serviceProvider.GetRequiredService<IRepository<RecurringPayment>>(),
OrderBuilder<RecurringPayment>.Create().Ascending(x => x.InitialOrderId), "InitialOrderId", true);
//the My account tab lists a customer's recurring payments in a store
await dbContext.CreateIndex(_serviceProvider.GetRequiredService<IRepository<RecurringPayment>>(),
OrderBuilder<RecurringPayment>.Create().Ascending(x => x.CustomerId).Ascending(x => x.StoreId)
.Descending(x => x.CreatedOnUtc), "CustomerId_StoreId_CreatedOnUtc");

//queuemail
await dbContext.CreateIndex(_queuedEmailRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Grand.Data;
using Grand.Domain.Orders;
using Grand.Infrastructure.Migrations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Grand.Module.Migration.Migrations._2._5;

public class MigrationRecurringPaymentCustomerIndex : IMigration
{
private const string IndexName = "CustomerId_StoreId_CreatedOnUtc";

public int Priority => 1;
public DbVersion Version => new(2, 5);
public Guid Identity => new("5b0c2f3e-8d4a-4e61-9a7f-2c1e6d8b3f04");
public string Name => "Customer index on Recurring payment 2.5";

public async Task<bool> UpgradeProcess(IServiceProvider serviceProvider)
{
var repository = serviceProvider.GetRequiredService<IRepository<RecurringPayment>>();
var databaseContext = serviceProvider.GetRequiredService<IDatabaseContext>();
var logService = serviceProvider.GetRequiredService<ILogger<MigrationRecurringPaymentCustomerIndex>>();

try
{
//the My account tab lists a customer's recurring payments in a store, newest first
await databaseContext.CreateIndex(repository,
OrderBuilder<RecurringPayment>.Create().Ascending(x => x.CustomerId).Ascending(x => x.StoreId)
.Descending(x => x.CreatedOnUtc), IndexName);
}
catch (Exception ex)
{
logService.LogError(ex, "UpgradeProcess - RecurringPaymentCustomerIndex (2.5)");
return false;
}
Comment on lines +31 to +35

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,48 @@ public async Task ClaimCycle_Completing_PublishesNeitherEvent()
Assert.AreEqual(0, Published<RecurringPaymentCancelledEvent>().Count);
Assert.AreEqual(0, Published<RecurringPaymentPastDueEvent>().Count);
}

[TestMethod]
public async Task GetCustomerRecurringPayments_ReturnsOnlyTheCustomersInTheStore_NewestFirst()
{
await _repository.InsertAsync(new RecurringPayment {
Id = "old", CustomerId = "c1", StoreId = "s1", CreatedOnUtc = new DateTime(2026, 1, 1)
});
await _repository.InsertAsync(new RecurringPayment {
Id = "new", CustomerId = "c1", StoreId = "s1", CreatedOnUtc = new DateTime(2026, 2, 1)
});
await _repository.InsertAsync(new RecurringPayment { Id = "other-customer", CustomerId = "c2", StoreId = "s1" });
await _repository.InsertAsync(new RecurringPayment { Id = "other-store", CustomerId = "c1", StoreId = "s2" });

var result = await _service.GetCustomerRecurringPayments("c1", "s1");

CollectionAssert.AreEqual(new[] { "new", "old" }, result.Select(x => x.Id).ToArray());
}

[TestMethod]
public async Task HasCustomerRecurringPayments_OnlyTheCustomersInTheStore()
{
await _repository.InsertAsync(new RecurringPayment { Id = "rp1", CustomerId = "c1", StoreId = "s1" });

Assert.IsTrue(await _service.HasCustomerRecurringPayments("c1", "s1"));
Assert.IsFalse(await _service.HasCustomerRecurringPayments("c1", "s2"));
Assert.IsFalse(await _service.HasCustomerRecurringPayments("c2", "s1"));
}

[TestMethod]
public async Task HasCustomerRecurringPayments_EmptyCustomerOrStore_False()
{
await _repository.InsertAsync(new RecurringPayment { CustomerId = "", StoreId = "" });

Assert.IsFalse(await _service.HasCustomerRecurringPayments("", ""));
}

[TestMethod]
public async Task GetCustomerRecurringPayments_EmptyCustomerOrStore_ReturnsNothing()
{
await _repository.InsertAsync(new RecurringPayment { CustomerId = "", StoreId = "" });

Assert.AreEqual(0, (await _service.GetCustomerRecurringPayments("", "s1")).Count);
Assert.AreEqual(0, (await _service.GetCustomerRecurringPayments("c1", "")).Count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Grand.Business.Core.Utilities.Checkout;
using Grand.Domain.Catalog;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grand.Business.Checkout.Tests.Utilities;

[TestClass]
public class RecurringCycleTextTests
{
[DataTestMethod]
[DataRow(1, RecurringCyclePeriod.Days, "RecurringPayment.Cycle.Day")]
[DataRow(3, RecurringCyclePeriod.Days, "RecurringPayment.Cycle.Days")]
[DataRow(1, RecurringCyclePeriod.Weeks, "RecurringPayment.Cycle.Week")]
[DataRow(2, RecurringCyclePeriod.Weeks, "RecurringPayment.Cycle.Weeks")]
[DataRow(1, RecurringCyclePeriod.Months, "RecurringPayment.Cycle.Month")]
[DataRow(12, RecurringCyclePeriod.Months, "RecurringPayment.Cycle.Months")]
[DataRow(1, RecurringCyclePeriod.Years, "RecurringPayment.Cycle.Year")]
[DataRow(2, RecurringCyclePeriod.Years, "RecurringPayment.Cycle.Years")]
public void ResourceKey_OneCycleIsSingular_MoreArePlural(int cycleLength, RecurringCyclePeriod period,
string expected)
{
Assert.AreEqual(expected, RecurringCycleText.ResourceKey(cycleLength, period));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
HideNotesTab: false,
HideDocumentsTab: false,
HideSubAccountsTab: false,
HideRecurringPaymentsTab: false,
DownloadableProductsValidateUser: false,
NewsletterEnabled: false,
NewsletterTickedByDefault: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
HideCoursesTab: false,
TwoFactorAuthenticationEnabled: false,
HideSubAccountsTab: false,
HideRecurringPaymentsTab: false,
HideDocumentsTab: false
}
Loading
Loading