Skip to content

Commit

Permalink
Merge pull request #64 from route4me/v1.0.5
Browse files Browse the repository at this point in the history
Added functions "get" and "save" to Address Bar-codes.
  • Loading branch information
r4m-juan authored Mar 24, 2022
2 parents 1c7f185 + 8a071ad commit 14493b1
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 19 deletions.
32 changes: 32 additions & 0 deletions examples/Barcodes/get-barcodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict"

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

helper.describeIntegration(helper.toSuiteName(__filename), function T() {
this.timeout(5000)
this.slow(3000)
it(path.basename(__filename), (done) => {
// const Route4Me = require("route4me-node")
const expect = chai.expect
const apiKey = "11111111111111111111111111111111"
const route4me = new Route4Me(apiKey)

const routeId = "893E6C33F0494572DEB2FAE34B2D3E0B"
const routeDestinationId = 705601646
const limit = 10;

route4me.AddressBarcodes.get(routeId, routeDestinationId, limit, (err, res) => {
debug("error ", err)
debug("result ", res)
expect(err).is.null
expect(res).exist
console.log(res)
})
// TODO: remove `done` call from examples
done()
})
})
50 changes: 50 additions & 0 deletions examples/Barcodes/save-barcodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict"

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

helper.describeIntegration(helper.toSuiteName(__filename), function T() {
this.timeout(5000)
this.slow(3000)
it(path.basename(__filename), (done) => {
// const Route4Me = require("route4me-node")
const expect = chai.expect
const apiKey = "11111111111111111111111111111111"
const route4me = new Route4Me(apiKey)

const data = {
barcodes: [{
barcode: "some barcode",
lat: 1,
lng: 2,
timestamp_date: 3,
timestamp_utc: 4,
scanned_at: "2022-03-17 00:01:02",
scan_type: "QR-code"
}, {
barcode: "ye some barcode",
lat: 5,
lng: 6,
timestamp_date: 7,
timestamp_utc: 8,
scanned_at: "2022-03-18 01:02:03",
scan_type: "QR-code"
}],
route_destination_id: 1,
route_id: "C50594BD37618FA8B28EE6A86FEFD9D1"
};

route4me.AddressBarcodes.save(data, (err, res) => {
debug("error ", err)
debug("result ", res)
expect(err).is.null
expect(res).exist
console.log(res)
})
// TODO: remove `done` call from examples
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.4",
"version": "1.0.5",
"description": "Access Route4Me's logistics-as-a-service API using our Node.js SDK",
"main": "./dist/",
"browser": "./dist/browser/route4me.js",
Expand Down
93 changes: 93 additions & 0 deletions src/resources/address-barcodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"use strict"

/**
* AddressBarcodes facility
*
* @category AddressBarcodes
*/
class AddressBarcodes {
/**
* Constructor
*
* @see {@link https://route4me.io/docs/#address-barcode}
* @since 0.1.8
* @private
*
* @param {RequestManager} requestManager - Request Manager
* @return {AddressBarcodes} - AddressBarcodes facility
*/
constructor(requestManager) {
this.r = requestManager
}

/**
* Save a AddressBarcodes.
*
* @see {@link https://route4me.io/docs/#save-a-barcode}
* @since 1.0.5
*
* @param {object} data - Valid AddressBarcodes data.
* @param {string} data.route_id - route ID.
* @param {number} data.route_destination_id - route destination ID.
* @param {array} data.barcodes - array of barcode objects.
* @param {string} data.barcodes.barcode - barcode data.
* @param {number} data.barcodes.lat - latitude.
* @param {number} data.barcodes.lng - longitude.
* @param {number} data.barcodes.timestamp_date - local date.
* @param {number} data.barcodes.timestamp_utc - UTC date.
* @param {string} data.barcodes.scanned_at - scan date as string "Y-M-D H:M:S".
* @param {string} data.barcodes.scan_type - barcode scan type.
*
* @param {module:route4me-node~RequestCallback<jsonschema:
* AddressBarcodes.AddressBarcodes>} [callback]
*/
save(data, callback) {
return this.r._makeRequest5({
method: "POST",
path: "/api/v5.0/address-barcodes",
body: data,
validationContext: "Barcodes.save" // utils.CustomInternalPostProcessing.fromJsonWithStatus,
}, callback)
}

/**
* Get address barcodes by a specified route ID.
*
* @see {@link https://route4me.io/docs/#get-address-barcodes}
* @since 1.0.5
*
* @param {string} routeId - Route ID
* @param {number} routeDestinationId - Route destination ID
* @param {number} limit - Number of barcodes returning by request
* @param {string} cursor - Cursor ID, on first call must be null
*
* @param {module:route4me-node~RequestCallback<jsonschema:
* AddressBarcodes.AddressBookSearchResult>} [callback]
*/
get(routeId, routeDestinationId, limit, cursor, callback) {
const qs = {
"route_id": routeId,
"route_destination_id": routeDestinationId,
"limit": limit
}

let cb = callback
let cr = cursor

if (cb === undefined && "function" === typeof cr) {
cb = cr
cr = null
}

if (null !== cr && "" !== cr) qs["cursor"] = cr

return this.r._makeRequest5({
method: "GET",
path: "/api/v5.0/address-barcodes",
qs,
validationContext: "AddressBarcodes.AddressBarcodesGetResult",
}, cb)
}
}

module.exports = AddressBarcodes
7 changes: 3 additions & 4 deletions src/resources/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,9 @@ class Orders {
* @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 {string} [data.cursor] - id of next page of orders, empty string on first call
* @param {number} [datap.er_page] - number of orders per page
* @param {Object} [data.filters]
*
* @param {module:route4me-node~RequestCallback<jsonschema:Orders.Orders>} [callback]
* [callback]
Expand Down
36 changes: 22 additions & 14 deletions src/route4me.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const platform = require("platform")
const ActivityFeed = require("./resources/activity-feed")
const Addresses = require("./resources/addresses")
const AddressBook = require("./resources/address-book")
const AddressBarcodes = require("./resources/address-barcodes")
const AvoidanceZones = require("./resources/avoidance-zones")
const Geocoding = require("./resources/geocoding")
const Members = require("./resources/members")
Expand All @@ -32,20 +33,21 @@ const RequestManager = require("./request-manager")
*
* Main members of the instanse of `Route4Me` class:
*
* * [ActivityFeed ]{@link ActivityFeed}
* * [Addresses ]{@link Addresses}
* * [AddressBook ]{@link AddressBook}
* * [AvoidanceZones]{@link AvoidanceZones}
* * [Geocoding ]{@link Geocoding}
* * [Members ]{@link Members}
* * [Notes ]{@link Notes}
* * [Optimizations ]{@link Optimizations}
* * [Orders ]{@link Orders}
* * [OrderCustomFields ]{@link OrderCustomFields}
* * [Routes ]{@link Routes}
* * [Territories ]{@link Territories}
* * [Tracking ]{@link Tracking}
* * [Vehicles ]{@link Vehicles}
* * [ActivityFeed ]{@link ActivityFeed}
* * [Addresses ]{@link Addresses}
* * [AddressBook ]{@link AddressBook}
* * [AddressBarcodes ]{@link AddressBarcodes}
* * [AvoidanceZones ]{@link AvoidanceZones}
* * [Geocoding ]{@link Geocoding}
* * [Members ]{@link Members}
* * [Notes ]{@link Notes}
* * [Optimizations ]{@link Optimizations}
* * [Orders ]{@link Orders}
* * [OrderCustomFields]{@link OrderCustomFields}
* * [Routes ]{@link Routes}
* * [Territories ]{@link Territories}
* * [Tracking ]{@link Tracking}
* * [Vehicles ]{@link Vehicles}
*
* Each member corresponds to an bunch of methods, described in API-documentation,
* but the most methods in this SDK have unified names:
Expand Down Expand Up @@ -126,6 +128,12 @@ class Route4Me {
* @since 0.1.8
*/
this.AddressBook = new AddressBook(req)
/**
* **AddressBarcodes** related API calls
* @type {AddressBarcodes}
* @since 1.0.5
*/
this.AddressBarcodes = new AddressBarcodes(req)
/**
* **Addresses** related API calls
* @type {Addresses}
Expand Down
107 changes: 107 additions & 0 deletions test/resources/address-barcodes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"use strict"

const request = require("superagent")
const saMock = require("superagent-mocker")(request)

const helper = require("./../helper")
const route4me = require("./../../dist")

const testApiKey = "11111111111111111111111111111111"

describe(helper.toSuiteName(__filename), () => {
describe("SDK methods", () => {
const route4meClient = new route4me.Route4Me(testApiKey)
const resource = route4meClient.AddressBarcodes
let req

beforeEach(() => {
req = null
saMock.get("*", (r) => { req = r; req.method = "GET"; return {} })
saMock.post("*", (r) => { req = r; req.method = "POST"; return {} })
})

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

describe("get", () => {
const routeId = "C50594BD37618FA8B28EE6A86FEFD9D1"
const routeDestinationId = 2
const limit = 10
const cursor = "a"

it("should call route4me", (done) => {

resource.get(routeId, routeDestinationId, limit, cursor, (err, res) => {
expect(err).is.null
expect(res).is.not.null
helper.expectRequest(req,
"GET", "https://wh.route4me.com/modules/api/v5.0/address-barcodes",
{
"route_id": "C50594BD37618FA8B28EE6A86FEFD9D1",
"route_destination_id": "2",
"limit": "10",
"cursor": "a"
},
null
)
done()
})
})

it("should call route4me without cursor", (done) => {

resource.get(routeId, routeDestinationId, limit, (err, res) => {
expect(err).is.null
expect(res).is.not.null
helper.expectRequest(req,
"GET", "https://wh.route4me.com/modules/api/v5.0/address-barcodes",
{
"route_id": "C50594BD37618FA8B28EE6A86FEFD9D1",
"route_destination_id": "2",
"limit": "10"
},
null
)
done()
})
})
})

describe("save", () => {
const data = {
barcodes: [{
barcode: "some barcode",
lat: 1,
lng: 2,
timestamp_date: 3,
timestamp_utc: 4,
scanned_at: "2022-03-17 00:01:02",
scan_type: "QR-code"
}, {
barcode: "ye some barcode",
lat: 5,
lng: 6,
timestamp_date: 7,
timestamp_utc: 8,
scanned_at: "2022-03-18 01:02:03",
scan_type: "QR-code"
}],
route_destination_id: 1,
route_id: "C50594BD37618FA8B28EE6A86FEFD9D1"
};

it("should call route4me", (done) => {
resource.save(data, (err, res) => {
expect(err).is.null
expect(res).is.not.null
helper.expectRequest(req,
"POST", "https://wh.route4me.com/modules/api/v5.0/address-barcodes", {},
data
)
done()
})
})
})
})
})

0 comments on commit 14493b1

Please sign in to comment.