Skip to content

Commit a4cc229

Browse files
authored
[feat] Add new SmartRate "recommend ship date", "estimate delivery date" functions (#457)
- Add new `SmartRate` service - Add `Shipment.recommendShipDate`, `SmartRate.recommendShipDate`, and `SmartRate.estimateDeliveryDate` functions - Update/add unit tests, cassettes as needed - Update TypeScript types for new functions and class
1 parent 5fb49ca commit a4cc229

File tree

14 files changed

+1131
-320
lines changed

14 files changed

+1131
-320
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next Release
44

5+
- Adds new `Shipment.recommendShipDate`, `SmartRate.recommendShipDate`, and `SmartRate.estimateDeliveryDate` functions
56
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
67
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update) for more details
78

src/easypost.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import RefundService from './services/refund_service';
2929
import ReportService from './services/report_service';
3030
import ScanFormService from './services/scan_form_service';
3131
import ShipmentService from './services/shipment_service';
32+
import SmartRateService from './services/smart_rate_service';
3233
import TrackerService from './services/tracker_service';
3334
import UserService from './services/user_service';
3435
import WebhookService from './services/webhook_service';
@@ -106,6 +107,7 @@ export const SERVICES = {
106107
Report: ReportService,
107108
ScanForm: ScanFormService,
108109
Shipment: ShipmentService,
110+
SmartRate: SmartRateService,
109111
Tracker: TrackerService,
110112
User: UserService,
111113
Webhook: WebhookService,

src/services/shipment_service.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ export default (easypostClient) =>
238238
}
239239

240240
/**
241-
* Retrieves the estimated delivery date of each Rate via SmartRate.
242-
* @param {string} id
243-
* @param {string} plannedShipDate
241+
* Retrieve the estimated delivery date of each Rate via SmartRate.
242+
* @param {string} id - The ID of the shipment to retrieve the estimated delivery date for.
243+
* @param {string} plannedShipDate - The planned ship date of the shipment.
244244
* @returns {Array} - An array of the estimated delivery date and rates.
245245
*/
246246
static async retrieveEstimatedDeliveryDate(id, plannedShipDate) {
@@ -258,4 +258,26 @@ export default (easypostClient) =>
258258
return Promise.reject(e);
259259
}
260260
}
261+
262+
/**
263+
* Retrieve a recommended ship date for a {@link Shipment shipment} via the Precision Shipping API, based on a specific desired delivery date.
264+
* @param id - The ID of the shipment to retrieve the recommended ship date for.
265+
* @param desiredDeliveryDate - The desired delivery date for the shipment.
266+
* @returns {Array} - An array of the recommended ship date and rates.
267+
*/
268+
static async recommendShipDate(id, desiredDeliveryDate) {
269+
const url = `shipments/${id}/smartrate/precision_shipping`;
270+
271+
const params = {
272+
desired_delivery_date: desiredDeliveryDate,
273+
};
274+
275+
try {
276+
const response = await easypostClient._get(url, params);
277+
278+
return this._convertToEasyPostObject(response.body.rates ?? [], params);
279+
} catch (e) {
280+
return Promise.reject(e);
281+
}
282+
}
261283
};

src/services/smart_rate_service.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import baseService from './base_service';
2+
3+
export default (easypostClient) =>
4+
/**
5+
* The SmartRateService class provides methods for interacting with EasyPost SmartRate APIs.
6+
* @param {EasyPostClient} easypostClient - The pre-configured EasyPostClient instance to use for API requests with this service.
7+
*/
8+
class SmartRateService extends baseService(easypostClient) {
9+
/**
10+
* Retrieve the estimated delivery date of each carrier-service level combination via the Smart Deliver By API, based on a specific ship date and origin-destination postal code pair.
11+
* @param params - The parameters to estimate the delivery date with.
12+
* @returns {Object} - Estimates and related metadata.
13+
*/
14+
static async estimateDeliveryDate(params) {
15+
const url = 'smartrate/deliver_by';
16+
17+
try {
18+
const response = await easypostClient._post(url, params);
19+
20+
return this._convertToEasyPostObject(response.body, params);
21+
} catch (e) {
22+
return Promise.reject(e);
23+
}
24+
}
25+
26+
/**
27+
* Retrieve a recommended ship date for each carrier-service level combination via the Smart Deliver On API, based on a specific delivery date and origin-destination postal code pair.
28+
* @param params - The parameters to recommend the ship date with.
29+
* @returns {Object} - Recommendation and related metadata.
30+
*/
31+
static async recommendShipDate(params) {
32+
const url = 'smartrate/deliver_on';
33+
34+
try {
35+
const response = await easypostClient._post(url, params);
36+
37+
return this._convertToEasyPostObject(response.body, params);
38+
} catch (e) {
39+
return Promise.reject(e);
40+
}
41+
}
42+
};

test/cassettes/Shipment-Service_2987889512/retrieve-estimated-delivery-dates-for-each-of-the-Rates-of-a-shipment_2875126681/recording.har

Lines changed: 312 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cassettes/Shipment-Service_2987889512/retrieve-recommended-ship-dates-for-each-of-the-Rates-of-a-shipment_1470819408/recording.har

Lines changed: 312 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cassettes/Shipment-Service_2987889512/retrieve-time-in-transit-data-for-each-of-the-Rates-of-a-shipment_2266090777/recording.har

Lines changed: 0 additions & 312 deletions
This file was deleted.

test/cassettes/SmartRate-Service_1309568275/estimate-delivery-date_838626811/recording.har

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)