Skip to content

Commit

Permalink
- Remove helper function for ship date estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed May 23, 2024
1 parent 601167a commit 3f13991
Showing 1 changed file with 0 additions and 76 deletions.
76 changes: 0 additions & 76 deletions EasyPost/Services/Beta/SmartRateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,82 +103,6 @@ public async Task<List<RateWithTimeInTransitDetailsByDeliveryDate>> RecommendShi
return await RequestAsync<List<RateWithTimeInTransitDetailsByDeliveryDate>>(Method.Get, $"shipments/{shipmentId}/smartrate/precision_shipping", cancellationToken, parameters.ToDictionary(), "rates", ApiVersion.Beta);
}

/// <summary>
/// Retrieve EasyPost's recommended ship date for a <see cref="Shipment"/> via the SmartRates API, based on a selected carrier, service and desired delivery date.
/// </summary>
/// <param name="shipmentId">The ID of the <see cref="Shipment"/> to get the recommended ship date for.</param>
/// <param name="carrier">The carrier to use for the <see cref="Shipment"/>.</param>
/// <param name="service">The service to use for the <see cref="Shipment"/>.</param>
/// <param name="arrivalDate">The desired delivery date.</param>
/// <param name="marginOfErrorForLateness">The maximum acceptable likelihood that the shipment will be late.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for the HTTP request.</param>
/// <returns>EasyPost's recommended ship date.</returns>
public async Task<DateTime?> WhenShouldShipToArriveBy(string shipmentId, string carrier, string service, string arrivalDate, float marginOfErrorForLateness = (float)0.1, CancellationToken cancellationToken = default)
{
// Get all the SmartRate for the shipment with recommended ship dates
List<RateWithTimeInTransitDetailsByDeliveryDate> rates = await RecommendShipDateByDeliveryDate(shipmentId, arrivalDate, cancellationToken);

// Find the rate(s) that matches the carrier and service
List<RateWithTimeInTransitDetailsByDeliveryDate> matchingRates = rates.FindAll(rate => rate.Rate!.Carrier == carrier && rate.Rate!.Service == service);

// If no matching rate is found, return null
if (matchingRates.Count == 0)
{
return null;
}

// If multiple matching rates are found, return the earliest ship date
DateTime? earliestShipDate = null;
foreach (RateWithTimeInTransitDetailsByDeliveryDate rate in matchingRates)
{
DateTime? shipDate = rate.TimeInTransitDetails!.EasyPostRecommendedShipDate;
float? likelihoodOfLateness = rate.TimeInTransitDetails!.LikelihoodShipmentIsLate;

// Skip if the likelihood of lateness is greater than the margin of error
if (likelihoodOfLateness > marginOfErrorForLateness)
{
continue;
}

// Update the earliest ship date if the current ship date is earlier than the current earliest ship date or if the current earliest ship date is null (first loop run)
if (earliestShipDate == null || shipDate < earliestShipDate)
{
earliestShipDate = shipDate;
}
}

// Return the earliest ship date, or null if no matching rate is found
return earliestShipDate;
}

/// <summary>
/// Retrieve EasyPost's recommended ship date for a <see cref="Shipment"/> via the SmartRates API, based on a <see cref="StatelessRate"/> and desired delivery date.
/// </summary>
/// <param name="shipmentId">The ID of the <see cref="Shipment"/> to get the recommended ship date for.</param>
/// <param name="rate">The <see cref="StatelessRate"/> to use for the <see cref="Shipment"/>.</param>
/// <param name="arrivalDate">The desired delivery date.</param>
/// <param name="marginOfErrorForLateness">The maximum acceptable likelihood that the shipment will be late.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for the HTTP request.</param>
/// <returns>EasyPost's recommended ship date.</returns>
public async Task<DateTime?> WhenShouldShipToArriveBy(string shipmentId, StatelessRate rate, string arrivalDate, float marginOfErrorForLateness = (float)0.1, CancellationToken cancellationToken = default)
{
return await WhenShouldShipToArriveBy(shipmentId, rate.Carrier!, rate.Service!, arrivalDate, marginOfErrorForLateness, cancellationToken);
}

/// <summary>
/// Retrieve EasyPost's recommended ship date for a <see cref="Shipment"/> via the SmartRates API, based on a <see cref="Rate"/> and desired delivery date.
/// </summary>
/// <param name="shipmentId">The ID of the <see cref="Shipment"/> to get the recommended ship date for.</param>
/// <param name="rate">The <see cref="Rate"/> to use for the <see cref="Shipment"/>.</param>
/// <param name="arrivalDate">The desired delivery date.</param>
/// <param name="marginOfErrorForLateness">The maximum acceptable likelihood that the shipment will be late.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for the HTTP request.</param>
/// <returns>EasyPost's recommended ship date.</returns>
public async Task<DateTime?> WhenShouldShipToArriveBy(string shipmentId, Rate rate, string arrivalDate, float marginOfErrorForLateness = (float)0.1, CancellationToken cancellationToken = default)
{
return await WhenShouldShipToArriveBy(shipmentId, rate.Carrier!, rate.Service!, arrivalDate, marginOfErrorForLateness, cancellationToken);
}

#endregion
}
}

0 comments on commit 3f13991

Please sign in to comment.