public async Task <Consignment> Create(ParcelLabelRequest request, CourierDetails courierDetails) { var token = await authService.GetToken(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); httpClient.DefaultRequestHeaders.Add("user_name", courierDetails.Username); request.Pickup_Address.Country_Code = courierSettings.CountryCode; request.Delivery_Address.Country_Code = courierSettings.CountryCode; request.Parcel_Details = new List <ParcelDetail> { new ParcelDetail(courierDetails.ServiceCode) }; var uriBuilder = new UriBuilder(courierSettings.ParcelLabelUrl); var jsonRequest = JsonConvert.SerializeObject(request, Formatting.None, serializerSettings); var jsonContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(uriBuilder.Uri, jsonContent); var result = response.Content.ReadAsStringAsync().Result; var contract = JsonConvert.DeserializeObject <ParcelLabelResponseContract>(result, serializerSettings); if (!response.IsSuccessStatusCode || string.IsNullOrEmpty(contract.Consignment_Id)) { logger.LogError($"Unsuccessful Parcel Label Create: {response.StatusCode}"); throw new ServiceException(response.StatusCode, $"Parcel Label Create Error - {contract.Errors[0].Details}"); } logger.LogDebug($"Parcel Label Create Job_Number:{request.Job_Number} Consignment_Id:{contract.Consignment_Id}"); // Include any errors/wanrnings related to parcel label create if (contract.Errors != null && contract.Errors.Count != 0) { logger.LogWarning($"Consignment_Id:{contract.Consignment_Id} Details:{contract.Errors[0].Details}"); return(new Consignment(contract.Consignment_Id, contract.Errors[0].Details)); } return(new Consignment(contract.Consignment_Id)); }
public async Task Process(CourierRequest request) { var consignment = new Consignment(); var courierDetails = new CourierDetails(); try { // Get Courier Details courierDetails = await courierDetailsService.Get(request.BranchId, request.DeliveryType); // Parcel Pickup var parcelPickupRequest = new ParcelPickupRequest { Carrier = request.Carrier, Caller = request.Caller, Service_Code = courierDetails.ServiceCode, Pickup_Date_Time = request.Parcel_Pickup_Date_Time, Parcel_Quantity = request.Parcel_Quantity, Pickup_Address = new PickupAddress { Phone = request.Parcel_Pickup_Address.Phone, Site_Code = courierDetails.SiteCode }, Delivery_Address = request.Parcel_Delivery_Address }; // Create a jobNumber var jobNumber = await parcelService.ParcelPickup(parcelPickupRequest, courierDetails); var parcelLabelRequest = new ParcelLabelRequest { Carrier = request.Carrier.ToUpper(), Logo_Id = courierDetails.LogoId, Job_Number = Convert.ToInt32(jobNumber), Sender_Details = request.Label_Sender_Details, Receiver_Details = request.Label_Receiver_Details, Pickup_Address = new PickAddress { Site_Code = Convert.ToInt32(courierDetails.SiteCode) }, Delivery_Address = request.Label_Delivery_Address }; // Create a label consignment = await labelService.Create(parcelLabelRequest, courierDetails); // Get the label ConsignmentURL consignment.ConsignmentURL = await labelService.GetStatus(consignment.ConsignmentId, courierDetails); // Update ACE with the ConsignmentUrl (NPLU) // await aceService.UpdateParcelLabel(request.BranchId.ToString(), request.FullOrderNumber, consignment, courierDetails.Username); // Setup Label is Ready notification var notificationRequest = new NotificationRequest { TransactionId = DateTime.Now.Ticks.ToString(), OrderNo = request.FullOrderNumber, BranchId = request.BranchId, BranchName = request.Caller, CustomerId = request.Label_Receiver_Details.Name, CustomerEmail = "*****@*****.**" }; // Send the notification await notificationService.Send(notificationRequest, courierDetails.Username, consignment.ConsignmentId); } catch (ServiceException ex) { // Update ACE of any consignment related errors consignment.Details = ex.Message; await aceService.UpdateParcelLabel(request.BranchId.ToString(), request.FullOrderNumber, consignment, courierDetails.Username); } catch (Exception ex) { // Log any other errors thrown by the service logger.LogError($"Something went wrong: {ex.Message}"); } }