Skip to content

Commit

Permalink
Expand the number of logged information
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed Feb 8, 2017
1 parent 8020bbe commit ba15c6a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 54 deletions.
65 changes: 40 additions & 25 deletions VirtoCommerce.OrderModule.Data/Observers/LogOrderChangesObserver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoCompare;
using VirtoCommerce.Domain.Commerce.Model;
using VirtoCommerce.Domain.Customer.Model;
using VirtoCommerce.Domain.Customer.Services;
Expand All @@ -19,7 +20,17 @@ public sealed class LogOrderChangesObserver : IObserver<OrderChangeEvent>
{
private readonly IMemberService _memberService;
private readonly IChangeLogService _changeLogService;
public LogOrderChangesObserver(IChangeLogService changeLogService, IMemberService memberService)
private static string[] _observedProperties;
static LogOrderChangesObserver()
{
var operationPropNames = ReflectionUtility.GetPropertyNames<IOperation>(x => x.Status, x => x.Comment, x => x.Currency, x => x.Number, x => x.IsApproved);
var orderPropNames = ReflectionUtility.GetPropertyNames<CustomerOrder>(x => x.DiscountAmount, x => x.Total, x => x.Fee, x => x.Number, x => x.TaxPercentRate, x => x.TaxTotal, x => x.TaxType);
var shipmentPropNames = ReflectionUtility.GetPropertyNames<Shipment>(x => x.DiscountAmount, x => x.Total, x => x.Fee, x => x.Number, x => x.TaxPercentRate, x => x.TaxTotal, x => x.TaxType, x => x.Height, x => x.Length, x => x.MeasureUnit, x => x.Price, x => x.ShipmentMethodCode, x => x.ShipmentMethodOption, x => x.Weight, x => x.WeightUnit, x => x.Width);
var paymentPropNames = ReflectionUtility.GetPropertyNames<PaymentIn>(x => x.DiscountAmount, x => x.Total, x => x.Number, x => x.TaxPercentRate, x => x.TaxTotal, x => x.TaxType, x => x.OuterId, x => x.AuthorizedDate, x => x.CapturedDate, x => x.Price, x => x.GatewayCode, x => x.IncomingDate, x => x.Purpose, x => x.VoidedDate);

_observedProperties = operationPropNames.Concat(orderPropNames).Concat(shipmentPropNames).Concat(paymentPropNames).Distinct().ToArray();
}
public LogOrderChangesObserver(IChangeLogService changeLogService, IMemberService memberService, string[] observedProperties)
{
_changeLogService = changeLogService;
_memberService = memberService;
Expand All @@ -46,45 +57,50 @@ public void OnNext(OrderChangeEvent changeEvent)
var modifiedOperations = changeEvent.ModifiedOrder.GetFlatObjectsListWithInterface<IOperation>().Distinct();

modifiedOperations.ToList().CompareTo(originalOperations.ToList(), EqualityComparer<IOperation>.Default,
(state, source, target) => operationLogs.AddRange(GetOperationLogs(state, source, target)));
(state, modified, original) => operationLogs.AddRange(GetOperationLogs(state, original, modified)));

_changeLogService.SaveChanges(operationLogs.ToArray());
}
}
#endregion


private IEnumerable<OperationLog> GetOperationLogs(EntryState changeState, IOperation source, IOperation target)
private IEnumerable<OperationLog> GetOperationLogs(EntryState changeState, IOperation original, IOperation modified)
{
var retVal = new List<string>();
if (changeState == EntryState.Modified)
{
if (source.Status != target.Status)
var diff = Comparer.Compare(original, modified);

if (original is Shipment)
{
retVal.Add(string.Format(OrderResources.OperationStatusChanged, source.OperationType, source.Number, target.Status, source.Status));
retVal.AddRange(GetShipmentChanges(original as Shipment, modified as Shipment));
diff.AddRange(Comparer.Compare<Shipment>(original as Shipment, modified as Shipment));
}
if (source.Comment != target.Comment)
if (original is PaymentIn)
{
retVal.Add(string.Format(OrderResources.OperationCommentChanged, source.OperationType, source.Number));
retVal.AddRange(GetPaymentChanges(original as PaymentIn, modified as PaymentIn));
diff.AddRange(Comparer.Compare<PaymentIn>(original as PaymentIn, modified as PaymentIn));
}
if (source is CustomerOrder)
if (original is CustomerOrder)
{
retVal.AddRange(GetCustomerOrderChanges(target as CustomerOrder, source as CustomerOrder));
retVal.AddRange(GetCustomerOrderChanges(original as CustomerOrder, modified as CustomerOrder));
diff.AddRange(Comparer.Compare<CustomerOrder>(original as CustomerOrder, modified as CustomerOrder));
}
if (source is Shipment)
foreach (var change in diff.Join(_observedProperties, x => x.Name.ToLowerInvariant(), x => x.ToLowerInvariant(), (x, y) => x).Distinct())
{
retVal.AddRange(GetShipmentChanges(target as Shipment, source as Shipment));
retVal.Add(string.Format(OrderResources.OperationPropertyChanged, original.OperationType, modified.Number, change.Name, change.OldValue, change.NewValue));
}
}
else if (changeState == EntryState.Deleted)
{
retVal.Add(string.Format(OrderResources.OperationDeleted, target.OperationType, target.Number));
retVal.Add(string.Format(OrderResources.OperationDeleted, modified.OperationType, modified.Number));
}
else if (changeState == EntryState.Added)
{
retVal.Add(string.Format(OrderResources.OperationAdded, target.OperationType, target.Number));
retVal.Add(string.Format(OrderResources.OperationAdded, modified.OperationType, modified.Number));
}
return retVal.Select(x=> GetLogRecord(target, x));
return retVal.Select(x=> GetLogRecord(modified, x));
}

private string[] GetCustomerOrderChanges(CustomerOrder originalOrder, CustomerOrder modifiedOrder)
Expand All @@ -93,32 +109,31 @@ private string[] GetCustomerOrderChanges(CustomerOrder originalOrder, CustomerOr
if (originalOrder.EmployeeId != modifiedOrder.EmployeeId)
{
var employeeName = "none";
if (string.IsNullOrEmpty(modifiedOrder.EmployeeId))
if (!string.IsNullOrEmpty(modifiedOrder.EmployeeId))
{
var employee = _memberService.GetByIds(new[] { modifiedOrder.EmployeeId }).OfType<Employee>().FirstOrDefault();
employeeName = employee != null ? employee.FullName : employeeName;
}
retVal.Add(string.Format(OrderResources.OrderEmployeeChanged, employeeName));
}
if(originalOrder.Total != modifiedOrder.Total)
{
retVal.Add(string.Format(OrderResources.OperationTotalChanged, originalOrder.OperationType, originalOrder.Number, originalOrder.Total.ToString("0.00"), modifiedOrder.Total.ToString("0.00")));
}
retVal.AddRange(GetAddressChanges(originalOrder, originalOrder.Addresses, modifiedOrder.Addresses));
}
retVal.AddRange(GetAddressChanges(originalOrder, originalOrder.Addresses, modifiedOrder.Addresses));
return retVal.ToArray();
}

private string[] GetShipmentChanges(Shipment originalShipment, Shipment modifiedShipment)
{
var retVal = new List<string>();
if (originalShipment.Total != modifiedShipment.Total)
{
retVal.Add(string.Format(OrderResources.OperationTotalChanged, modifiedShipment.OperationType, modifiedShipment.Number, originalShipment.Total.ToString("0.00"), modifiedShipment.Total.ToString("0.00")));
}
retVal.AddRange(GetAddressChanges(originalShipment, new Address[] { originalShipment.DeliveryAddress }, new Address[] { modifiedShipment.DeliveryAddress }));
return retVal.ToArray();
}

private string[] GetPaymentChanges(PaymentIn payment, PaymentIn modifiedPayment)
{
var retVal = new List<string>();
retVal.AddRange(GetAddressChanges(payment, new Address[] { payment.BillingAddress }, new Address[] { modifiedPayment.BillingAddress }));
return retVal.ToArray();
}

private string[] GetAddressChanges(IOperation operation, IEnumerable<Address> originalAddress, IEnumerable<Address> modifiedAddress)
{
var retVal = new List<string>();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions VirtoCommerce.OrderModule.Data/Resources/OrderResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,11 @@
<data name="OperationAdded" xml:space="preserve">
<value>The new {0} {1} added</value>
</data>
<data name="OperationCommentChanged" xml:space="preserve">
<value>The comment changed for {0} {1}</value>
</data>
<data name="OperationDeleted" xml:space="preserve">
<value>The {0} {1} deleted</value>
</data>
<data name="OperationStatusChanged" xml:space="preserve">
<value>The {0} {1} status changed from '{2}' to '{3}' </value>
</data>
<data name="OperationTotalChanged" xml:space="preserve">
<value>The {0} {1} total changed from {2} to {3}</value>
<data name="OperationPropertyChanged" xml:space="preserve">
<value>The {0} {1} property '{2}' changed from '{3}' to '{4}'</value>
</data>
<data name="OrderEmployeeChanged" xml:space="preserve">
<value>Order employee was changed to '{0}'</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public virtual CustomerOrder[] GetByIds(string[] orderIds, string responseGroup
//Make general change log for order
order.OperationsLog = order.GetFlatObjectsListWithInterface<IHasChangesHistory>().Distinct()
.SelectMany(x => x.OperationsLog)
.OrderBy(x => x.CreatedDate)
.Distinct().ToList();
}
return retVal.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoCompare, Version=0.3.0.53, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoCompare.0.3.0.53\lib\net452\AutoCompare.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CacheManager.Core, Version=0.8.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CacheManager.Core.0.8.0\lib\net45\CacheManager.Core.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions VirtoCommerce.OrderModule.Data/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoCompare" version="0.3.0.53" targetFramework="net461" />
<package id="CacheManager.Core" version="0.8.0" targetFramework="net451" />
<package id="Common.Logging" version="3.3.1" targetFramework="net461" />
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net461" />
Expand Down

0 comments on commit ba15c6a

Please sign in to comment.