public async Task <IActionResult> Execute(string orderId, string subject) { if (string.IsNullOrWhiteSpace(orderId)) { throw new ArgumentNullException(nameof(orderId)); } if (string.IsNullOrWhiteSpace(subject)) { throw new ArgumentNullException(nameof(subject)); } var validationResult = await _validator.Validate(orderId, subject); if (!validationResult.IsValid) { var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message); return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error)); } var track = await _upsClient.Track(validationResult.Order.TrackingNumber, new UpsCredentials { LicenseNumber = _settingsProvider.GetUpsLicenseNumber(), Password = _settingsProvider.GetUpsPassword(), UserName = _settingsProvider.GetUpsUsername() } ); if (track.Response.ResponseStatusCode == "0") { var error = _responseBuilder.GetError(ErrorCodes.Request, track.Response.Error.ErrorDescription); return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error)); } return(new OkObjectResult(_responseBuilder.GetUpsTrack(track))); }
public async Task <ConfirmOrderReceptionValidationResult> Validate(string orderId, string subject) { if (string.IsNullOrWhiteSpace(orderId)) { throw new ArgumentNullException(nameof(orderId)); } if (string.IsNullOrWhiteSpace(subject)) { throw new ArgumentNullException(nameof(subject)); } var record = await _orderRepository.Get(orderId); if (record == null) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderDoesntExist)); } if (record.Subject != subject) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderReceptionCanBeConfirmedOnlyByTheBuyer)); } if (record.Status != OrderAggregateStatus.Confirmed) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderIsNotConfirmed)); } if (record.TransportMode == OrderTransportModes.Packet) { if (record.OrderPayment == null || record.OrderPayment.Status != OrderPaymentStatus.Approved) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderPaymentHasNotBeenConfirmed)); } if (!record.IsLabelPurchased || string.IsNullOrWhiteSpace(record.TrackingNumber)) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderLabelHasNotBeenPurchased)); } var trackResult = await _upsClient.Track(record.TrackingNumber, new UpsCredentials { LicenseNumber = _settingsProvider.GetUpsLicenseNumber(), Password = _settingsProvider.GetUpsPassword(), UserName = _settingsProvider.GetUpsUsername() }); if (trackResult.Response.ResponseStatusCode == "0") { return(new ConfirmOrderReceptionValidationResult(trackResult.Response.Error.ErrorDescription)); } if (trackResult.Shipment == null || trackResult.Shipment.Package == null || trackResult.Shipment.Package.Activity == null || !trackResult.Shipment.Package.Activity.Any(a => a.Status != null && a.Status.StatusType != null && a.Status.StatusType.Code == DeliveredCode)) { return(new ConfirmOrderReceptionValidationResult(ErrorDescriptions.TheOrderParcelHasNotBeenDelivered)); } } return(new ConfirmOrderReceptionValidationResult(record)); }