Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft delete support for sync OrderStatus entity #5

Draft
wants to merge 2 commits into
base: v12/dev
Choose a base branch
from
Draft
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
31 changes: 31 additions & 0 deletions src/uSync.Umbraco.Commerce/Serializers/OrderStatusSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;
using System.Xml.Linq;
using Umbraco.Commerce.Common;
using Umbraco.Commerce.Core.Api;
Expand Down Expand Up @@ -62,6 +63,11 @@ protected override SyncAttempt<OrderStatusReadOnly> DeserializeCore(XElement nod

item.SetColor(node.Element(nameof(item.Color)).ValueOrDefault(item.Color));
item.SetSortOrder(node.Element(nameof(item.SortOrder)).ValueOrDefault(item.SortOrder));

if(item.IsDeleted)
{
RestoreSoftDelete(item);
}

_CommerceApi.SaveOrderStatus(item);
uow.Complete();
Expand All @@ -70,6 +76,15 @@ protected override SyncAttempt<OrderStatusReadOnly> DeserializeCore(XElement nod
}
}

private static void RestoreSoftDelete(OrderStatus item)
{
// NOTE: we have to use reflection until Umraco.Commerce resolve this thread: https://github.com/umbraco/Umbraco.Commerce.Issues/discussions/522
var stateField = typeof(OrderStatus).GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
var state = (OrderStatusState)stateField.GetValue(item);
var deletedTimestampProperty = typeof(OrderStatusState).GetProperty(nameof(OrderStatusState.DeletedTimestamp), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
deletedTimestampProperty.SetValue(state, 0);
}

public override string GetItemAlias(OrderStatusReadOnly item)
=> item.Alias;

Expand All @@ -79,6 +94,22 @@ public override void DoDeleteItem(OrderStatusReadOnly item)
public override OrderStatusReadOnly DoFindItem(Guid key)
=> _CommerceApi.GetOrderStatus(key);

public override ChangeType IsCurrent(XElement node, SyncSerializerOptions options)
{
XElement current = null;
var item = FindItem(node);
if(item != null && !item.IsDeleted)
{
var attempt = Serialize(item, options);
if(attempt.Success)
{
current = attempt.Item;
}
}

return IsCurrent(node, current, options);
}

public override void DoSaveItem(OrderStatusReadOnly item)
{
using (var uow = _uowProvider.Create())
Expand Down