|
1 | | -using System; |
2 | | -using System.Threading; |
3 | | -using System.Threading.Tasks; |
4 | | -using MediatR; |
| 1 | +using MediatR; |
5 | 2 | using Newtonsoft.Json; |
| 3 | +using Polly; |
6 | 4 | using Shared.DomainDrivenDesign.EventSourcing; |
7 | 5 | using Shared.EventStore.Aggregate; |
8 | 6 | using Shared.EventStore.EventHandling; |
9 | 7 | using Shared.Results; |
10 | 8 | using SimpleResults; |
| 9 | +using System; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
11 | 12 | using TransactionProcessor.Aggregates; |
| 13 | +using TransactionProcessor.BusinessLogic.Common; |
12 | 14 | using TransactionProcessor.BusinessLogic.Events; |
13 | 15 | using TransactionProcessor.BusinessLogic.Requests; |
14 | 16 | using TransactionProcessor.Models.Merchant; |
@@ -59,34 +61,27 @@ public async Task<Result> Handle(IDomainEvent domainEvent, |
59 | 61 | private async Task<Result> HandleSpecificDomainEvent(CallbackReceivedEnrichedEvent domainEvent, |
60 | 62 | CancellationToken cancellationToken) |
61 | 63 | { |
62 | | - if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString()) |
63 | | - { |
64 | | - // Work out the merchant id from the reference field (second part, split on hyphen) |
65 | | - String merchantReference = domainEvent.Reference.Split("-")[1]; |
66 | | - |
67 | | - Result<Merchant> result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken); |
68 | | - if (result.IsFailed) |
69 | | - return ResultHelpers.CreateFailure(result); |
70 | | - |
71 | | - // We now need to deserialise the message from the callback |
72 | | - CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject<CallbackHandler.DataTransferObjects.Deposit>(domainEvent.CallbackMessage); |
73 | | - |
74 | | - MerchantCommands.MakeMerchantDepositCommand command = new(domainEvent.EstateId, |
75 | | - result.Data.MerchantId, |
76 | | - DataTransferObjects.Requests.Merchant.MerchantDepositSource.Automatic, |
77 | | - new MakeMerchantDepositRequest |
78 | | - { |
79 | | - DepositDateTime = callbackMessage.DateTime, |
80 | | - Reference = callbackMessage.Reference, |
81 | | - Amount = callbackMessage.Amount, |
82 | | - }); |
83 | | - return await this.Mediator.Send(command, cancellationToken); |
84 | | - } |
85 | | - return Result.Success(); |
86 | | - } |
| 64 | + IAsyncPolicy<Result> retryPolicy = PolicyFactory.CreatePolicy(2, policyTag: "MerchantSettlementDomainEventHandler - MerchantFeeSettledEvent"); |
| 65 | + |
| 66 | + return await retryPolicy.ExecuteAsync(async () => { |
| 67 | + if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString()) { |
| 68 | + // Work out the merchant id from the reference field (second part, split on hyphen) |
| 69 | + String merchantReference = domainEvent.Reference.Split("-")[1]; |
87 | 70 |
|
| 71 | + Result<Merchant> result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken); |
| 72 | + if (result.IsFailed) |
| 73 | + return ResultHelpers.CreateFailure(result); |
88 | 74 |
|
| 75 | + // We now need to deserialise the message from the callback |
| 76 | + CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject<CallbackHandler.DataTransferObjects.Deposit>(domainEvent.CallbackMessage); |
89 | 77 |
|
| 78 | + MerchantCommands.MakeMerchantDepositCommand command = new(domainEvent.EstateId, result.Data.MerchantId, DataTransferObjects.Requests.Merchant.MerchantDepositSource.Automatic, new MakeMerchantDepositRequest { DepositDateTime = callbackMessage.DateTime, Reference = callbackMessage.Reference, Amount = callbackMessage.Amount, }); |
| 79 | + return await this.Mediator.Send(command, cancellationToken); |
| 80 | + } |
| 81 | + |
| 82 | + return Result.Success(); |
| 83 | + }); |
| 84 | + } |
90 | 85 |
|
91 | 86 | #endregion |
92 | 87 | } |
|
0 commit comments