public async Task <ActionResult <IEnumerable <GetAllUnpaidRequestOutput> > > GetAllUnpaidRequestAsync(CancellationToken cancellationToken) { // Log Entry. _logger.LogInformation((int)LoggingEvents.EntryMethod, "UnpaidsController.GetAllUnpaidRequestAsync called"); var getAllUnpaidRequestAsyncResult = await _unpaidRequestClient.GetAllUnpaidRequestAsync(cancellationToken); if (getAllUnpaidRequestAsyncResult == null) { return(BadRequest()); } if (!getAllUnpaidRequestAsyncResult.Any()) { return(BadRequest()); } return(Ok(getAllUnpaidRequestAsyncResult)); }
public async Task <bool> HandleUnpaidRequestAsync(IEnumerable <TbUnpaid> unpaids, string idempotencyKey, CancellationToken cancellationToken) { if (unpaids == null) { _logger.LogError((int)LoggingEvents.ValidationFailed, "UnpaidEngine.HandleUnpaidRequestAsync - unpaids is null"); return(false); } var isBatchSuccessful = false; foreach (var unpaid in unpaids) { // Get all pending UnpaidRequests by idempotencyKey var unpaidRequestsToUpdate = await _unpaidRequestClient.GetAllUnpaidRequestAsync(unpaid.UnpaidId, Status.Pending, cancellationToken); if (unpaidRequestsToUpdate == null) { _logger.LogWarning((int)LoggingEvents.GetItem, "UnpaidEngine.HandleUnpaidRequestAsync - _unpaidRequestClient.GetAllUnpaidRequestAsync returned null", new { BatchKey = idempotencyKey }); isBatchSuccessful = false; continue; } var singleUnpaidRequestToUpdate = unpaidRequestsToUpdate.FirstOrDefault(); if (singleUnpaidRequestToUpdate == null) { continue; } var correlationId = $"{idempotencyKey}_{singleUnpaidRequestToUpdate.UnpaidRequestId}"; // Send notification. var notificationResult = await _notification.SendAsync($"Dear {unpaid.Name}", unpaid.Message, unpaid.IdNumber, correlationId, cancellationToken);; var status = Status.Failed; if (notificationResult.StatusCode == HttpStatusCode.Accepted) { status = Status.Success; isBatchSuccessful = true; } // Update UnpaidRequest status. var updateUnpaidRequestResult = await _unpaidRequestClient.UpdateUnpaidRequestAsync(singleUnpaidRequestToUpdate.UnpaidRequestId, Notification.Push, status, notificationResult.AdditionalErrorMessage, DateTime.UtcNow, correlationId, cancellationToken); if (updateUnpaidRequestResult <= 0) { _logger.LogWarning((int)LoggingEvents.UpdateItem, "UnpaidEngine.HandleUnpaidRequestAsync - _unpaidRequestClient.UpdateUnpaidRequestAsync returned no results", new { BatchKey = idempotencyKey }); isBatchSuccessful = false; } } // Update Batch Status. var batchStatus = Status.Failed; if (isBatchSuccessful) { batchStatus = Status.Success; } var updateUnpaidBatchResult = await _unpaidBatchClient.UpdateUnpaidBatchAsync(idempotencyKey, batchStatus, DateTime.UtcNow, cancellationToken); if (updateUnpaidBatchResult <= 0) { _logger.LogWarning((int)LoggingEvents.UpdateItem, "UnpaidEngine.HandleUnpaidRequestAsync - _unpaidRequestClient.UpdateUnpaidBatchAsync returned no results", new { BatchKey = idempotencyKey }); isBatchSuccessful = false; } return(isBatchSuccessful); }