Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1008 B
Raw Blame
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.System.ScheduleTasks;
using Grand.Domain.Common;

namespace Grand.Module.ScheduledTasks.BackgroundServices;

public class CancelOrderScheduledTask : IScheduleTask
{
    private readonly IOrderService _orderService;
    private readonly SystemSettings _systemSettings;

    public CancelOrderScheduledTask(
        SystemSettings systemSettings,
        IOrderService orderService)
    {
        _systemSettings = systemSettings;
        _orderService = orderService;
    }

    public async Task Execute()
    {
        if (!_systemSettings.DaysToCancelUnpaidOrder.HasValue)
            return;

        var startCancelDate = CalculateStartCancelDate(_systemSettings.DaysToCancelUnpaidOrder.Value);
        await _orderService.CancelExpiredOrders(startCancelDate);
    }

    private static DateTime CalculateStartCancelDate(int daysToCancelUnpaidOrder) => DateTime.UtcNow.Date.AddDays(-daysToCancelUnpaidOrder);
}