From 8a071ada4a544079a4cd289729a43af2990cddd2 Mon Sep 17 00:00:00 2001 From: fifteen Date: Mon, 21 Mar 2022 17:11:13 +0500 Subject: [PATCH] Added functions "get" and "save" to Address-barcodes. --- examples/Barcodes/get-barcodes.js | 32 +++++++ examples/Barcodes/save-barcodes.js | 50 +++++++++++ package.json | 2 +- src/resources/address-barcodes.js | 93 ++++++++++++++++++++ src/resources/orders.js | 7 +- src/route4me.js | 36 ++++---- test/resources/address-barcodes.spec.js | 107 ++++++++++++++++++++++++ 7 files changed, 308 insertions(+), 19 deletions(-) create mode 100644 examples/Barcodes/get-barcodes.js create mode 100644 examples/Barcodes/save-barcodes.js create mode 100644 src/resources/address-barcodes.js create mode 100644 test/resources/address-barcodes.spec.js diff --git a/examples/Barcodes/get-barcodes.js b/examples/Barcodes/get-barcodes.js new file mode 100644 index 0000000..93b08c3 --- /dev/null +++ b/examples/Barcodes/get-barcodes.js @@ -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() + }) +}) diff --git a/examples/Barcodes/save-barcodes.js b/examples/Barcodes/save-barcodes.js new file mode 100644 index 0000000..75f0635 --- /dev/null +++ b/examples/Barcodes/save-barcodes.js @@ -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() + }) +}) diff --git a/package.json b/package.json index d529da1..e3b33e2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/resources/address-barcodes.js b/src/resources/address-barcodes.js new file mode 100644 index 0000000..449d703 --- /dev/null +++ b/src/resources/address-barcodes.js @@ -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} [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} [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 diff --git a/src/resources/orders.js b/src/resources/orders.js index b9e561e..2a2aab5 100644 --- a/src/resources/orders.js +++ b/src/resources/orders.js @@ -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} [callback] * [callback] diff --git a/src/route4me.js b/src/route4me.js index 2b885db..d2677c8 100644 --- a/src/route4me.js +++ b/src/route4me.js @@ -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") @@ -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: @@ -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} diff --git a/test/resources/address-barcodes.spec.js b/test/resources/address-barcodes.spec.js new file mode 100644 index 0000000..dd3d0e0 --- /dev/null +++ b/test/resources/address-barcodes.spec.js @@ -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() + }) + }) + }) + }) +})