Skip to content

Commit

Permalink
Orders: init and create method
Browse files Browse the repository at this point in the history
see #5
  • Loading branch information
maxkoryukov committed Feb 12, 2017
1 parent eaaf456 commit fd9c9e1
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/resources/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"use strict"

const debug = require("debug")("route4me")

const utils = require("./../utils")
const errors = require("./../errors")

class CustomInternalPostProcessing {
/**
* Handle `duplicate` output
*
* @private
*
* @example <caption>Expected input</caption>
* Sample = {
* "optimization_problem_id":"672998C4269918AFF461E5A691BAB8D0",
* "success":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {string} - The ID of duplicate
*/
static duplicate(data, ctx, res) {
if (
!data
|| "boolean" !== typeof data["success"]
|| "string" !== typeof data["optimization_problem_id"]
) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data["success"]) {
return data["optimization_problem_id"]
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}
}

// ===================================

/**
* @namespace
*/
class Orders {
/**
* Orders facility
*
* @see {@link https://route4me.io/docs/#orders}
* @since 0.1.11
* @private
*
* @param {Route4Me} route4me - Route4Me manager
* @return {Orders} - Orders facility
*/
constructor(route4me) {
this.r = route4me
}

/**
* Create an Order
*
* @example <caption>Sample input</caption>
*
* {
* "address_1": "1358 E Luzerne St, Philadelphia, PA 19124, US",
* "cached_lat" : 48.335991,
* "cached_lng" : 31.18287,
* "address_alias" : "Auto test address",
* "address_city" : "Philadelphia",
* "EXT_FIELD_first_name" : "Igor",
* "EXT_FIELD_last_name" : "Progman",
* "EXT_FIELD_email" : "[email protected]",
* "EXT_FIELD_phone" : "380380380380",
* "EXT_FIELD_custom_data" : [
* {
* "order_id" : "10",
* "name" : "Bill Soul"
* }
* ]
* }
*
* @see {@link https://route4me.io/docs/#create-an-order}
* @category Orders
* @since 0.1.11
*
* @todo TODO: use custom input format (well formatted)
*
* @param {jsonschema:Orders.OrderInput} data - New order
* @param {module:route4me-node~RequestCallback<jsonschema:Orders.Order>} [callback]
*/
create(data, callback) {
const body = data // convert data

return this.r._makeRequest({
method: "POST",
path: "/api.v4/order.php",
body,
validationContext: "Orders.Order",
}, callback)
}
}

module.exports = Orders
6 changes: 6 additions & 0 deletions src/route4me.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Geocoding = require("./resources/geocoding")
const Members = require("./resources/members")
const Notes = require("./resources/notes")
const Optimizations = require("./resources/optimizations")
const Orders = require("./resources/orders")
const Routes = require("./resources/routes")
const Territories = require("./resources/territories")
const Tracking = require("./resources/tracking")
Expand Down Expand Up @@ -122,6 +123,11 @@ class Route4Me {
* @type {Optimizations}
*/
this.Optimizations = new Optimizations(this)
/**
* **Orders** related API calls
* @type {Orders}
*/
this.Orders = new Orders(this)
/**
* **Routes** related API calls
* @type {Routes}
Expand Down
79 changes: 79 additions & 0 deletions test/resources/orders.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"use strict"

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

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

const Route4Me = require("../../src/route4me")

const testApiKey = "11111111111111111111111111111111"


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

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

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

const data = {
"address_1": "1358 E Luzerne St, Philadelphia, PA 19124, US",
"cached_lat" : 48.335991,
"cached_lng" : 31.18287,
"address_alias" : "Auto test address",
"address_city" : "Philadelphia",
"EXT_FIELD_first_name" : "Igor",
"EXT_FIELD_last_name" : "Progman",
"EXT_FIELD_email" : "[email protected]",
"EXT_FIELD_phone" : "380380380380",
"EXT_FIELD_custom_data" : [ {
"order_id" : "10",
"name" : "Bill Soul"
} ],
}

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

helper.expectRequest(req,
"POST", "https://route4me.com/api.v4/order.php",
{ },
{
"address_1": "1358 E Luzerne St, Philadelphia, PA 19124, US",
"cached_lat" : 48.335991,
"cached_lng" : 31.18287,
"address_alias" : "Auto test address",
"address_city" : "Philadelphia",
"EXT_FIELD_first_name" : "Igor",
"EXT_FIELD_last_name" : "Progman",
"EXT_FIELD_email" : "[email protected]",
"EXT_FIELD_phone" : "380380380380",
"EXT_FIELD_custom_data" : [ {
"order_id" : "10",
"name" : "Bill Soul"
} ],
}
)

done()
})
})
})

})
})

0 comments on commit fd9c9e1

Please sign in to comment.