-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateCouponsBlock.cs
230 lines (196 loc) · 11 KB
/
CreateCouponsBlock.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Plugin.LoyaltyPoints.Entities;
using Plugin.LoyaltyPoints.Helpers;
using Plugin.LoyaltyPoints.Pipelines.Arguments;
using Plugin.LoyaltyPoints.Policies;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.Core.Commands;
using Sitecore.Commerce.Plugin.Coupons;
using Sitecore.Commerce.Plugin.ManagedLists;
using Sitecore.Commerce.Plugin.Promotions;
using Sitecore.Commerce.Plugin.SQL;
using Sitecore.Framework.Pipelines;
namespace Plugin.LoyaltyPoints.Pipelines.Blocks
{
/// <summary>
/// Ensures list <see cref="Constants.AvailableCouponsList"/> has number of coupons specified in <see cref="policy.ReprovisionTriggerCount"/>.
///
/// Note: By running update in a transaction, this block guards against race conditions. If two processors ran this minion, the
/// </summary>
class CreateCouponsBlock : PipelineBlock<CreateCouponsArgument, CreateCouponsArgument, CommercePipelineExecutionContext>
{
private readonly GetManagedListCommand _getManagedListCommand;
private readonly CreateManagedListCommand _createManagedListCommand;
private readonly AddListEntitiesPipeline _addListEntitiesPipeline;
private readonly GetListCountCommand _getListCountCommand;
private readonly FindEntityCommand _findEntityCommand;
private readonly DuplicatePromotionCommand _duplicatePromotionCommand;
private readonly AddPrivateCouponCommand _addPrivateCouponCommand;
private readonly NewCouponAllocationCommand _newCouponAllocationCommand;
private readonly PersistEntityCommand _persistEntityCommand;
private readonly GetEntitiesInListCommand _getEntitiesInListCommand;
public CreateCouponsBlock(
GetManagedListCommand getManagedListCommand,
CreateManagedListCommand createManagedListCommand,
GetListCountCommand getListCountCommand,
FindEntityCommand findEntityCommand,
DuplicatePromotionCommand duplicatePromotionCommand,
AddPrivateCouponCommand addPrivateCouponCommand,
NewCouponAllocationCommand newCouponAllocationCommand,
AddListEntitiesPipeline addListEntitiesPipeline,
PersistEntityCommand persistEntityCommand,
GetEntitiesInListCommand getEntitiesInListCommand
)
{
_getManagedListCommand = getManagedListCommand;
_createManagedListCommand = createManagedListCommand;
_getListCountCommand = getListCountCommand;
_findEntityCommand = findEntityCommand;
_duplicatePromotionCommand = duplicatePromotionCommand;
_addPrivateCouponCommand = addPrivateCouponCommand;
_newCouponAllocationCommand = newCouponAllocationCommand;
_addListEntitiesPipeline = addListEntitiesPipeline;
_persistEntityCommand = persistEntityCommand;
_getEntitiesInListCommand = getEntitiesInListCommand;
}
public override async Task<CreateCouponsArgument> Run(
CreateCouponsArgument arg,
CommercePipelineExecutionContext context)
{
await this._getManagedListCommand.PerformTransaction(context.CommerceContext, async () =>
{
var policy = context.CommerceContext.GetPolicy<LoyaltyPointsPolicy>();
LoyaltyPointsEntity loyaltyPointsEntity = await _findEntityCommand.Process(context.CommerceContext,typeof(LoyaltyPointsEntity),
Constants.EntityId,shouldCreate: true) as LoyaltyPointsEntity;
if (loyaltyPointsEntity == null)
{
await context.AbortWithError("Unable to access or create LoyaltyPointsEntity {0}",
"LoyaltyPointsEntityNotReturned", Constants.EntityId);
return;
}
// Prevent simultaneous updates, in case multiple minion instances. Since these might be on scaled servers, mutex lock uses database field.
// Lock is read before count is checked, so that when first process finishes and releases row, counts will be replenished.
// Notes:
// 1. If this pipeline aborts, the lock should be rolled back.
// 2. Assuming transactions are enabled, the second process should never see IsLocked=true, as the read won't return until the lock is released. However,
// this syntax should work on a non-transactional environment, and makes the intent of the code clearer.
if (loyaltyPointsEntity.IsLocked)
{
await context.AbortWithError("{0} is locked. If this condition persists, unset this value through the database.", "EntityLocked", Constants.EntityId);
return;
}
var list = await EnsureList(context, Constants.AvailableCouponsList);
if (list == null)
{
await context.AbortWithError("Unable to create list {0}", "UnableToCreateList", Constants.AvailableCouponsList);
return;
}
long count = await _getListCountCommand.Process(context.CommerceContext, Constants.AvailableCouponsList);
context.Logger.LogDebug($"{this.Name}: List {Constants.AvailableCouponsList} has {count} items.");
if (count <= policy.ReprovisionTriggerCount)
{
loyaltyPointsEntity.IsLocked = true;
await _persistEntityCommand.Process(context.CommerceContext, loyaltyPointsEntity);
context.Logger.LogDebug($"{this.Name}: List {Constants.AvailableCouponsList} is at or under reprovision count of {policy.ReprovisionTriggerCount}.");
loyaltyPointsEntity.SequenceNumber++;
string suffix = loyaltyPointsEntity.SequenceNumber.ToString();
string promotionName = string.Format(Constants.GeneratedPromotion, suffix);
Promotion promotion = await GeneratePromotion(context, promotionName);
if (promotion == null)
{
await context.AbortWithError("Unable to generate promotion {0}.", "PromotionNotFound",
promotionName);
return;
}
await AddCoupons(context, promotion, suffix);
if (context.IsNullOrHasErrors())
{
return;
}
await AllocateCoupons(context, promotion, suffix);
if (context.IsNullOrHasErrors())
{
return;
}
ApprovePromotion(context, promotion);
await CopyCouponsToList(context, loyaltyPointsEntity, list);
if (context.IsNullOrHasErrors())
{
return;
}
loyaltyPointsEntity.IsLocked = false;
await _persistEntityCommand.Process(context.CommerceContext, list);
await _persistEntityCommand.Process(context.CommerceContext, promotion);
await _persistEntityCommand.Process(context.CommerceContext, loyaltyPointsEntity);
}
});
return arg;
}
private async Task<ManagedList> EnsureList(CommercePipelineExecutionContext context, string listName)
{
ManagedList list = await _getManagedListCommand.Process(context.CommerceContext, listName);
if (list == null)
{
context.Logger.LogDebug($"{this.Name}: List {listName} not found. Creating it.");
list = await _createManagedListCommand.Process(context.CommerceContext, listName);
}
return list;
}
/// <summary>
/// Create a new promotion so that coupons can be provisioned.
/// Coupons cannot be added to an approved promotion.
/// </summary>
private async Task<Promotion> GeneratePromotion(CommercePipelineExecutionContext context, string promotionName)
{
string rootPromotion = context.GetPolicy<LoyaltyPointsPolicy>().TemplatePromotionFriendlyId;
Promotion promotion = await this._duplicatePromotionCommand.Process(context.CommerceContext, rootPromotion, promotionName);
if (context.IsNullOrHasErrors())
{
return null;
}
return promotion;
}
private async Task AddCoupons(CommercePipelineExecutionContext context, Promotion promotion, string suffix)
{
var policy = context.GetPolicy<LoyaltyPointsPolicy>();
await _addPrivateCouponCommand.Process(
context.CommerceContext,
promotion.Id,
policy.CouponPrefix,
suffix,
policy.CouponBlockSize);
}
/// <summary>
/// Coupons must be in allocated to be usable by customers.
/// </summary>
private async Task AllocateCoupons(CommercePipelineExecutionContext context, Promotion promotion, string suffix)
{
var policy = context.GetPolicy<LoyaltyPointsPolicy>();
string privateCouponGroupId = $"{CommerceEntity.IdPrefix<PrivateCouponGroup>()}{policy.CouponPrefix}-{suffix}";
var privateCouponGroup = await _findEntityCommand.Process(context.CommerceContext,
typeof(PrivateCouponGroup),
privateCouponGroupId) as PrivateCouponGroup;
if (privateCouponGroup == null)
{
await context.AbortWithError("Unable to find PrivateCouponGroup { 0}.", "PrivateCouponGroupNotFound", privateCouponGroupId);
return;
}
await _newCouponAllocationCommand.Process(context.CommerceContext, promotion, privateCouponGroup,
policy.CouponBlockSize);
}
private async Task CopyCouponsToList(CommercePipelineExecutionContext context, LoyaltyPointsEntity entity, ManagedList targetList)
{
var policy = context.GetPolicy<LoyaltyPointsPolicy>();
string sourceListName = $"promotion-{policy.CouponPrefix}-{entity.SequenceNumber}-allocatedcoupons";
var coupons = await _getEntitiesInListCommand.Process(context.CommerceContext,
sourceListName, 0,
policy.CouponBlockSize);
await _addListEntitiesPipeline.Run(new ListEntitiesArgument(coupons, targetList.Name), context);
}
private static void ApprovePromotion(CommercePipelineExecutionContext context, Promotion promotion)
{
promotion.SetComponent(new ApprovalComponent(context.GetPolicy<ApprovalStatusPolicy>().Approved));
}
}
}