Skip to content

Commit

Permalink
Merge pull request #63 from route4me/v1.0.4
Browse files Browse the repository at this point in the history
Added functions "archive" and "history" to Orders.
  • Loading branch information
r4m-juan committed Mar 24, 2022
2 parents 8fc4867 + fd1f73a commit 1c7f185
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 2 deletions.
35 changes: 35 additions & 0 deletions examples/Orders/get-archive-of-orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict"

const path = require("path")
const debug = require("debug")("route4me-node:examples")
const chai = require("chai");
require("../init-examples-suite")
const helper = require("../../test/helper")

helper.describeIntegration(helper.toSuiteName(__filename), function T() {
this.timeout(5000)
this.slow(3000)
var expect = chai.expect;
it(path.basename(__filename), (done) => {
const apiKey = "11111111111111111111111111111111"
const route4me = new Route4Me(apiKey)
const orderId = "2662207"

const data = {
cursor: "",
per_page: 10,
filters: []
};

route4me.Orders.archive(data, (err, orderResult) => {
debug("error ", err)
debug("result ", orderResult)

// Expectations about result
expect(err).is.null
expect(orderResult).exist
console.log(orderResult);
})
done()
})
})
30 changes: 30 additions & 0 deletions examples/Orders/get-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict"

const path = require("path")
const debug = require("debug")("route4me-node:examples")
const chai = require("chai");
require("../init-examples-suite")
const helper = require("../../test/helper")

helper.describeIntegration(helper.toSuiteName(__filename), function T() {
this.timeout(5000)
this.slow(3000)
var expect = chai.expect;
it(path.basename(__filename), (done) => {
const apiKey = "11111111111111111111111111111111"
const route4me = new Route4Me(apiKey)
const orderId = 11105107
const trackingNumber = "1"

route4me.Orders.history(orderId, trackingNumber, (err, orderResult) => {
debug("error ", err)
debug("result ", orderResult)

// Expectations about result
expect(err).is.null
expect(orderResult).exist
console.log(orderResult);
})
done()
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route4me-node",
"version": "1.0.3",
"version": "1.0.4",
"description": "Access Route4Me's logistics-as-a-service API using our Node.js SDK",
"main": "./dist/",
"browser": "./dist/browser/route4me.js",
Expand Down
29 changes: 28 additions & 1 deletion src/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class RequestManager {

this._apiKey = apiKey
this._baseUrl = opt["baseUrl"]
this._baseUrl5 = opt["baseUrl5"]
this._userAgent = opt["userAgent"]

this._logger = opt["logger"]
Expand Down Expand Up @@ -134,6 +135,7 @@ class RequestManager {

/**
* Wrapper around {@link external:superagent} with all options applied.
* Uses base url route4me API v4.0
*
* @todo TODO: rename this method!!!
* @protected
Expand Down Expand Up @@ -172,7 +174,7 @@ class RequestManager {

apiUrl = options.url
} else {
apiUrl = `${this._baseUrl}${options.path}`
apiUrl = (options["v5"] ? `${this._baseUrl5}${options.path}` : `${this._baseUrl}${options.path}`)
}

qs["api_key"] = this._apiKey
Expand Down Expand Up @@ -229,6 +231,31 @@ class RequestManager {
return resHandler.getPromise()
}

/**
* Wrapper around {@link external:superagent} with all options applied.
* Uses base url route4me API v5.0
*
* @todo TODO: rename this method!!!
* @protected
*
* @param {object} options Request options
* @param {string} options.method HTTP method
* @param {string} options.path Server path
* @param {object} [options.qs] Query string
* @param {object} [options.body] Body
* @param {null|string|function} [options.validationContext=null]
* * `null` cause validation disabled (TODO: test this case)
* * `string` is threated as the name of JSON Schema
* * `function` will be used for validation.
* @param {module:route4me-node~RequestCallback} [callback]
*/
_makeRequest5(options, callback) {
options["v5"] = true // eslint-disable-line
const res = this._makeRequest(options, callback)
options["v5"] = false // eslint-disable-line
return res
}

/**
* Early cancel request
*
Expand Down
53 changes: 53 additions & 0 deletions src/resources/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,59 @@ class Orders {
validationContext: "OrderCustomFields.Response",
}, callback)
}

/**
* Get all the archive orders created under the specific Route4Me account
*
* @see {@link https://route4me.io/docs}
* @since 1.0.4
*
* @param {Object} [data] - Archive params
* can have next fields
* cursor: string - id of next page of orders, empty string on first call
* per_page: integer - number of orders per page
* filters: object
*
* @param {module:route4me-node~RequestCallback<jsonschema:Orders.Orders>} [callback]
* [callback]
*/
archive(data, callback) {
const body = utils.clone(data)

return this.r._makeRequest5({
method: "POST",
path: "/api/v5.0/orders/archive",
qs: null,
body,
validationContext: "Orders.archive",
}, callback)
}

/**
* Get the orders history created under the specific Route4Me account
*
* @see {@link https://route4me.io/docs}
* @since 1.0.4
*
* @param {number} [orderId] - Order ID
* @param {string} [trackingNumber] - Tracking number
*
* @param {module:route4me-node~RequestCallback<jsonschema:Orders.Orders>} [callback]
* [callback]
*/
history(orderId, trackingNumber, callback) {
const qs = {
order_id: orderId,
tracking_number: trackingNumber
}

return this.r._makeRequest5({
method: "GET",
path: "/api/v5.0/orders/history",
qs,
validationContext: "Orders.history",
}, callback)
}
}

module.exports = Orders
1 change: 1 addition & 0 deletions src/route4me.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Route4Me {
// check options

opt["baseUrl"] = utils.get(options, "baseUrl", "https://api.route4me.com")
opt["baseUrl5"] = utils.get(options, "baseUrl", "https://wh.route4me.com/modules")
opt["logger"] = utils.get(options, "logger", new utils.ILogger())
opt["promise"] = utils.get(options, "promise", false)
opt["validate"] = utils.get(options, "validate", false)
Expand Down
63 changes: 63 additions & 0 deletions test/resources/orders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,68 @@ describe(helper.toSuiteName(__filename), () => {
})
})

describe("archive", () => {
beforeEach(() => {
saMock.post("*", (r) => {
req = r
req.method = "POST"
return { body: { } }
})
})

afterEach(() => {
saMock.clearRoutes()
})

it("archive should call route4me", (done) => {
resource.archive(null, (err, res) => {
expect(err).not.exist
expect(res).exist

helper.expectRequest(req,
"POST", "https://wh.route4me.com/modules/api/v5.0/orders/archive", {
}
)

done()
})
})
})

describe("history", () => {
beforeEach(() => {
saMock.get("*", (r) => {
req = r
req.method = "GET"
return { body: { } }
})
})

afterEach(() => {
saMock.clearRoutes()
})

const orderId = 11105107
const trackingNumber = "1"

it("history should call route4me", (done) => {
resource.history(orderId, trackingNumber, (err, res) => {
expect(err).not.exist
expect(res).exist

helper.expectRequest(req,
"GET", "https://wh.route4me.com/modules/api/v5.0/orders/history", {
"order_id": "11105107",
"tracking_number": "1"
},
null
)

done()
})
})
})

describe("createOrderCustomFields", () => {
beforeEach(() => {
saMock.post("*", (r) => {
Expand Down Expand Up @@ -433,6 +495,7 @@ describe(helper.toSuiteName(__filename), () => {
})
})
})

describe("updateOrderCustomFields", () => {
beforeEach(() => {
saMock.put("*", (r) => {
Expand Down

0 comments on commit 1c7f185

Please sign in to comment.