-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializeGiftcardsIndexingViewBlock.cs
More file actions
109 lines (101 loc) · 4.1 KB
/
InitializeGiftcardsIndexingViewBlock.cs
File metadata and controls
109 lines (101 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Linq;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.EntityViews;
using Sitecore.Commerce.Plugin.GiftCards;
using Sitecore.Commerce.Plugin.Search;
using Sitecore.Framework.Conditions;
using Sitecore.Framework.Pipelines;
namespace Sample.Commerce.Plugin.Index.Pipelines.Blocks
{
[PipelineDisplayName("InitializeGiftcardsIndexingViewBlock")]
public class InitializeGiftcardsIndexingViewBlock : SyncPipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
/// <summary>
/// Runs the specified argument.
/// </summary>
/// <param name="arg">The argument.</param>
/// <param name="context">The context.</param>
/// <returns>An <see cref="EntityView"/></returns>
public override EntityView Run(EntityView arg, CommercePipelineExecutionContext context)
{
Condition.Requires(arg).IsNotNull($"{Name}: argument cannot be null.");
var argument = context.CommerceContext.GetObjects<SearchIndexMinionArgument>().FirstOrDefault();
if (string.IsNullOrEmpty(argument?.Policy?.Name))
{
return arg;
}
var giftCards = argument.Entities?.OfType<GiftCard>().ToList();
if (giftCards == null || !giftCards.Any())
{
return arg;
}
var searchViewNames = context.GetPolicy<KnownSearchViewsPolicy>();
giftCards.ForEach(
giftCard =>
{
var documentView = arg.ChildViews.Cast<EntityView>()
.FirstOrDefault(v => v.EntityId.Equals(giftCard.Id, StringComparison.OrdinalIgnoreCase) && v.Name.Equals(searchViewNames.Document, StringComparison.OrdinalIgnoreCase));
if (documentView == null)
{
documentView = new EntityView
{
Name = context.GetPolicy<KnownSearchViewsPolicy>().Document,
EntityId = giftCard.Id
};
arg.ChildViews.Add(documentView);
}
AddIndexedProperties(context, documentView, giftCard);
});
return arg;
}
private void AddIndexedProperties(CommercePipelineExecutionContext context, EntityView documentView, GiftCard giftCard)
{
documentView.Properties.Add(new ViewProperty
{
Name = "EntityUniqueId",
RawValue = giftCard.UniqueId.ToString()
});
documentView.Properties.Add(new ViewProperty
{
Name = "EntityId",
RawValue = giftCard.Id
});
documentView.Properties.Add(new ViewProperty
{
Name = "GiftCardCode",
RawValue = giftCard.GiftCardCode
});
documentView.Properties.Add(new ViewProperty
{
Name = "ActivationDate",
RawValue = giftCard.ActivationDate
});
documentView.Properties.Add(new ViewProperty
{
Name = "Balance",
RawValue = giftCard.Balance.Amount
});
documentView.Properties.Add(new ViewProperty
{
Name = "OriginalAmount",
RawValue = giftCard.OriginalAmount.Amount
});
documentView.Properties.Add(new ViewProperty
{
Name = "DateCreated",
RawValue = giftCard.DateCreated
});
documentView.Properties.Add(new ViewProperty
{
Name = "DateUpdated",
RawValue = giftCard.DateUpdated
});
documentView.Properties.Add(new ViewProperty
{
Name = "ArtifactStoreId",
RawValue = context.CommerceContext.Environment.ArtifactStoreId
});
}
}
}