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/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/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/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/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.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/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/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/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/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 b2d686412..50b35ddb0 100644 --- a/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/MerchandiseReturnViewModelService.cs @@ -14,10 +14,12 @@ 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; 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; @@ -658,9 +660,15 @@ 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) && + !(await downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.MerchandiseReturn, + merchandiseReturn.Id)) + return false; + var merchandiseReturnNote = new MerchandiseReturnNote { DisplayToCustomer = displayToCustomer, Note = message, @@ -674,6 +682,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,7 +701,7 @@ public virtual async Task DeleteMerchandiseReturnNote(MerchandiseReturn merchand if (!string.IsNullOrEmpty(merchandiseReturnNote.DownloadId)) { var attachment = await downloadService.GetDownloadById(merchandiseReturnNote.DownloadId); - if (attachment != null) + if (attachment.IsAttachmentOf(DownloadType.MerchandiseReturn, merchandiseReturn.Id)) await downloadService.DeleteDownload(attachment); } } 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 d511053ee..ebca9c257 100644 --- a/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/ShipmentViewModelService.cs @@ -9,9 +9,11 @@ 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; +using Grand.Web.AdminShared.Extensions; using Grand.Web.AdminShared.Interfaces; using Grand.Web.AdminShared.Models.Orders; using Microsoft.AspNetCore.Mvc.Rendering; @@ -256,9 +258,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) && + !(await _downloadService.GetDownloadById(downloadId)).IsAttachmentOf(DownloadType.Shipment, shipment.Id)) + return false; + var shipmentNote = new ShipmentNote { DisplayToCustomer = displayToCustomer, Note = message, @@ -269,6 +276,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,7 +292,7 @@ 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 (attachment.IsAttachmentOf(DownloadType.Shipment, shipment.Id)) await _downloadService.DeleteDownload(attachment); } } 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 889fadc80..886ca7780 100644 Binary files a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml and b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml differ diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml index 071964f37..a1d50f3e2 100644 --- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml +++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml @@ -2817,4 +2817,13 @@ 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. + + + The attached file was not uploaded for this merchandise return. Upload it again here. +