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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
feat(arch001): add BaseShipmentController PDF export + bulk-action region
KrzysztofPajak2cd2dbd2568b
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.csindex e072b658d..16f44c431 100644--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs@@ -4,6 +4,7 @@ using Grand.Business.Core.Interfaces.Checkout.Shipping;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Pdf;+using Grand.Domain.Localization;using Grand.Domain.Orders;using Grand.Domain.Shipping;using Grand.Infrastructure;@@ -67,6 +68,9 @@ public class BaseShipmentControllerTestsvar translationServiceMock = new Mock<ITranslationService>();translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("resource");var contextAccessorMock = new Mock<IContextAccessor>();+ var workContextMock = new Mock<IWorkContext>();+ workContextMock.Setup(w => w.WorkingLanguage).Returns(new Language { Id = "lang-1" });+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);var pdfServiceMock = new Mock<IPdfService>();var dateTimeServiceMock = new Mock<IDateTimeService>();_mediatorMock = new Mock<IMediator>();@@ -568,4 +572,119 @@ public class BaseShipmentControllerTestsAssert.AreEqual(deliveryDate, shipment.DeliveryDateUtc);_shipmentServiceMock.Verify(s => s.UpdateShipment(shipment), Times.Once);}++ [TestMethod]+ public async Task PdfPackagingSlip_Denied_RedirectsToList()+ {+ _shipmentServiceMock.Setup(s => s.GetShipmentById("s1")).ReturnsAsync((Shipment)null);++ var result = await _controller.PdfPackagingSlip("s1");++ var redirect = result as RedirectToActionResult;+ Assert.IsNotNull(redirect);+ Assert.AreEqual("List", redirect.ActionName);+ _orderServiceMock.Verify(s => s.GetOrderById(It.IsAny<string>()), Times.Never);+ }++ [TestMethod]+ public async Task PdfPackagingSlipAll_NoShipments_ShowsErrorAndRedirects()+ {+ _shipmentViewModelServiceMock+ .Setup(v => v.PrepareShipments(It.IsAny<ShipmentListModel>(), 1, 100))+ .ReturnsAsync((Enumerable.Empty<Shipment>(), 0));++ var result = await _controller.PdfPackagingSlipAll(new ShipmentListModel());++ var redirect = result as RedirectToActionResult;+ Assert.IsNotNull(redirect);+ Assert.AreEqual("List", redirect.ActionName);+ }++ [TestMethod]+ public async Task PdfPackagingSlipAll_ForcesStoreAndVendorIdConditionally()+ {+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");+ _scopeMock.Setup(s => s.DefaultVendorId).Returns("vendor-1");+ _shipmentViewModelServiceMock+ .Setup(v => v.PrepareShipments(It.IsAny<ShipmentListModel>(), 1, 100))+ .ReturnsAsync((Enumerable.Empty<Shipment>(), 0));++ var model = new ShipmentListModel { StoreId = "attacker-store", VendorId = "attacker-vendor" };+ await _controller.PdfPackagingSlipAll(model);++ Assert.AreEqual("store-1", model.StoreId);+ Assert.AreEqual("vendor-1", model.VendorId);+ }++ [TestMethod]+ public async Task PdfPackagingSlipSelected_FiltersToAccessibleShipments()+ {+ var accessibleShipment = new Shipment { Id = "s1" };+ var deniedShipment = new Shipment { Id = "s2" };+ _shipmentServiceMock+ .Setup(s => s.GetShipmentsByIds(new[] { "s1", "s2" }))+ .ReturnsAsync((IList<Shipment>)new List<Shipment> { accessibleShipment, deniedShipment });+ _scopeMock.Setup(s => s.HasAccess(accessibleShipment)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.HasAccess(deniedShipment)).ReturnsAsync(false);++ var result = await _controller.PdfPackagingSlipSelected("s1,s2");++ var fileResult = result as FileContentResult;+ Assert.IsNotNull(fileResult);+ Assert.AreEqual("packagingslips.pdf", fileResult.FileDownloadName);+ _scopeMock.Verify(s => s.HasAccess(accessibleShipment), Times.Once);+ _scopeMock.Verify(s => s.HasAccess(deniedShipment), Times.Once);+ }++ [TestMethod]+ public async Task SetAsShippedSelected_FiltersToAccessibleShipments_IgnoresPerItemExceptions()+ {+ var accessibleShipment1 = new Shipment { Id = "s1" };+ var accessibleShipment2 = new Shipment { Id = "s2" };+ var deniedShipment = new Shipment { Id = "s3" };+ _shipmentServiceMock+ .Setup(s => s.GetShipmentsByIds(new[] { "s1", "s2", "s3" }))+ .ReturnsAsync((IList<Shipment>)new List<Shipment> { accessibleShipment1, accessibleShipment2, deniedShipment });+ _scopeMock.Setup(s => s.HasAccess(accessibleShipment1)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.HasAccess(accessibleShipment2)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.HasAccess(deniedShipment)).ReturnsAsync(false);++ _mediatorMock+ .Setup(m => m.Send(It.Is<ShipCommand>(c => c.Shipment == accessibleShipment1), It.IsAny<CancellationToken>()))+ .ThrowsAsync(new Exception("boom"));+ _mediatorMock+ .Setup(m => m.Send(It.Is<ShipCommand>(c => c.Shipment == accessibleShipment2), It.IsAny<CancellationToken>()))+ .ReturnsAsync(true);++ var result = await _controller.SetAsShippedSelected(new List<string> { "s1", "s2", "s3" });++ var jsonResult = result as JsonResult;+ Assert.IsNotNull(jsonResult);+ _mediatorMock.Verify(m => m.Send(It.Is<ShipCommand>(c => c.Shipment == accessibleShipment1), It.IsAny<CancellationToken>()), Times.Once);+ _mediatorMock.Verify(m => m.Send(It.Is<ShipCommand>(c => c.Shipment == accessibleShipment2), It.IsAny<CancellationToken>()), Times.Once);+ _mediatorMock.Verify(m => m.Send(It.Is<ShipCommand>(c => c.Shipment == deniedShipment), It.IsAny<CancellationToken>()), Times.Never);+ }++ [TestMethod]+ public async Task SetAsDeliveredSelected_FiltersToAccessibleShipments()+ {+ var accessibleShipment = new Shipment { Id = "s1" };+ var deniedShipment = new Shipment { Id = "s2" };+ _shipmentServiceMock+ .Setup(s => s.GetShipmentsByIds(new[] { "s1", "s2" }))+ .ReturnsAsync((IList<Shipment>)new List<Shipment> { accessibleShipment, deniedShipment });+ _scopeMock.Setup(s => s.HasAccess(accessibleShipment)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.HasAccess(deniedShipment)).ReturnsAsync(false);++ _mediatorMock+ .Setup(m => m.Send(It.IsAny<DeliveryCommand>(), It.IsAny<CancellationToken>()))+ .ReturnsAsync(true);++ var result = await _controller.SetAsDeliveredSelected(new List<string> { "s1", "s2" });++ var jsonResult = result as JsonResult;+ Assert.IsNotNull(jsonResult);+ _mediatorMock.Verify(m => m.Send(It.Is<DeliveryCommand>(c => c.Shipment == accessibleShipment), It.IsAny<CancellationToken>()), Times.Once);+ _mediatorMock.Verify(m => m.Send(It.Is<DeliveryCommand>(c => c.Shipment == deniedShipment), It.IsAny<CancellationToken>()), Times.Never);+ }}diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.csindex d9ed3cb0d..bf8e4b5d6 100644--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs@@ -361,4 +361,147 @@ public abstract class BaseShipmentController(}#endregion++ #region PDF export and bulk actions++ [PermissionAuthorizeAction(PermissionActionName.Preview)]+ public async Task<IActionResult> PdfPackagingSlip(string shipmentId)+ {+ var (shipment, denied) = await LoadAuthorizedShipment(shipmentId);+ if (denied != null) return denied;++ var order = await orderService.GetOrderById(shipment.OrderId);+ if (order == null)+ //No order found with the specified id+ return RedirectToAction("List");++ var shipments = new List<Shipment> { shipment };++ byte[] bytes;+ using (var stream = new MemoryStream())+ {+ await pdfService.PrintPackagingSlipsToPdf(stream, shipments, contextAccessor.WorkContext.WorkingLanguage.Id);+ bytes = stream.ToArray();+ }++ return File(bytes, "application/pdf", $"packagingslip_{shipment.Id}.pdf");+ }++ [PermissionAuthorizeAction(PermissionActionName.Export)]+ [HttpPost]+ public async Task<IActionResult> PdfPackagingSlipAll(ShipmentListModel model)+ {+ if (scope.DefaultStoreId is not null) model.StoreId = scope.DefaultStoreId;+ if (scope.DefaultVendorId is not null) model.VendorId = scope.DefaultVendorId;++ //load shipments+ var shipments = await shipmentViewModelService.PrepareShipments(model, 1, 100);++ //ensure that we at least one shipment selected+ if (shipments.totalCount == 0)+ {+ Error(translationService.GetResource("Admin.Orders.Shipments.NoShipmentsSelected"));+ return RedirectToAction("List");+ }++ byte[] bytes;+ using (var stream = new MemoryStream())+ {+ await pdfService.PrintPackagingSlipsToPdf(stream, shipments.shipments.ToList(),+ contextAccessor.WorkContext.WorkingLanguage.Id);+ bytes = stream.ToArray();+ }++ return File(bytes, "application/pdf", "packagingslips.pdf");+ }++ [PermissionAuthorizeAction(PermissionActionName.Export)]+ [HttpPost]+ public async Task<IActionResult> PdfPackagingSlipSelected(string selectedIds)+ {+ var shipments = new List<Shipment>();+ if (selectedIds != null)+ {+ var ids = selectedIds+ .Split([','], StringSplitOptions.RemoveEmptyEntries)+ .Select(x => x)+ .ToArray();+ shipments.AddRange(await shipmentService.GetShipmentsByIds(ids));+ }++ var accessibleShipments = new List<Shipment>();+ foreach (var s in shipments)+ if (await scope.HasAccess(s))+ accessibleShipments.Add(s);++ //ensure that we at least one shipment selected — checks the unfiltered count, matching+ //all 3 originals' pre-existing (and slightly inconsistent) behavior; see the disclosed,+ //not-fixed note below this method+ if (shipments.Count == 0)+ {+ Error(translationService.GetResource("Admin.Orders.Shipments.NoShipmentsSelected"));+ return RedirectToAction("List");+ }++ byte[] bytes;+ using (var stream = new MemoryStream())+ {+ await pdfService.PrintPackagingSlipsToPdf(stream, accessibleShipments, contextAccessor.WorkContext.WorkingLanguage.Id);+ bytes = stream.ToArray();+ }++ return File(bytes, "application/pdf", "packagingslips.pdf");+ }++ [PermissionAuthorizeAction(PermissionActionName.Edit)]+ [HttpPost]+ public async Task<IActionResult> SetAsShippedSelected(ICollection<string> selectedIds)+ {+ var shipments = new List<Shipment>();+ if (selectedIds != null) shipments.AddRange(await shipmentService.GetShipmentsByIds(selectedIds.ToArray()));++ var accessibleShipments = new List<Shipment>();+ foreach (var s in shipments)+ if (await scope.HasAccess(s))+ accessibleShipments.Add(s);++ foreach (var shipment in accessibleShipments)+ try+ {+ await mediator.Send(new ShipCommand { Shipment = shipment, NotifyCustomer = true });+ }+ catch+ {+ //ignore any exception+ }++ return Json(new { Result = true });+ }++ [PermissionAuthorizeAction(PermissionActionName.Edit)]+ [HttpPost]+ public async Task<IActionResult> SetAsDeliveredSelected(ICollection<string> selectedIds)+ {+ var shipments = new List<Shipment>();+ if (selectedIds != null) shipments.AddRange(await shipmentService.GetShipmentsByIds(selectedIds.ToArray()));++ var accessibleShipments = new List<Shipment>();+ foreach (var s in shipments)+ if (await scope.HasAccess(s))+ accessibleShipments.Add(s);++ foreach (var shipment in accessibleShipments)+ try+ {+ await mediator.Send(new DeliveryCommand { Shipment = shipment, NotifyCustomer = true });+ }+ catch+ {+ //ignore any exception+ }++ return Json(new { Result = true });+ }++ #endregion}