Skip to content

Commit

Permalink
Grand.Business.Messages.Commands - remove IHttpContextAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Oct 20, 2021
1 parent 341737a commit 9eaf2d8
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Grand.Domain.Data;
using Grand.Domain.Messages;
using MediatR;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,20 +11,16 @@ namespace Grand.Business.Messages.Commands.Handlers
public class InsertContactUsCommandHandler : IRequestHandler<InsertContactUsCommand, bool>
{
private readonly IRepository<ContactUs> _repository;
private readonly IHttpContextAccessor _httpContextAccessor;

public InsertContactUsCommandHandler(
IRepository<ContactUs> repository,
IHttpContextAccessor httpContextAccessor)
IRepository<ContactUs> repository)
{
_repository = repository;
_httpContextAccessor = httpContextAccessor;
}

public async Task<bool> Handle(InsertContactUsCommand request, CancellationToken cancellationToken)
{
var contactus = new ContactUs()
{
var contactus = new ContactUs() {
CreatedOnUtc = DateTime.UtcNow,
CustomerId = request.CustomerId,
StoreId = request.StoreId,
Expand All @@ -37,7 +32,7 @@ public async Task<bool> Handle(InsertContactUsCommand request, CancellationToken
EmailAccountId = request.EmailAccountId,
Attributes = request.ContactAttributes,
ContactAttributeDescription = request.ContactAttributeDescription,
IpAddress = _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString()
IpAddress = request.RemoteIpAddress
};

await _repository.InsertAsync(contactus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class InsertContactUsCommand : IRequest<bool>
public string FullName { get; set; }
public string Subject { get; set; }
public string Enquiry { get; set; }
public string RemoteIpAddress { get; set; }
public string ContactAttributeDescription { get; set; }
public IList<CustomAttribute> ContactAttributes { get; set; }
public string EmailAccountId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,9 @@ public partial interface IMessageProviderService
/// <summary>
/// Sends "email a friend" message
/// </summary>
/// <param name="customer">Customer instance</param>
/// <param name="store">Store</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="product">Product instance</param>
/// <param name="customerEmail">Customer's email</param>
/// <param name="friendsEmail">Friend's email</param>
/// <param name="personalMessage">Personal message</param>
/// <returns>Queued email identifier</returns>
Task<int> SendProductQuestionMessage(Customer customer, Store store, string languageId,
Product product, string customerEmail, string fullName, string phone, string message);
Product product, string customerEmail, string fullName, string phone, string message, string ipaddress);

#endregion

Expand Down Expand Up @@ -453,21 +446,24 @@ public partial interface IMessageProviderService
/// <param name="body">Email body</param>
/// <param name="attrInfo">Attr info</param>
/// <param name="customAttributes">Custom Attributes</param>
/// <param name="ipaddress">ip address</param>
/// <returns>Queued email identifier</returns>
Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail, string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes);
Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail, string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes, string ipaddress);

/// <summary>
/// Sends "contact vendor" message
/// </summary>
/// <param name="vendor">Vendor</param>
/// <param name="customer">Customer</param>
/// <param name="store">Store</param>
/// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <param name="ipaddress">Ip Address</param>
/// <returns>Queued email identifier</returns>
Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail, string senderName, string subject, string body);
Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail, string senderName, string subject, string body, string ipaddress);

/// <summary>
/// Sends a customer action event - Add to cart notification to a customer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,16 +828,9 @@ public virtual async Task<int> SendNewOrderNoteAddedCustomerMessage(Order order,
/// <summary>
/// Sends "email a friend" message
/// </summary>
/// <param name="customer">Customer instance</param>
/// <param name="store">Store</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="product">Product instance</param>
/// <param name="customerEmail">Customer's email</param>
/// <param name="friendsEmail">Friend's email</param>
/// <param name="personalMessage">Personal message</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendProductQuestionMessage(Customer customer, Store store, string languageId,
Product product, string customerEmail, string fullName, string phone, string message)
Product product, string customerEmail, string fullName, string phone, string message, string ipaddress)
{
if (customer == null)
throw new ArgumentNullException(nameof(customer));
Expand Down Expand Up @@ -881,7 +874,8 @@ public virtual async Task<int> SendNewOrderNoteAddedCustomerMessage(Order order,
Enquiry = bodyReplaced,
FullName = fullName,
Subject = subjectReplaced,
EmailAccountId = emailAccount.Id
EmailAccountId = emailAccount.Id,
RemoteIpAddress = ipaddress
});
}

Expand Down Expand Up @@ -1601,7 +1595,7 @@ public virtual async Task<int> SendBackinStockMessage(Customer customer, Product
/// <param name="customAttributes">CustomAttributes</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendContactUsMessage(Customer customer, Store store, string languageId, string senderEmail,
string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes)
string senderName, string subject, string body, string attrInfo, IList<CustomAttribute> customAttributes, string ipaddress)
{
var language = await EnsureLanguageIsActive(languageId, store.Id);
var messageTemplate = await GetMessageTemplate("Service.ContactUs", store.Id);
Expand Down Expand Up @@ -1652,7 +1646,8 @@ public virtual async Task<int> SendBackinStockMessage(Customer customer, Product
Subject = string.IsNullOrEmpty(subject) ? "Contact Us (form)" : subject,
ContactAttributeDescription = attrInfo,
ContactAttributes = customAttributes,
EmailAccountId = emailAccount.Id
EmailAccountId = emailAccount.Id,
RemoteIpAddress = ipaddress
});
}
return await SendNotification(messageTemplate, emailAccount, languageId, liquidObject, toEmail, toName,
Expand All @@ -1666,16 +1661,18 @@ public virtual async Task<int> SendBackinStockMessage(Customer customer, Product
/// <summary>
/// Sends "contact vendor" message
/// </summary>
/// <param name="vendor">Vendor</param>
/// <param name="customer">Customer</param>
/// <param name="store">Store</param>
/// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <param name="ipaddress">Ip address</param>
/// <returns>Queued email identifier</returns>
public virtual async Task<int> SendContactVendorMessage(Customer customer, Store store, Vendor vendor, string languageId, string senderEmail,
string senderName, string subject, string body)
string senderName, string subject, string body, string ipaddress)
{
if (vendor == null)
throw new ArgumentNullException(nameof(vendor));
Expand Down Expand Up @@ -1727,6 +1724,7 @@ public virtual async Task<int> SendBackinStockMessage(Customer customer, Product
VendorId = vendor.Id,
Email = senderEmail,
Enquiry = body,
RemoteIpAddress = ipaddress,
FullName = senderName,
Subject = String.IsNullOrEmpty(subject) ? "Contact Us (form)" : subject,
EmailAccountId = emailAccount.Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Grand.Domain.Common;
using Grand.Domain.Data;
using Grand.Domain.Messages;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
Expand All @@ -15,22 +14,18 @@ namespace Grand.Business.Messages.Tests.Commands
public class InsertContactUsCommandHandlerTests
{
private Mock<IRepository<ContactUs>> _repositoryMock;
private Mock<IHttpContextAccessor> _accessor;
private InsertContactUsCommandHandler _handler;

[TestInitialize]
public void Init()
{
_repositoryMock = new Mock<IRepository<ContactUs>>();
_accessor = new Mock<IHttpContextAccessor>();
_handler = new InsertContactUsCommandHandler(_repositoryMock.Object, _accessor.Object);
_handler = new InsertContactUsCommandHandler(_repositoryMock.Object);
}

[TestMethod]
public async Task Handler_InsertEntity()
{
var httpContextMock = new Mock<HttpContext>();
_accessor.Setup(c => c.HttpContext).Returns(httpContextMock.Object);
var command = new InsertContactUsCommand() {
ContactAttributeDescription = "d",
Email = "grand@gmail.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void Init()
_queuedEmailServiceMock.Object,
_languageServiceMock.Object,
_emailAccountServiceMock.Object,
_messageTokenProviderMock.Object,
_storeServiceMock.Object,
_storeHelperServiceMock.Object,
_groupService.Object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<(ContactUsModel model, IList<string> errors)> Handle(ContactUs
{
request.Model.ContactAttribute = attributes;
request.Model.ContactAttributeInfo = await _contactAttributeParser.FormatAttributes(_workContext.WorkingLanguage, attributes, _workContext.CurrentCustomer);
request.Model = await SendContactUs(request.Model, request.Store);
request.Model = await SendContactUs(request, request.Store);

//activity log
await _customerActivityService.InsertActivity("PublicStore.ContactUs", "", _translationService.GetResource("ActivityLog.PublicStore.ContactUs"));
Expand Down Expand Up @@ -408,17 +408,19 @@ private async Task<IList<ContactUsModel.ContactAttributeModel>> PrepareContactAt
return model;
}

private async Task<ContactUsModel> SendContactUs(ContactUsModel model, Store store)
private async Task<ContactUsModel> SendContactUs(ContactUsSendCommand request, Store store)
{
var subject = _commonSettings.SubjectFieldOnContactUsForm ? model.Subject : null;
var body = FormatText.ConvertText(model.Enquiry);
var subject = _commonSettings.SubjectFieldOnContactUsForm ? request.Model.Subject : null;
var body = FormatText.ConvertText(request.Model.Enquiry);

await _messageProviderService.SendContactUsMessage(_workContext.CurrentCustomer, store, _workContext.WorkingLanguage.Id, model.Email.Trim(), model.FullName, subject, body, model.ContactAttributeInfo, model.ContactAttribute);
await _messageProviderService.SendContactUsMessage
(_workContext.CurrentCustomer, store, _workContext.WorkingLanguage.Id, request.Model.Email.Trim(), request.Model.FullName, subject,
body, request.Model.ContactAttributeInfo, request.Model.ContactAttribute, request.IpAddress);

model.SuccessfullySent = true;
model.Result = _translationService.GetResource("ContactUs.YourEnquiryHasBeenSent");
request.Model.SuccessfullySent = true;
request.Model.Result = _translationService.GetResource("ContactUs.YourEnquiryHasBeenSent");

return model;
return request.Model;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<bool> Handle(SendProductAskQuestionMessageCommand request, Can
{
await _messageProviderService.SendProductQuestionMessage(request.Customer, request.Store,
request.Language.Id, request.Product, request.Model.Email, request.Model.FullName, request.Model.Phone,
FormatText.ConvertText(request.Model.Message));
FormatText.ConvertText(request.Model.Message), request.RemoteIpAddress);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<ContactVendorModel> Handle(ContactVendorSendCommand request, C
var body = FormatText.ConvertText(request.Model.Enquiry);

await _messageProviderService.SendContactVendorMessage(_workContext.CurrentCustomer, request.Store, request.Vendor, _workContext.WorkingLanguage.Id,
request.Model.Email.Trim(), request.Model.FullName, subject, body);
request.Model.Email.Trim(), request.Model.FullName, subject, body, request.IpAddress);

request.Model.SuccessfullySent = true;
request.Model.Result = _translationService.GetResource("ContactVendor.YourEnquiryHasBeenSent");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ContactUsSendCommand : IRequest<(ContactUsModel model, IList<string
public ContactUsModel Model { get; set; }
public IFormCollection Form { get; set; }
public bool CaptchaValid { get; set; }
public string IpAddress { get; set; }
public Store Store { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class SendProductAskQuestionMessageCommand : IRequest<bool>
public Language Language { get; set; }
public Product Product { get; set; }
public ProductAskQuestionModel Model { get; set; }
public string RemoteIpAddress { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ContactVendorSendCommand : IRequest<ContactVendorModel>
public Vendor Vendor { get; set; }
public Store Store { get; set; }
public ContactVendorModel Model { get; set; }
public string IpAddress { get; set; }

}
}
3 changes: 2 additions & 1 deletion src/Web/Grand.Web/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ public virtual async Task<IActionResult> SetTaxType(int customerTaxType, string
CaptchaValid = captchaValid,
Form = form,
Model = model,
Store = _workContext.CurrentStore
Store = _workContext.CurrentStore,
IpAddress = HttpContext?.Connection?.RemoteIpAddress?.ToString()
});

if (result.errors.Any())
Expand Down
6 changes: 4 additions & 2 deletions src/Web/Grand.Web/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ public virtual async Task<IActionResult> AskQuestion(ProductAskQuestionModel mod
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Model = model,
Product = product
Product = product,
RemoteIpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
});

//activity log
Expand Down Expand Up @@ -830,7 +831,8 @@ public virtual async Task<IActionResult> AskQuestionOnProduct(ProductAskQuestion
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Model = productaskqestionmodel,
Product = product
Product = product,
RemoteIpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
});

//activity log
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Grand.Web/Controllers/VendorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public virtual async Task<IActionResult> ContactVendor(ContactVendorModel model,

if (ModelState.IsValid)
{
model = await _mediator.Send(new ContactVendorSendCommand() { Model = model, Vendor = vendor, Store = _workContext.CurrentStore }); ;
model = await _mediator.Send(new ContactVendorSendCommand() { Model = model, Vendor = vendor, Store = _workContext.CurrentStore, IpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString() });
return View(model);
}

Expand Down

0 comments on commit 9eaf2d8

Please sign in to comment.