Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CustomerNoteControllerTests : CustomerControllerTestsBase<CustomerN
protected override CustomerNoteController CreateController() => new(
CustomerServiceMock.Object,
CustomerNoteViewModelServiceMock.Object,
TranslationServiceMock.Object,
ScopeMock.Object);

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IOrderService>();
var productServiceMock = new Mock<IProductService>();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<IDownloadService>();
downloads.Setup(d => d.GetDownloadById("d1"))
.ReturnsAsync(new Download { DownloadType = type, ReferenceId = referenceId });
var returns = new Mock<IMerchandiseReturnService>();
var service = Build(new Mock<ICustomerService>().Object, new Mock<IContextAccessor>().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<MerchandiseReturnNote>()),
saved ? Times.Once() : Times.Never());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomerNote>(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<CustomerNote>(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<CustomerNote>()), 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<CustomerNote>()), 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<CustomerNote>())).Returns(Task.CompletedTask);
_downloadServiceMock.Setup(d => d.GetDownloadById("d1")).ReturnsAsync(download);
Expand All @@ -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<CustomerNote>())).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<Download>()), Times.Never);
}

[TestMethod]
public async Task DeleteCustomerNote_NotFound_Throws()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public class ShipmentViewModelServiceTests
private Mock<IWarehouseService> _warehouseServiceMock;
private Mock<IMeasureService> _measureServiceMock;
private Mock<IAdminDataScope<Shipment>> _scopeMock;
private Mock<IDownloadService> _downloadServiceMock;
private ShipmentViewModelService _service;

[TestInitialize]
Expand All @@ -40,6 +42,7 @@ public void Setup()
_warehouseServiceMock = new Mock<IWarehouseService>();
_measureServiceMock = new Mock<IMeasureService>();
_scopeMock = new Mock<IAdminDataScope<Shipment>>();
_downloadServiceMock = new Mock<IDownloadService>();

_measureServiceMock.Setup(m => m.GetMeasureWeightById(It.IsAny<string>())).ReturnsAsync((MeasureWeight)null);
_measureServiceMock.Setup(m => m.GetMeasureDimensionById(It.IsAny<string>()))
Expand All @@ -62,7 +65,7 @@ public void Setup()
new Mock<IDateTimeService>().Object,
new Mock<ICountryService>().Object,
_translationServiceMock.Object,
new Mock<IDownloadService>().Object,
_downloadServiceMock.Object,
new Mock<IShippingService>().Object,
new Mock<IStockQuantityService>().Object,
new MeasureSettings(),
Expand Down Expand Up @@ -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<ShipmentNote>(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<ShipmentNote>()), 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<ShipmentNote> { 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<Download>()), Times.Never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion src/Web/Grand.Web.Admin/Controllers/CustomerNoteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,6 +19,7 @@ namespace Grand.Web.Admin.Controllers;
public class CustomerNoteController(
ICustomerService customerService,
ICustomerNoteViewModelService customerNoteViewModelService,
IRequestTranslationService translationService,
IAdminDataScope<Customer> scope)
: BaseCustomerScopedController(customerService, scope)
{
Expand All @@ -41,7 +43,13 @@ public async Task<IActionResult> 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 });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ public async Task<IActionResult> 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 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,11 @@ public async Task<IActionResult> 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 });
}
Expand Down
10 changes: 10 additions & 0 deletions src/Web/Grand.Web.AdminShared/Extensions/DownloadExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Grand.Domain.Customers;
using Grand.Web.AdminShared.Models.Customers;
using Grand.Web.AdminShared.Models.Customers;

namespace Grand.Web.AdminShared.Interfaces;

public interface ICustomerNoteViewModelService
{
Task<IList<CustomerModel.CustomerNote>> PrepareCustomerNoteList(string customerId);

Task<CustomerNote> InsertCustomerNote(string customerId, string downloadId, bool displayToCustomer, string title,
Task<bool> InsertCustomerNote(string customerId, string downloadId, bool displayToCustomer, string title,
string message);

Task DeleteCustomerNote(string id, string customerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Task<MerchandiseReturnModel> PrepareMerchandiseReturnModel(MerchandiseReturnMode
Task<IList<MerchandiseReturnModel.MerchandiseReturnNote>> PrepareMerchandiseReturnNotes(
MerchandiseReturn merchandiseReturn);

Task InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, string downloadId,
Task<bool> InsertMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, Order order, string downloadId,
bool displayToCustomer, string message);

Task DeleteMerchandiseReturnNote(MerchandiseReturn merchandiseReturn, string id);
Expand Down
Loading
Loading