forked from route4me/route4me-nodejs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) | ||
|
||
}) | ||
}) |