Skip to content

Commit

Permalink
Fix - Correction de l'update d'un transfer si c'est une donation
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 9, 2020
1 parent dc7d954 commit e7ac5b4
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions Sheaft.Application.Handlers/Commands/TransferCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,15 @@ public async Task<Result<TransactionStatus>> Handle(RefreshTransferStatusCommand
{
return await ExecuteAsync(request, async () =>
{
var transfer = await _context.GetSingleAsync<Transfer>(c => c.Identifier == request.Identifier, token);
if (transfer.Status == TransactionStatus.Succeeded || transfer.Status == TransactionStatus.Failed)
return Ok(transfer.Status);
var transfer = await _context.FindSingleAsync<Transfer>(c => c.Identifier == request.Identifier, token);
if(transfer != null)
return await HandleTransferStatusAsync(request, transfer, token);
var pspResult = await _pspService.GetTransferAsync(transfer.Identifier, token);
if (!pspResult.Success)
return Failed<TransactionStatus>(pspResult.Exception);
var donation = await _context.FindSingleAsync<Donation>(c => c.Identifier == request.Identifier, token);
if(donation != null)
return await HandleDonationStatusAsync(request, donation, token);
transfer.SetStatus(pspResult.Data.Status);
transfer.SetResult(pspResult.Data.ResultCode, pspResult.Data.ResultMessage);
transfer.SetExecutedOn(pspResult.Data.ProcessedOn);
await _context.SaveChangesAsync(token);
switch (transfer.Status)
{
case TransactionStatus.Failed:
_mediatr.Post(new TransferFailedEvent(request.RequestUser) { TransferId = transfer.Id });
break;
}
return Ok(transfer.Status);
return NotFound<TransactionStatus>();
});
}

Expand Down Expand Up @@ -155,6 +142,56 @@ public async Task<Result<Guid>> Handle(CreateTransferCommand request, Cancellati
});
}

private async Task<Result<TransactionStatus>> HandleTransferStatusAsync(RefreshTransferStatusCommand request, Transfer transfer, CancellationToken token)
{
if (transfer.Status == TransactionStatus.Succeeded || transfer.Status == TransactionStatus.Failed)
return Ok(transfer.Status);

var pspResult = await _pspService.GetTransferAsync(transfer.Identifier, token);
if (!pspResult.Success)
return Failed<TransactionStatus>(pspResult.Exception);

transfer.SetStatus(pspResult.Data.Status);
transfer.SetResult(pspResult.Data.ResultCode, pspResult.Data.ResultMessage);
transfer.SetExecutedOn(pspResult.Data.ProcessedOn);

await _context.SaveChangesAsync(token);

switch (transfer.Status)
{
case TransactionStatus.Failed:
_mediatr.Post(new TransferFailedEvent(request.RequestUser) { TransferId = transfer.Id });
break;
}

return Ok(transfer.Status);
}

private async Task<Result<TransactionStatus>> HandleDonationStatusAsync(RefreshTransferStatusCommand request, Donation donation, CancellationToken token)
{
if (donation.Status == TransactionStatus.Succeeded || donation.Status == TransactionStatus.Failed)
return Ok(donation.Status);

var pspResult = await _pspService.GetTransferAsync(donation.Identifier, token);
if (!pspResult.Success)
return Failed<TransactionStatus>(pspResult.Exception);

donation.SetStatus(pspResult.Data.Status);
donation.SetResult(pspResult.Data.ResultCode, pspResult.Data.ResultMessage);
donation.SetExecutedOn(pspResult.Data.ProcessedOn);

await _context.SaveChangesAsync(token);

switch (donation.Status)
{
case TransactionStatus.Failed:
_mediatr.Post(new DonationFailedEvent(request.RequestUser) { DonationId = donation.Id });
break;
}

return Ok(donation.Status);
}

private async Task<IEnumerable<Guid>> GetNextTransferIdsAsync(int skip, int take, CancellationToken token)
{
return await _context.Transfers
Expand Down

0 comments on commit e7ac5b4

Please sign in to comment.