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.

27.0 KB
Raw Blame
using Grand.Business.Core.Commands.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Pdf;
using Grand.Domain.Orders;
using Grand.Domain.Permissions;
using Grand.Infrastructure;
using Grand.Mediator;
using Grand.Web.AdminShared.Extensions;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Orders;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
using Grand.Business.Core.Queries.Checkout.Orders;

namespace Grand.Web.AdminShared.Controllers;

public abstract class BaseOrderManagementController(
    IOrderViewModelService orderViewModelService,
    IOrderService orderService,
    IOrderStatusService orderStatusService,
    ITranslationService translationService,
    IContextAccessor contextAccessor,
    IPdfService pdfService,
    IMediator mediator,
    IAdminDataScope<Order> scope)
    : BaseOrderController(orderViewModelService, orderService, translationService, contextAccessor,
        pdfService, scope)
{
    // Exposed for Grand.Web.Admin's concrete OrderController subclass (Task 17), which calls
    // Mediator.Send(...) directly - primary-constructor parameters aren't visible to derived
    // classes by name in C#.
    protected IMediator Mediator => mediator;

    #region Payments and other order workflow

    // POST: the order page submits its form here (the button is an <input type="submit">), and a
    // state-changing GET would let any link or prefetch cancel an order.
    [PermissionAuthorizeAction(PermissionActionName.Cancel)]
    [HttpPost]
    public async Task<IActionResult> CancelOrder(string id)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        if (!await mediator.Send(new CanCancelOrderQuery { Order = order }))
        {
            Error(TranslationService.GetResource("Admin.Orders.CannotCancel"));
            return RedirectToAction("Edit", "Order", new { id });
        }

        try
        {
            await mediator.Send(new CancelOrderCommand { Order = order, NotifyCustomer = true });
            Success("Successfully canceled order");
            return RedirectToAction("Edit", "Order", new { id });
        }
        catch (Exception exc)
        {
            Error(exc);
            return RedirectToAction("Edit", "Order", new { id });
        }
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> SaveOrderTags(OrderModel orderModel)
    {
        var (order, denied) = await LoadAuthorizedOrder(orderModel.Id);
        if (denied != null) return denied;

        try
        {
            await OrderViewModelService.SaveOrderTags(order, orderModel.OrderTags);
            var model = new OrderModel();
            await OrderViewModelService.PrepareOrderDetailsModel(model, order);
            return RedirectToAction("Edit", "Order", new { id = order.Id });
        }
        catch (Exception exception)
        {
            Error(exception, false);
            return RedirectToAction("Edit", "Order", new { id = order.Id });
        }
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> ChangeOrderStatus(string id, OrderModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        try
        {
            var status = await orderStatusService.GetByStatusId(model.OrderStatusId);
            ArgumentNullException.ThrowIfNull(status);

            order.OrderStatusId = model.OrderStatusId;
            await OrderService.UpdateOrder(order);

            await OrderService.InsertOrderNote(new OrderNote {
                Note = $"Order status has been edited. New status: {status.Name}",
                DisplayToCustomer = false,
                OrderId = order.Id
            });
            model = new OrderModel();
            await OrderViewModelService.PrepareOrderDetailsModel(model, order);
            return RedirectToAction("Edit", "Order", new { id });
        }
        catch (Exception exc)
        {
            Error(exc, false);
            return RedirectToAction("Edit", "Order", new { id });
        }
    }

    #endregion

    #region Order totals / shipping / user fields

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> EditOrderTotals(string id, OrderModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        order.OrderSubtotalInclTax = model.OrderSubtotalInclTaxValue;
        order.OrderSubtotalExclTax = model.OrderSubtotalExclTaxValue;
        order.OrderSubTotalDiscountInclTax = model.OrderSubTotalDiscountInclTaxValue;
        order.OrderSubTotalDiscountExclTax = model.OrderSubTotalDiscountExclTaxValue;
        order.OrderShippingInclTax = model.OrderShippingInclTaxValue;
        order.OrderShippingExclTax = model.OrderShippingExclTaxValue;
        order.PaymentMethodAdditionalFeeInclTax = model.PaymentMethodAdditionalFeeInclTaxValue;
        order.PaymentMethodAdditionalFeeExclTax = model.PaymentMethodAdditionalFeeExclTaxValue;
        order.OrderTax = model.TaxValue;
        order.OrderDiscount = model.OrderTotalDiscountValue;
        order.OrderTotal = model.OrderTotalValue;
        order.CurrencyRate = model.CurrencyRate;
        await OrderService.UpdateOrder(order);

        await OrderService.InsertOrderNote(new OrderNote {
            Note = "Order totals have been edited",
            DisplayToCustomer = false,
            OrderId = order.Id
        });

        await OrderViewModelService.PrepareOrderDetailsModel(model, order);
        return RedirectToAction("Edit", "Order", new { id });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> EditShippingMethod(string id, OrderModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        order.ShippingMethod = model.ShippingMethod;
        await OrderService.UpdateOrder(order);

        await OrderService.InsertOrderNote(new OrderNote {
            Note = "Shipping method has been edited",
            DisplayToCustomer = false,
            OrderId = order.Id
        });
        await OrderViewModelService.PrepareOrderDetailsModel(model, order);

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    [HttpPost]
    public async Task<IActionResult> EditUserFields(string id, OrderModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        order.UserFields = model.UserFields;
        await OrderService.UpdateOrder(order);
        await OrderViewModelService.PrepareOrderDetailsModel(model, order);

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    #endregion

    #region Edit, delete

    [PermissionAuthorizeAction(PermissionActionName.Delete)]
    [HttpPost]
    public async Task<IActionResult> Delete(OrderDeleteModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(model.Id);
        if (denied != null) return denied;

        if (ModelState.IsValid)
        {
            await mediator.Send(new DeleteOrderCommand { Order = order });
            return RedirectToAction("List");
        }

        Error(ModelState);
        return RedirectToAction("Edit", "Order", new { model.Id });
    }

    #endregion

    #region Order items

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> SaveOrderItem(string id, OrderItemsModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        if (order.OrderStatusId == (int)OrderStatusSystem.Cancelled)
        {
            Error("You can't edit position when order is canceled");
            return RedirectToAction("Edit", "Order", new { id });
        }

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        var itemModel = model.Items.FirstOrDefault(x => x.Id == model.OrderItemId)
            ?? throw new ArgumentException("No order item model found with the specified id");

        var (error, message) = await ApplyOrderItemEdit(order, orderItem, itemModel.Quantity,
            itemModel.UnitPriceExclTaxValue);
        if (error)
        {
            Error(message);
            return RedirectToAction("Edit", "Order", new { id });
        }

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    /// <summary>
    ///     The quantity/unit price rules and the amount arithmetic of an order item edit, shared by
    ///     the form post (<see cref="SaveOrderItem" />) and the products grid
    ///     (<see cref="OrderItemUpdate" />). Every amount is still computed here on the server.
    /// </summary>
    private async Task<(bool error, string message)> ApplyOrderItemEdit(Order order, OrderItem orderItem,
        int quantity, double unitPriceExclTax)
    {
        if (quantity == 0 || (orderItem.OpenQty != orderItem.Quantity && orderItem.IsShipEnabled))
            return (true, "You can't change quantity");

        if (orderItem.Quantity == quantity && orderItem.UnitPriceExclTax == unitPriceExclTax)
            return (true, "Nothing has been changed");

        orderItem.Quantity = quantity;
        orderItem.OpenQty = quantity;

        if (orderItem.UnitPriceExclTax != unitPriceExclTax)
        {
            orderItem.UnitPriceExclTax = unitPriceExclTax;
            orderItem.UnitPriceInclTax =
                Math.Round(orderItem.UnitPriceExclTax * orderItem.TaxRate / 100 + orderItem.UnitPriceExclTax, 2);
            orderItem.PriceInclTax = Math.Round(orderItem.UnitPriceInclTax * orderItem.Quantity, 2);
            orderItem.PriceExclTax = Math.Round(orderItem.UnitPriceExclTax * orderItem.Quantity, 2);
            orderItem.DiscountAmountInclTax = 0;
            orderItem.DiscountAmountExclTax = 0;
        }
        else
        {
            orderItem.PriceInclTax = Math.Round(orderItem.UnitPriceInclTax * orderItem.Quantity, 2);
            orderItem.PriceExclTax = Math.Round(orderItem.UnitPriceExclTax * orderItem.Quantity, 2);
            orderItem.DiscountAmountInclTax = 0;
            orderItem.DiscountAmountExclTax = 0;
        }

        await mediator.Send(new UpdateOrderItemCommand { Order = order, OrderItem = orderItem });
        return (false, string.Empty);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> DeleteOrderItem(string id, string orderItemId)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        var result = await mediator.Send(new DeleteOrderItemCommand { Order = order, OrderItem = orderItem });
        if (result.error) Error(result.message);

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> CancelOrderItem(string id, string orderItemId)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        var result = await mediator.Send(new CancelOrderItemCommand { Order = order, OrderItem = orderItem });
        if (result.error) Error(result.message);
        else Success("The order item was successfully canceled");

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> ResetDownloadCount(string id, string orderItemId)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        orderItem.DownloadCount = 0;
        await OrderService.UpdateOrder(order);
        var model = new OrderModel();
        await OrderViewModelService.PrepareOrderDetailsModel(model, order);

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> ActivateDownloadItem(string id, string orderItemId)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        orderItem.IsDownloadActivated = !orderItem.IsDownloadActivated;
        await OrderService.UpdateOrder(order);
        var model = new OrderModel();
        await OrderViewModelService.PrepareOrderDetailsModel(model, order);

        await SaveSelectedTabIndex(persistForTheNextRequest: true);
        return RedirectToAction("Edit", "Order", new { id });
    }

    #endregion

    #region Order items grid

    /// <summary>
    ///     Inline edit of one row of the products grid. Enforces exactly the rules
    ///     <see cref="SaveOrderItem" /> enforces; the answer is the grid's
    ///     <see cref="Grand.Web.Common.DataSource.DataSourceResult" /> contract, so a refused edit is
    ///     shown by display_kendoui_grid_error instead of a redirect.
    /// </summary>
    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> OrderItemUpdate(string orderId, OrderModel.OrderItemModel model)
    {
        var (order, denied) = await LoadAuthorizedOrderForGrid(orderId);
        if (denied != null) return denied;

        if (order.OrderStatusId == (int)OrderStatusSystem.Cancelled)
            return GridError("You can't edit position when order is canceled");

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.Id);
        if (orderItem == null) return GridError("No order item found with the specified id");

        var (error, message) = await ApplyOrderItemEdit(order, orderItem, model.Quantity, model.UnitPriceExclTaxValue);
        return error ? GridError(message) : Json(new Grand.Web.Common.DataSource.DataSourceResult());
    }

    /// <summary>Delete command of the products grid; the same command <see cref="DeleteOrderItem" /> sends.</summary>
    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> OrderItemDelete(string orderId, OrderModel.OrderItemModel model)
    {
        var (order, denied) = await LoadAuthorizedOrderForGrid(orderId);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.Id);
        if (orderItem == null) return GridError("No order item found with the specified id");

        var result = await mediator.Send(new DeleteOrderItemCommand { Order = order, OrderItem = orderItem });
        return result.error ? GridError(result.message) : Json(new Grand.Web.Common.DataSource.DataSourceResult());
    }

    /// <summary>Cancel command of the products grid; the same command <see cref="CancelOrderItem" /> sends.</summary>
    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> OrderItemCancel(string orderId, string orderItemId)
    {
        var (order, denied) = await LoadAuthorizedOrderForGrid(orderId);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
        if (orderItem == null) return GridError("No order item found with the specified id");

        var result = await mediator.Send(new CancelOrderItemCommand { Order = order, OrderItem = orderItem });
        return result.error ? GridError(result.message) : Json(new Grand.Web.Common.DataSource.DataSourceResult());
    }

    /// <summary>
    ///     <see cref="LoadAuthorizedOrder" /> for the grid endpoints: a denial is an Errors answer the
    ///     grid can show, never a redirect an XHR would follow into an HTML page.
    /// </summary>
    private async Task<(Order order, IActionResult denied)> LoadAuthorizedOrderForGrid(string orderId)
    {
        var order = await OrderService.GetOrderById(orderId);
        if (order == null || !await Scope.HasAccess(order)) return (null, GridError("Access denied"));
        return (order, null);
    }

    private JsonResult GridError(string message) =>
        Json(new Grand.Web.Common.DataSource.DataSourceResult { Errors = message });

    #endregion

    #region License popup

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> UploadLicenseFilePopup(string id, string orderItemId,
        [FromServices] Grand.Business.Core.Interfaces.Catalog.Products.IProductService productService)
    {
        var (order, denied) = await LoadAuthorizedOrder(id);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        var product = await productService.GetProductByIdIncludeArch(orderItem.ProductId);
        if (!product.IsDownload) throw new ArgumentException("Product is not downloadable");

        var model = new OrderModel.UploadLicenseModel {
            LicenseDownloadId = !string.IsNullOrEmpty(orderItem.LicenseDownloadId) ? orderItem.LicenseDownloadId : "",
            OrderId = order.Id,
            OrderItemId = orderItem.Id
        };
        return View(model);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> UploadLicenseFilePopup(OrderModel.UploadLicenseModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(model.OrderId);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        orderItem.LicenseDownloadId = !string.IsNullOrEmpty(model.LicenseDownloadId) ? model.LicenseDownloadId : null;
        await OrderService.UpdateOrder(order);

        model.RefreshPage = true;
        return View(model);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> DeleteLicenseFilePopup(OrderModel.UploadLicenseModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(model.OrderId);
        if (denied != null) return denied;

        var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == model.OrderItemId)
            ?? throw new ArgumentException("No order item found with the specified id");
        orderItem.LicenseDownloadId = null;
        await OrderService.UpdateOrder(order);

        return RedirectToAction("Edit", "Order", new { id = model.OrderId });
    }

    #endregion

    #region Add product to order

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> AddProductToOrder(string orderId)
    {
        var (order, denied) = await LoadAuthorizedOrder(orderId);
        if (denied != null) return denied;

        var model = await OrderViewModelService.PrepareAddOrderProductModel(order);
        return View(model);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> AddProductToOrder(
        Grand.Web.Common.DataSource.DataSourceRequest command, OrderModel.AddOrderProductModel model,
        [FromServices] Grand.Business.Core.Interfaces.Catalog.Products.IProductService productService)
    {
        var categoryIds = new List<string>();
        if (!string.IsNullOrEmpty(model.SearchCategoryId)) categoryIds.Add(model.SearchCategoryId);

        var gridModel = new Grand.Web.Common.DataSource.DataSourceResult();
        var products = (await productService.SearchProducts(categoryIds: categoryIds,
            storeId: Scope.DefaultStoreId,
            brandId: model.SearchBrandId,
            collectionId: model.SearchCollectionId,
            productType: model.SearchProductTypeId > 0 ? (Grand.Domain.Catalog.ProductType?)model.SearchProductTypeId : null,
            keywords: model.SearchProductName,
            pageIndex: command.Page - 1,
            pageSize: command.PageSize,
            showHidden: true)).products;

        gridModel.Data = products.Select(x => new OrderModel.AddOrderProductModel.ProductModel {
            Id = x.Id, Name = x.Name, Sku = x.Sku
        });
        gridModel.Total = products.TotalCount;

        return Json(gridModel);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> AddProductToOrderDetails(string orderId, string productId)
    {
        var (order, denied) = await LoadAuthorizedOrder(orderId);
        if (denied != null) return denied;

        var model = await OrderViewModelService.PrepareAddProductToOrderModel(order, productId);
        return View(model);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> AddProductToOrderDetails(AddProductToOrderModel model)
    {
        var (order, denied) = await LoadAuthorizedOrder(model.OrderId);
        if (denied != null) return denied;

        var warnings = await OrderViewModelService.AddProductToOrderDetails(model);
        if (!warnings.Any()) return RedirectToAction("Edit", "Order", new { id = model.OrderId });

        var result = await OrderViewModelService.PrepareAddProductToOrderModel(order, model.ProductId);
        result.Warnings.AddRange(warnings);
        return View(result);
    }

    #endregion

    #region Addresses

    [PermissionAuthorizeAction(PermissionActionName.Preview)]
    public async Task<IActionResult> AddressEdit(string addressId, string orderId, bool billingAddress)
    {
        var (order, denied) = await LoadAuthorizedOrder(orderId);
        if (denied != null) return denied;

        var address = new Grand.Domain.Common.Address();
        switch (billingAddress)
        {
            case true when order.BillingAddress != null:
                if (order.BillingAddress.Id == addressId) address = order.BillingAddress;
                break;
            case false when order.ShippingAddress != null:
                if (order.ShippingAddress.Id == addressId) address = order.ShippingAddress;
                break;
        }

        if (address == null)
            throw new ArgumentException("No address found with the specified id", nameof(addressId));

        var model = await OrderViewModelService.PrepareOrderAddressModel(order, address);
        model.BillingAddress = billingAddress;
        return View(model);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    [HttpPost]
    public async Task<IActionResult> AddressEdit(OrderAddressModel model,
        [FromServices] Grand.Business.Core.Interfaces.Common.Addresses.IAddressAttributeService addressAttributeService,
        [FromServices] Grand.Business.Core.Interfaces.Common.Addresses.IAddressAttributeParser addressAttributeParser)
    {
        var (order, denied) = await LoadAuthorizedOrder(model.OrderId);
        if (denied != null) return denied;

        var address = new Grand.Domain.Common.Address();
        switch (model.BillingAddress)
        {
            case true when order.BillingAddress != null:
                if (order.BillingAddress.Id == model.Address.Id) address = order.BillingAddress;
                break;
            case false when order.ShippingAddress != null:
                if (order.ShippingAddress.Id == model.Address.Id) address = order.ShippingAddress;
                break;
        }

        if (ModelState.IsValid)
        {
            var customAttributes = await model.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
            await OrderViewModelService.UpdateOrderAddress(order, address, model, customAttributes);
            return RedirectToAction("AddressEdit",
                new { addressId = model.Address.Id, orderId = model.OrderId, model.BillingAddress });
        }

        model = await OrderViewModelService.PrepareOrderAddressModel(order, address);
        return View(model);
    }

    #endregion

    #region Order notes

    [PermissionAuthorizeAction(PermissionActionName.List)]
    [HttpPost]
    public async Task<IActionResult> OrderNotesSelect(string orderId, Grand.Web.Common.DataSource.DataSourceRequest command)
    {
        var order = await OrderService.GetOrderById(orderId)
            ?? throw new ArgumentException("No order found with the specified id");
        // Preserved host divergence: Admin's original throws for both not-found and Sales-Manager
        // denial; Store's original throws only for not-found and soft-denies (empty content) for
        // store-mismatch. Unifying these into one behavior would be a real change for one host -
        // deliberately not done here. See plan's Global Constraints.
        if (!await Scope.HasAccess(order)) return Content("");

        var orderNoteModels = await OrderViewModelService.PrepareOrderNotes(order);
        var gridModel = new Common.DataSource.DataSourceResult {
            Data = orderNoteModels,
            Total = orderNoteModels.Count
        };
        return Json(gridModel);
    }

    [PermissionAuthorizeAction(PermissionActionName.Edit)]
    public async Task<IActionResult> OrderNoteAdd(string orderId, string downloadId, bool displayToCustomer, string message)
    {
        var order = await OrderService.GetOrderById(orderId);
        if (order == null || !await Scope.HasAccess(order)) return Json(new { Result = false });

        await OrderViewModelService.InsertOrderNote(order, downloadId, displayToCustomer, message);
        return Json(new { Result = true });
    }

    [PermissionAuthorizeAction(PermissionActionName.Delete)]
    [HttpPost]
    public async Task<IActionResult> OrderNoteDelete(string id, string orderId)
    {
        var order = await OrderService.GetOrderById(orderId)
            ?? throw new ArgumentException("No order found with the specified id");
        if (!await Scope.HasAccess(order)) return Json(new { Result = false });

        await OrderViewModelService.DeleteOrderNote(order, id);
        return new JsonResult("");
    }

    #endregion
}