From cb806306d2a33c3cbf44f6b42b5f404a3c1f0d00 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 05:29:43 +0100 Subject: [PATCH 1/2] Attach to a shipment or merchandise return note only a file uploaded for it The note forms post the id of the file they uploaded, and the panels saved any id they were given. A note could point at a file uploaded for another record, even one of another store, and the customer could then download it from the note; deleting the note deleted that file. Shipment and merchandise return notes now take only a file uploaded for that shipment or return, as order notes already do, and deleting a note removes only such a file. The panels show a message when the file is refused. --- .../BaseMerchandiseReturnControllerTests.cs | 17 +++++++ .../BaseShipmentControllerTests.cs | 16 +++++++ .../MerchandiseReturnViewModelServiceTests.cs | 31 +++++++++++-- .../Services/ShipmentViewModelServiceTests.cs | 43 +++++++++++++++++- .../BaseMerchandiseReturnController.cs | 9 +++- .../Controllers/BaseShipmentController.cs | 6 ++- .../IMerchandiseReturnViewModelService.cs | 2 +- .../Interfaces/IShipmentViewModelService.cs | 2 +- .../MerchandiseReturnViewModelService.cs | 16 ++++++- .../Services/ShipmentViewModelService.cs | 15 +++++- .../Partials/MerchandiseReturnNotes.cshtml | 5 ++ .../Shipment/Partials/ShipmentNotes.cshtml | 5 ++ .../App_Data/Resources/DefaultLanguage.xml | Bin 1838394 -> 1839268 bytes .../App_Data/Resources/Upgrade/en_250.xml | 6 +++ 14 files changed, 160 insertions(+), 13 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMerchandiseReturnControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMerchandiseReturnControllerTests.cs index f5995470a..c8d470075 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMerchandiseReturnControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMerchandiseReturnControllerTests.cs @@ -575,6 +575,8 @@ public async Task NoteAdd_Authorized_ResolvesOrderFromEntity_NotFromAParameter() _merchandiseReturnServiceMock.Setup(s => s.GetMerchandiseReturnById("mr1")).ReturnsAsync(entity); _scopeMock.Setup(s => s.HasAccess(entity)).ReturnsAsync(true); _orderServiceMock.Setup(o => o.GetOrderById("o1")).ReturnsAsync(order); + _merchandiseReturnViewModelServiceMock + .Setup(v => v.InsertMerchandiseReturnNote(entity, order, "download-1", true, "msg")).ReturnsAsync(true); var result = await _controller.MerchandiseReturnNoteAdd("mr1", "download-1", true, "msg"); @@ -585,6 +587,21 @@ public async Task NoteAdd_Authorized_ResolvesOrderFromEntity_NotFromAParameter() v => v.InsertMerchandiseReturnNote(entity, order, "download-1", true, "msg"), Times.Once); } + [TestMethod] + public async Task NoteAdd_FileOfAnotherReturn_ReturnsJsonResultFalse() + { + var entity = new MerchandiseReturn { Id = "mr1", OrderId = "o1" }; + _merchandiseReturnServiceMock.Setup(s => s.GetMerchandiseReturnById("mr1")).ReturnsAsync(entity); + _scopeMock.Setup(s => s.HasAccess(entity)).ReturnsAsync(true); + _orderServiceMock.Setup(o => o.GetOrderById("o1")).ReturnsAsync(new Order { Id = "o1" }); + + //the view-model service refuses the download: it was not uploaded for this return + var result = await _controller.MerchandiseReturnNoteAdd("mr1", "download-x", false, "msg"); + + var json = result as JsonResult; + Assert.IsFalse((bool)json.Value.GetType().GetProperty("Result").GetValue(json.Value)); + } + [TestMethod] public async Task NoteAdd_OrderNotFound_ReturnsJsonResultFalse() { diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs index 8d25de7ce..9e9cc33c1 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseShipmentControllerTests.cs @@ -833,6 +833,8 @@ public async Task ShipmentNoteAdd_Authorized_PassesDownloadIdThrough() var shipment = new Shipment { Id = "s1", OrderId = "o1" }; _shipmentServiceMock.Setup(s => s.GetShipmentById("s1")).ReturnsAsync(shipment); _scopeMock.Setup(s => s.HasAccess(shipment)).ReturnsAsync(true); + _shipmentViewModelServiceMock.Setup(v => v.InsertShipmentNote(shipment, "download-1", true, "hello")) + .ReturnsAsync(true); var result = await _controller.ShipmentNoteAdd("s1", "download-1", true, "hello"); @@ -846,6 +848,20 @@ public async Task ShipmentNoteAdd_Authorized_PassesDownloadIdThrough() v => v.InsertShipmentNote(shipment, "download-1", true, "hello"), Times.Once); } + [TestMethod] + public async Task ShipmentNoteAdd_FileOfAnotherShipment_ReturnsResultFalse() + { + var shipment = new Shipment { Id = "s1", OrderId = "o1" }; + _shipmentServiceMock.Setup(s => s.GetShipmentById("s1")).ReturnsAsync(shipment); + _scopeMock.Setup(s => s.HasAccess(shipment)).ReturnsAsync(true); + + //the view-model service refuses the download: it was not uploaded for this shipment + var result = await _controller.ShipmentNoteAdd("s1", "download-x", true, "hello"); + + var value = (result as JsonResult)!.Value; + Assert.AreEqual(false, value.GetType().GetProperty("Result")!.GetValue(value)); + } + [TestMethod] public async Task ShipmentNoteDelete_Denied_Throws() { diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/MerchandiseReturnViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/MerchandiseReturnViewModelServiceTests.cs index 3d98ea96c..5cca063d9 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/MerchandiseReturnViewModelServiceTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/MerchandiseReturnViewModelServiceTests.cs @@ -19,6 +19,7 @@ using Grand.Domain.Customers; using Grand.Domain.Directory; using Grand.Domain.Localization; +using Grand.Domain.Media; using Grand.Domain.Orders; using Grand.Domain.Permissions; using Grand.Domain.Shipping; @@ -300,7 +301,8 @@ public async Task PrepareListModel_AdminScope_VendorsAfterAll() } private static MerchandiseReturnViewModelService Build(ICustomerService customerService, - IContextAccessor contextAccessor) + IContextAccessor contextAccessor, IDownloadService downloadService = null, + IMerchandiseReturnService merchandiseReturnService = null) { var orderServiceMock = new Mock(); var productServiceMock = new Mock(); @@ -335,13 +337,13 @@ private static MerchandiseReturnViewModelService Build(ICustomerService customer dateTimeServiceMock.Object, translationServiceMock.Object, merchandiseReturnMessageProviderServiceMock.Object, - merchandiseReturnServiceMock.Object, + merchandiseReturnService ?? merchandiseReturnServiceMock.Object, priceFormatterMock.Object, addressSettings, countryServiceMock.Object, addressAttributeServiceMock.Object, addressAttributeParserMock.Object, - downloadServiceMock.Object, + downloadService ?? downloadServiceMock.Object, orderSettings, enumTranslationServiceMock.Object, contextAccessor, @@ -1468,4 +1470,27 @@ public async Task ResolveRefund_NamesTheStaffMember() mocks.Refunds.Verify(x => x.ResolveRefund("r1", false, "john@shop.com"), Times.Once); } + + [TestMethod] + [DataRow(DownloadType.MerchandiseReturn, "mr1", true)] + [DataRow(DownloadType.MerchandiseReturn, "mr2", false)] + [DataRow(DownloadType.Order, "mr1", false)] + public async Task InsertMerchandiseReturnNote_SavesOnlyAFileUploadedForThisReturn(DownloadType type, + string referenceId, bool saved) + { + var downloads = new Mock(); + downloads.Setup(d => d.GetDownloadById("d1")) + .ReturnsAsync(new Download { DownloadType = type, ReferenceId = referenceId }); + var returns = new Mock(); + var service = Build(new Mock().Object, new Mock().Object, + downloads.Object, returns.Object); + + var result = await service.InsertMerchandiseReturnNote(new MerchandiseReturn { Id = "mr1" }, new Order(), + "d1", false, "msg"); + + Assert.AreEqual(saved, result); + returns.Verify(r => r.InsertMerchandiseReturnNote(It.IsAny()), + saved ? Times.Once() : Times.Never()); + } } + diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/ShipmentViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/ShipmentViewModelServiceTests.cs index 2560b2f2e..7372947c5 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Services/ShipmentViewModelServiceTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Services/ShipmentViewModelServiceTests.cs @@ -7,6 +7,7 @@ using Grand.Business.Core.Interfaces.Storage; using Grand.Domain.Catalog; using Grand.Domain.Directory; +using Grand.Domain.Media; using Grand.Domain.Orders; using Grand.Domain.Shipping; using Grand.Infrastructure; @@ -29,6 +30,7 @@ public class ShipmentViewModelServiceTests private Mock _warehouseServiceMock; private Mock _measureServiceMock; private Mock> _scopeMock; + private Mock _downloadServiceMock; private ShipmentViewModelService _service; [TestInitialize] @@ -40,6 +42,7 @@ public void Setup() _warehouseServiceMock = new Mock(); _measureServiceMock = new Mock(); _scopeMock = new Mock>(); + _downloadServiceMock = new Mock(); _measureServiceMock.Setup(m => m.GetMeasureWeightById(It.IsAny())).ReturnsAsync((MeasureWeight)null); _measureServiceMock.Setup(m => m.GetMeasureDimensionById(It.IsAny())) @@ -62,7 +65,7 @@ public void Setup() new Mock().Object, new Mock().Object, _translationServiceMock.Object, - new Mock().Object, + _downloadServiceMock.Object, new Mock().Object, new Mock().Object, new MeasureSettings(), @@ -287,4 +290,42 @@ public async Task PrepareShipment_NullDefaultVendorId_LeavesVendorIdNull() Assert.IsNotNull(shipment); Assert.IsNull(shipment.VendorId); } + + [TestMethod] + public async Task InsertShipmentNote_FileUploadedForThisShipment_Saved() + { + var shipment = new Shipment { Id = "s1" }; + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")) + .ReturnsAsync(new Download { DownloadType = DownloadType.Shipment, ReferenceId = "s1" }); + + Assert.IsTrue(await _service.InsertShipmentNote(shipment, "d1", false, "msg")); + _shipmentServiceMock.Verify(s => s.InsertShipmentNote(It.Is(n => n.DownloadId == "d1")), Times.Once); + } + + [TestMethod] + [DataRow(DownloadType.Shipment, "s2")] + [DataRow(DownloadType.Product, "s1")] + public async Task InsertShipmentNote_FileOfAnotherRecord_Refused(DownloadType type, string referenceId) + { + var shipment = new Shipment { Id = "s1" }; + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")) + .ReturnsAsync(new Download { DownloadType = type, ReferenceId = referenceId }); + + Assert.IsFalse(await _service.InsertShipmentNote(shipment, "d1", false, "msg")); + _shipmentServiceMock.Verify(s => s.InsertShipmentNote(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task DeleteShipmentNote_KeepsAFileOfAnotherRecord() + { + var shipment = new Shipment { Id = "s1" }; + var foreign = new Download { DownloadType = DownloadType.Product, ReferenceId = "p1" }; + _shipmentServiceMock.Setup(s => s.GetShipmentNotes("s1")) + .ReturnsAsync(new List { new() { Id = "n1", ShipmentId = "s1", DownloadId = "d1" } }); + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(foreign); + + await _service.DeleteShipmentNote(shipment, "n1"); + + _downloadServiceMock.Verify(d => d.DeleteDownload(It.IsAny()), Times.Never); + } } diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseMerchandiseReturnController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseMerchandiseReturnController.cs index 64b79eefb..87da37284 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseMerchandiseReturnController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseMerchandiseReturnController.cs @@ -349,8 +349,13 @@ public async Task MerchandiseReturnNoteAdd(string merchandiseRetu var order = await orderService.GetOrderById(merchandiseReturn.OrderId); if (order == null) return Json(new { Result = false }); - await merchandiseReturnViewModelService.InsertMerchandiseReturnNote(merchandiseReturn, order, downloadId, - displayToCustomer, message); + if (!await merchandiseReturnViewModelService.InsertMerchandiseReturnNote(merchandiseReturn, order, downloadId, + displayToCustomer, message)) + return Json(new { + Result = false, + Message = translationService.GetResource( + "Admin.Orders.MerchandiseReturns.MerchandiseReturnNotes.DownloadNotOfMerchandiseReturn") + }); return Json(new { Result = true }); } diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs index 3e166303b..6535c6daa 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseShipmentController.cs @@ -634,7 +634,11 @@ public async Task ShipmentNoteAdd(string shipmentId, string downl if (shipment == null || !await scope.HasAccess(shipment)) return Json(new { Result = false }); - await shipmentViewModelService.InsertShipmentNote(shipment, downloadId, displayToCustomer, message); + if (!await shipmentViewModelService.InsertShipmentNote(shipment, downloadId, displayToCustomer, message)) + return Json(new { + Result = false, + Message = translationService.GetResource("Admin.Orders.Shipments.ShipmentNotes.DownloadNotOfShipment") + }); return Json(new { Result = true }); } diff --git a/src/Web/Grand.Web.AdminShared/Interfaces/IMerchandiseReturnViewModelService.cs b/src/Web/Grand.Web.AdminShared/Interfaces/IMerchandiseReturnViewModelService.cs index b2773a04a..9bfbf3933 100644 --- a/src/Web/Grand.Web.AdminShared/Interfaces/IMerchandiseReturnViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Interfaces/IMerchandiseReturnViewModelService.cs @@ -60,7 +60,7 @@ Task PrepareMerchandiseReturnModel(MerchandiseReturnMode Task> PrepareMerchandiseReturnNotes( MerchandiseReturn merchandiseReturn); - Task InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, string downloadId, + Task InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, string downloadId, bool displayToCustomer, string message); Task DeleteMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, string id); diff --git a/src/Web/Grand.Web.AdminShared/Interfaces/IShipmentViewModelService.cs b/src/Web/Grand.Web.AdminShared/Interfaces/IShipmentViewModelService.cs index 0d66e45ba..0ddcd37e7 100644 --- a/src/Web/Grand.Web.AdminShared/Interfaces/IShipmentViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Interfaces/IShipmentViewModelService.cs @@ -13,7 +13,7 @@ Task PrepareShipmentModel(Shipment shipment, bool prepareProducts Task GetStockQty(Product product, string warehouseId); Task GetReservedQty(Product product, string warehouseId); Task> PrepareShipmentNotes(Shipment shipment); - Task InsertShipmentNote(Shipment shipment, string downloadId, bool displayToCustomer, string message); + Task InsertShipmentNote(Shipment shipment, string downloadId, bool displayToCustomer, string message); Task DeleteShipmentNote(Shipment shipment, string id); Task PrepareShipmentListModel(); Task PrepareShipmentModel(Order order); diff --git a/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs index b2d686412..feb349dc0 100644 --- a/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs @@ -14,6 +14,7 @@ using Grand.Domain.Catalog; using Grand.Domain.Common; using Grand.Domain.Directory; +using Grand.Domain.Media; using Grand.Domain.Orders; using Grand.Domain.Permissions; using Grand.Domain.Shipping; @@ -658,9 +659,14 @@ public virtual Task DeleteMerchandiseReturn(MerchandiseReturn merchandis return merchandiseReturnNoteModels; } - public virtual async Task InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, + public virtual async Task InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, string downloadId, bool displayToCustomer, string message) { + //the id comes from the request; a file uploaded for another merchandise return is refused + if (!string.IsNullOrEmpty(downloadId) && + !IsAttachmentOf(await downloadService.GetDownloadById(downloadId), merchandiseReturn)) + return false; + var merchandiseReturnNote = new MerchandiseReturnNote { DisplayToCustomer = displayToCustomer, Note = message, @@ -674,6 +680,8 @@ public virtual async Task InsertMerchandiseReturnNote(MerchandiseReturn merchand //email await merchandiseReturnMessageProviderService.SendNewMerchandiseReturnNoteAddedCustomerMessage(merchandiseReturn, merchandiseReturnNote, order); + + return true; } public virtual async Task DeleteMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, string id) @@ -691,11 +699,15 @@ public virtual async Task DeleteMerchandiseReturnNote(MerchandiseReturn merchand if (!string.IsNullOrEmpty(merchandiseReturnNote.DownloadId)) { var attachment = await downloadService.GetDownloadById(merchandiseReturnNote.DownloadId); - if (attachment != null) + if (IsAttachmentOf(attachment, merchandiseReturn)) await downloadService.DeleteDownload(attachment); } } + //the notes form uploads with DownloadType.MerchandiseReturn and the return id as the reference + private static bool IsAttachmentOf(Download download, MerchandiseReturn merchandiseReturn) => + download is { DownloadType: DownloadType.MerchandiseReturn } && download.ReferenceId == merchandiseReturn.Id; + #region Fields #endregion Fields diff --git a/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs index d511053ee..1011628da 100644 --- a/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs @@ -9,6 +9,7 @@ using Grand.Business.Core.Interfaces.Storage; using Grand.Domain.Catalog; using Grand.Domain.Directory; +using Grand.Domain.Media; using Grand.Domain.Orders; using Grand.Domain.Shipping; using Grand.Infrastructure; @@ -256,9 +257,14 @@ public virtual async Task GetReservedQty(Product product, string warehouseI return shipmentNoteModels; } - public virtual async Task InsertShipmentNote(Shipment shipment, string downloadId, bool displayToCustomer, + public virtual async Task InsertShipmentNote(Shipment shipment, string downloadId, bool displayToCustomer, string message) { + //the id comes from the request; a file uploaded for another shipment is refused + if (!string.IsNullOrEmpty(downloadId) && + !IsAttachmentOf(await _downloadService.GetDownloadById(downloadId), shipment)) + return false; + var shipmentNote = new ShipmentNote { DisplayToCustomer = displayToCustomer, Note = message, @@ -269,6 +275,7 @@ public virtual async Task InsertShipmentNote(Shipment shipment, string downloadI //new shipment note notification // TODO + return true; } public virtual async Task DeleteShipmentNote(Shipment shipment, string id) @@ -284,11 +291,15 @@ public virtual async Task DeleteShipmentNote(Shipment shipment, string id) if (!string.IsNullOrEmpty(shipmentNote.DownloadId)) { var attachment = await _downloadService.GetDownloadById(shipmentNote.DownloadId); - if (attachment != null) + if (IsAttachmentOf(attachment, shipment)) await _downloadService.DeleteDownload(attachment); } } + //the notes form uploads with DownloadType.Shipment and the shipment id as the reference + private static bool IsAttachmentOf(Download download, Shipment shipment) => + download is { DownloadType: DownloadType.Shipment } && download.ReferenceId == shipment.Id; + public virtual async Task<(IEnumerable shipments, int totalCount)> PrepareShipments( ShipmentListModel model, int pageIndex, int pageSize) { diff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/MerchandiseReturn/Partials/MerchandiseReturnNotes.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/MerchandiseReturn/Partials/MerchandiseReturnNotes.cshtml index 517e17282..f1266f362 100644 --- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/MerchandiseReturn/Partials/MerchandiseReturnNotes.cshtml +++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/MerchandiseReturn/Partials/MerchandiseReturnNotes.cshtml @@ -101,6 +101,11 @@ url: "@(Url.Action("MerchandiseReturnNoteAdd", "MerchandiseReturn", new { area = area }))", data: postData, success: function (data) { + if (!data.Result) { + alert(data.Message || '@Loc["Admin.Orders.MerchandiseReturns.MerchandiseReturnNotes.AddFailed"]'); + $('#addMerchandiseReturnNote').attr('disabled', false); + return; + } var grid = GrandAdmin.grids.get("#merchandisereturnnotes-grid"); grid.dataSource.read(); $('#addMerchandiseReturnNote').attr('disabled', false); diff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/Shipment/Partials/ShipmentNotes.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/Shipment/Partials/ShipmentNotes.cshtml index 448a1a1f6..c41a60365 100644 --- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/Shipment/Partials/ShipmentNotes.cshtml +++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/Shipment/Partials/ShipmentNotes.cshtml @@ -100,6 +100,11 @@ url: "@(Url.Action("ShipmentNoteAdd", "Shipment", new { area = area }))", data: postData, success: function (data) { + if (!data.Result) { + alert(data.Message || 'Failed to add shipment note.'); + $('#addShipmentNote').attr('disabled', false); + return; + } var grid = GrandAdmin.grids.get("#shipmentnotes-grid"); grid.dataSource.read(); $('#addShipmentNote').attr('disabled', false); diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml index 889fadc80c7b4b6acf3dc631e2955ef43b42e05e..1f3a534046f8809851c7c84cff38790a1dbf7159 100644 GIT binary patch delta 230 zcmdlrt6<5(f`%5x7N!>F7M2#)7Pc1l7LFFq7OocVEj)5U)A@c03$W)gESV(o^e=8WqM5y&#dVJ^*kZl<%D?NNlxEj z$!j$IfgUfF7M2#)7Pc1l7LFFq7OocVEj)5U+kXi2ypx=MAdA;x`hwLw zT+ Cycle + + The attached file was not uploaded for this shipment. Upload it again here. + + + The attached file was not uploaded for this merchandise return. Upload it again here. + From 5ea979eff3da848c7eb2f24ce117b689d81aa78c Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:16:31 +0100 Subject: [PATCH 2/2] Attach to a customer note only a file uploaded for that customer, check every note kind one way Customer notes saved any file id the request gave and deleted that file with the note, the gap shipment and merchandise return notes had. They now take only a file uploaded for that customer, and deleting a note removes only such a file. The order, shipment and merchandise return notes each had their own copy of the check; the four note kinds now share one Download extension. --- .../CustomerNoteControllerTests.cs | 1 + .../CustomerNoteViewModelServiceTests.cs | 59 +++++++++++++++--- .../Partials/CreateOrUpdate.TabNotes.cshtml | 5 ++ .../Controllers/CustomerNoteController.cs | 10 ++- .../Extensions/DownloadExtensions.cs | 10 +++ .../ICustomerNoteViewModelService.cs | 5 +- .../Services/CustomerNoteViewModelService.cs | 13 +++- .../MerchandiseReturnViewModelService.cs | 10 ++- .../Services/OrderNoteViewModelService.cs | 9 +-- .../Services/ShipmentViewModelService.cs | 9 +-- .../App_Data/Resources/DefaultLanguage.xml | Bin 1839268 -> 1839674 bytes .../App_Data/Resources/Upgrade/en_250.xml | 3 + 12 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 src/Web/Grand.Web.AdminShared/Extensions/DownloadExtensions.cs diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/CustomerNoteControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/CustomerNoteControllerTests.cs index 6655a2435..ae2e63599 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/CustomerNoteControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/CustomerNoteControllerTests.cs @@ -12,6 +12,7 @@ public class CustomerNoteControllerTests : CustomerControllerTestsBase new( CustomerServiceMock.Object, CustomerNoteViewModelServiceMock.Object, + TranslationServiceMock.Object, ScopeMock.Object); [TestMethod] diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/CustomerNoteViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/CustomerNoteViewModelServiceTests.cs index c124fd062..37f86a9a2 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Services/CustomerNoteViewModelServiceTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Services/CustomerNoteViewModelServiceTests.cs @@ -39,23 +39,47 @@ public void Setup() [TestMethod] public async Task InsertCustomerNote_InsertsNoteWithMappedFields() { - var note = await _customerNoteViewModelService.InsertCustomerNote("c1", "d1", false, "title", "message"); - - Assert.AreEqual("c1", note.CustomerId); - Assert.AreEqual("d1", note.DownloadId); - Assert.AreEqual("title", note.Title); - Assert.AreEqual("message", note.Note); - Assert.IsFalse(note.DisplayToCustomer); - _customerNoteServiceMock.Verify( - c => c.InsertCustomerNote(It.Is(x => x.CustomerId == "c1" && x.Title == "title")), + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(new Download + { Id = "d1", DownloadType = DownloadType.Customer, ReferenceId = "c1" }); + + var result = await _customerNoteViewModelService.InsertCustomerNote("c1", "d1", false, "title", "message"); + + Assert.IsTrue(result); + _customerNoteServiceMock.Verify(c => c.InsertCustomerNote(It.Is(x => + x.CustomerId == "c1" && x.DownloadId == "d1" && x.Title == "title" && x.Note == "message" && + !x.DisplayToCustomer)), Times.Once); } + [TestMethod] + public async Task InsertCustomerNote_FileOfAnotherRecord_Refused_NothingInserted() + { + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(new Download + { Id = "d1", DownloadType = DownloadType.Product, ReferenceId = "c1" }); + + var result = await _customerNoteViewModelService.InsertCustomerNote("c1", "d1", false, "title", "message"); + + Assert.IsFalse(result); + _customerNoteServiceMock.Verify(c => c.InsertCustomerNote(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task InsertCustomerNote_FileOfAnotherCustomer_Refused_NothingInserted() + { + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(new Download + { Id = "d1", DownloadType = DownloadType.Customer, ReferenceId = "c2" }); + + var result = await _customerNoteViewModelService.InsertCustomerNote("c1", "d1", false, "title", "message"); + + Assert.IsFalse(result); + _customerNoteServiceMock.Verify(c => c.InsertCustomerNote(It.IsAny()), Times.Never); + } + [TestMethod] public async Task DeleteCustomerNote_DeletesNoteAndAttachment() { var note = new CustomerNote { Id = "n1", CustomerId = "c1", DownloadId = "d1" }; - var download = new Download { Id = "d1" }; + var download = new Download { Id = "d1", DownloadType = DownloadType.Customer, ReferenceId = "c1" }; _customerNoteServiceMock.Setup(c => c.GetCustomerNote("n1")).ReturnsAsync(note); _customerNoteServiceMock.Setup(c => c.DeleteCustomerNote(It.IsAny())).Returns(Task.CompletedTask); _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(download); @@ -67,6 +91,21 @@ public async Task DeleteCustomerNote_DeletesNoteAndAttachment() _downloadServiceMock.Verify(d => d.DeleteDownload(download), Times.Once); } + [TestMethod] + public async Task DeleteCustomerNote_FileOfAnotherRecord_NoteDeleted_FileKept() + { + var note = new CustomerNote { Id = "n1", CustomerId = "c1", DownloadId = "d1" }; + _customerNoteServiceMock.Setup(c => c.GetCustomerNote("n1")).ReturnsAsync(note); + _customerNoteServiceMock.Setup(c => c.DeleteCustomerNote(It.IsAny())).Returns(Task.CompletedTask); + _downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(new Download + { Id = "d1", DownloadType = DownloadType.Product, ReferenceId = "p1" }); + + await _customerNoteViewModelService.DeleteCustomerNote("n1", "c1"); + + _customerNoteServiceMock.Verify(c => c.DeleteCustomerNote(note), Times.Once); + _downloadServiceMock.Verify(d => d.DeleteDownload(It.IsAny()), Times.Never); + } + [TestMethod] public async Task DeleteCustomerNote_NotFound_Throws() { diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Customer/Partials/CreateOrUpdate.TabNotes.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Customer/Partials/CreateOrUpdate.TabNotes.cshtml index ae32a1a1e..dfae560ed 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Customer/Partials/CreateOrUpdate.TabNotes.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Customer/Partials/CreateOrUpdate.TabNotes.cshtml @@ -105,6 +105,11 @@ url: "@(Url.Action("CustomerNoteAdd", "CustomerNote", new { area = Constants.AreaAdmin }))", data: postData, success: function (data) { + if (!data.Result) { + alert(data.Message || 'Failed to add customer note.'); + $('#addCustomerNote').attr('disabled', false); + return; + } var grid = GrandAdmin.grids.get("#customernotes-grid"); grid.dataSource.read(); $('#addCustomerNote').attr('disabled', false); diff --git a/src/Web/Grand.Web.Admin/Controllers/CustomerNoteController.cs b/src/Web/Grand.Web.Admin/Controllers/CustomerNoteController.cs index ec1b2de93..b73170230 100644 --- a/src/Web/Grand.Web.Admin/Controllers/CustomerNoteController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/CustomerNoteController.cs @@ -6,6 +6,7 @@ using Grand.Web.AdminShared.Interfaces; using Grand.Web.Common.DataSource; using Grand.Web.Common.Filters; +using Grand.Web.Common.Localization; using Grand.Web.Common.Security.Authorization; using Microsoft.AspNetCore.Mvc; @@ -18,6 +19,7 @@ namespace Grand.Web.Admin.Controllers; public class CustomerNoteController( ICustomerService customerService, ICustomerNoteViewModelService customerNoteViewModelService, + IRequestTranslationService translationService, IAdminDataScope scope) : BaseCustomerScopedController(customerService, scope) { @@ -41,7 +43,13 @@ public async Task CustomerNoteAdd(string customerId, string downl var (_, denied) = await LoadAuthorizedCustomer(customerId); if (denied != null) return Json(new { Result = false }); - await customerNoteViewModelService.InsertCustomerNote(customerId, downloadId, displayToCustomer, title, message); + if (!await customerNoteViewModelService.InsertCustomerNote(customerId, downloadId, displayToCustomer, title, + message)) + return Json(new { + Result = false, + Message = translationService.GetResource("Admin.Customers.Customers.CustomerNotes.DownloadNotOfCustomer") + }); + return Json(new { Result = true }); } diff --git a/src/Web/Grand.Web.AdminShared/Extensions/DownloadExtensions.cs b/src/Web/Grand.Web.AdminShared/Extensions/DownloadExtensions.cs new file mode 100644 index 000000000..dc64dfcc7 --- /dev/null +++ b/src/Web/Grand.Web.AdminShared/Extensions/DownloadExtensions.cs @@ -0,0 +1,10 @@ +using Grand.Domain.Media; + +namespace Grand.Web.AdminShared.Extensions; + +public static class DownloadExtensions +{ + //a notes form uploads with its record's download type and the record id as the reference + public static bool IsAttachmentOf(this Download download, DownloadType downloadType, string referenceId) => + download != null && download.DownloadType == downloadType && download.ReferenceId == referenceId; +} diff --git a/src/Web/Grand.Web.AdminShared/Interfaces/ICustomerNoteViewModelService.cs b/src/Web/Grand.Web.AdminShared/Interfaces/ICustomerNoteViewModelService.cs index 7898768e4..06904e2c1 100644 --- a/src/Web/Grand.Web.AdminShared/Interfaces/ICustomerNoteViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Interfaces/ICustomerNoteViewModelService.cs @@ -1,5 +1,4 @@ -using Grand.Domain.Customers; -using Grand.Web.AdminShared.Models.Customers; +using Grand.Web.AdminShared.Models.Customers; namespace Grand.Web.AdminShared.Interfaces; @@ -7,7 +6,7 @@ public interface ICustomerNoteViewModelService { Task> PrepareCustomerNoteList(string customerId); - Task InsertCustomerNote(string customerId, string downloadId, bool displayToCustomer, string title, + Task InsertCustomerNote(string customerId, string downloadId, bool displayToCustomer, string title, string message); Task DeleteCustomerNote(string id, string customerId); diff --git a/src/Web/Grand.Web.AdminShared/Services/CustomerNoteViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/CustomerNoteViewModelService.cs index 5d28d52e7..0f46716fb 100644 --- a/src/Web/Grand.Web.AdminShared/Services/CustomerNoteViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/CustomerNoteViewModelService.cs @@ -3,7 +3,9 @@ using Grand.Business.Core.Interfaces.Messages; using Grand.Business.Core.Interfaces.Storage; using Grand.Domain.Customers; +using Grand.Domain.Media; using Grand.Infrastructure; +using Grand.Web.AdminShared.Extensions; using Grand.Web.AdminShared.Interfaces; using Grand.Web.AdminShared.Models.Customers; @@ -42,9 +44,14 @@ public class CustomerNoteViewModelService( return customerNoteModels; } - public virtual async Task InsertCustomerNote(string customerId, string downloadId, + public virtual async Task InsertCustomerNote(string customerId, string downloadId, bool displayToCustomer, string title, string message) { + //the id comes from the request; a file uploaded for another customer is refused + if (!string.IsNullOrEmpty(downloadId) && + !(await downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.Customer, customerId)) + return false; + var customerNote = new CustomerNote { DisplayToCustomer = displayToCustomer, Title = title, @@ -63,7 +70,7 @@ await customerService.GetCustomerById(customerId), contextAccessor.StoreContext. contextAccessor.WorkContext.WorkingLanguage.Id); } - return customerNote; + return true; } public virtual async Task DeleteCustomerNote(string id, string customerId) @@ -78,7 +85,7 @@ public virtual async Task DeleteCustomerNote(string id, string customerId) if (!string.IsNullOrEmpty(customerNote.DownloadId)) { var attachment = await downloadService.GetDownloadById(customerNote.DownloadId); - if (attachment != null) + if (attachment.IsAttachmentOf(DownloadType.Customer, customerId)) await downloadService.DeleteDownload(attachment); } } diff --git a/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs index feb349dc0..50b35ddb0 100644 --- a/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs @@ -19,6 +19,7 @@ using Grand.Domain.Permissions; using Grand.Domain.Shipping; using Grand.Infrastructure; +using Grand.Web.AdminShared.Extensions; using Grand.Web.AdminShared.Extensions.Mapping; using Grand.Web.AdminShared.Interfaces; using Grand.Web.AdminShared.Models.Common; @@ -664,7 +665,8 @@ public virtual async Task InsertMerchandiseReturnNote(MerchandiseReturn me { //the id comes from the request; a file uploaded for another merchandise return is refused if (!string.IsNullOrEmpty(downloadId) && - !IsAttachmentOf(await downloadService.GetDownloadById(downloadId), merchandiseReturn)) + !(await downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.MerchandiseReturn, + merchandiseReturn.Id)) return false; var merchandiseReturnNote = new MerchandiseReturnNote { @@ -699,15 +701,11 @@ public virtual async Task DeleteMerchandiseReturnNote(MerchandiseReturn merchand if (!string.IsNullOrEmpty(merchandiseReturnNote.DownloadId)) { var attachment = await downloadService.GetDownloadById(merchandiseReturnNote.DownloadId); - if (IsAttachmentOf(attachment, merchandiseReturn)) + if (attachment.IsAttachmentOf(DownloadType.MerchandiseReturn, merchandiseReturn.Id)) await downloadService.DeleteDownload(attachment); } } - //the notes form uploads with DownloadType.MerchandiseReturn and the return id as the reference - private static bool IsAttachmentOf(Download download, MerchandiseReturn merchandiseReturn) => - download is { DownloadType: DownloadType.MerchandiseReturn } && download.ReferenceId == merchandiseReturn.Id; - #region Fields #endregion Fields diff --git a/src/Web/Grand.Web.AdminShared/Services/OrderNoteViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/OrderNoteViewModelService.cs index 2172ba920..136f93d10 100644 --- a/src/Web/Grand.Web.AdminShared/Services/OrderNoteViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/OrderNoteViewModelService.cs @@ -4,6 +4,7 @@ using Grand.Business.Core.Interfaces.Storage; using Grand.Domain.Media; using Grand.Domain.Orders; +using Grand.Web.AdminShared.Extensions; using Grand.Web.AdminShared.Interfaces; using Grand.Web.AdminShared.Models.Orders; @@ -46,7 +47,7 @@ public virtual async Task InsertOrderNote(Order order, string downloadId, { //the id comes from the request; a file uploaded for another order is refused if (!string.IsNullOrEmpty(downloadId) && - !IsAttachmentOf(await downloadService.GetDownloadById(downloadId), order)) + !(await downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.Order, order.Id)) return false; var orderNote = new OrderNote { @@ -77,12 +78,8 @@ public virtual async Task DeleteOrderNote(Order order, string id) if (!string.IsNullOrEmpty(orderNote.DownloadId)) { var attachment = await downloadService.GetDownloadById(orderNote.DownloadId); - if (IsAttachmentOf(attachment, order)) + if (attachment.IsAttachmentOf(DownloadType.Order, order.Id)) await downloadService.DeleteDownload(attachment); } } - - //the notes form uploads with DownloadType.Order and the order id as the reference - private static bool IsAttachmentOf(Download download, Order order) => - download is { DownloadType: DownloadType.Order } && download.ReferenceId == order.Id; } diff --git a/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs index 1011628da..ebca9c257 100644 --- a/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs @@ -13,6 +13,7 @@ using Grand.Domain.Orders; using Grand.Domain.Shipping; using Grand.Infrastructure; +using Grand.Web.AdminShared.Extensions; using Grand.Web.AdminShared.Interfaces; using Grand.Web.AdminShared.Models.Orders; using Microsoft.AspNetCore.Mvc.Rendering; @@ -262,7 +263,7 @@ public virtual async Task InsertShipmentNote(Shipment shipment, string dow { //the id comes from the request; a file uploaded for another shipment is refused if (!string.IsNullOrEmpty(downloadId) && - !IsAttachmentOf(await _downloadService.GetDownloadById(downloadId), shipment)) + !(await _downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.Shipment, shipment.Id)) return false; var shipmentNote = new ShipmentNote { @@ -291,15 +292,11 @@ public virtual async Task DeleteShipmentNote(Shipment shipment, string id) if (!string.IsNullOrEmpty(shipmentNote.DownloadId)) { var attachment = await _downloadService.GetDownloadById(shipmentNote.DownloadId); - if (IsAttachmentOf(attachment, shipment)) + if (attachment.IsAttachmentOf(DownloadType.Shipment, shipment.Id)) await _downloadService.DeleteDownload(attachment); } } - //the notes form uploads with DownloadType.Shipment and the shipment id as the reference - private static bool IsAttachmentOf(Download download, Shipment shipment) => - download is { DownloadType: DownloadType.Shipment } && download.ReferenceId == shipment.Id; - public virtual async Task<(IEnumerable shipments, int totalCount)> PrepareShipments( ShipmentListModel model, int pageIndex, int pageSize) { diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml index 1f3a534046f8809851c7c84cff38790a1dbf7159..886ca7780e8522dce217b2cb23ffeb81c7af1fc4 100644 GIT binary patch delta 170 zcmZ3ouwd7cf`%5x7N!>F7M2#)7Pc1l7LF~PiwvhH2yn_wmlNWVnm#F8Qek?+K~|mV z33oZzrcco0@tQubi&J3wvwohQ=?`2vHKuztaCuEnSS&3x{lGIWj_CsPMY!4pj5vW9 yL~{c%4-oSLF&_}~1F--Q3j(na5DNpb2oQ?`u^14G1F-}UO9HXfb^#;lDfa-6ATqB2 delta 105 zcmWN=#|eNy6hJ{-65RTl fk9E-u88K$Ulo@jtELpK;! Cycle + + The attached file was not uploaded for this customer. Upload it again here. + The attached file was not uploaded for this shipment. Upload it again here.