route4me-node
Version:
Access Route4Me's logistics-as-a-service API using our Node.js SDK
239 lines (199 loc) • 6.69 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var utils = require("./../utils");
// const errors = require("./../errors")
// ===================================
/**
* Enum for all known **activity type**.
* @readonly
* @enum {string}
* @alias ActivityTypeEnum
* @category Route4Me
*/
var activityTypeEnum = {
/* eslint-disable key-spacing */
AreaAdded: "area-added",
AreaRemoved: "area-removed",
AreaUpdated: "area-updated",
GeofenceEntered: "geofence-entered",
GeofenceLeft: "geofence-left",
RouteDelete: "route-delete",
RouteOptimized: "route-optimized",
RouteOwnerChanged: "route-owner-changed",
DeleteDestination: "delete-destination",
DestinationOutSequence: "destination-out-sequence",
InsertDestination: "insert-destination",
MarkDestinationDeparted: "mark-destination-departed",
MarkDestinationVisited: "mark-destination-visited",
MoveDestination: "move-destination",
UpdateDestinations: "update-destinations",
/**
* Get driver arrived early activities
*
* {@link https://route4me.io/docs/#driver-arrived-early}
*
* @type {string}
*/
DriverArrivedEarly: "driver-arrived-early",
DriverArrivedLate: "driver-arrived-late",
DriverArrivedOnTime: "driver-arrived-on-time",
MemberCreated: "member-created",
MemberDeleted: "member-deleted",
MemberModified: "member-modified",
NoteInsert: "note-insert",
UserMessage: "user_message"
};
var _aliases = {};
Object.keys(activityTypeEnum).forEach(function (key) {
var v = activityTypeEnum[key];
_aliases[key] = v;
_aliases[v] = v;
});
var aliasedActivityTypeEnum = Object.freeze(_aliases);
/**
* ActivityFeed facility
*
* @category ActivityFeed
*/
var ActivityFeed = function () {
/**
* Constructor
*
* @see {@link https://route4me.io/docs/#activity-feed}
* @since 0.1.12
* @private
*
* @param {RequestManager} requestManager - Request Manager
* @return {ActivityFeed} - ActivityFeed facility
*/
function ActivityFeed(requestManager) {
_classCallCheck(this, ActivityFeed);
this.r = requestManager;
this.__activityTypeEnum = activityTypeEnum;
}
/**
* Log a Specific Message
*
* This example demonstrates how to permanently store a specific message
* directly to the activity feed. For example, this can be used for one or
* two-way chat.
*
* **The created activity will have `activityType === "user_message"`**
*
* @see {@link https://route4me.io/docs/#log-a-specific-message}
* @since 0.1.12
*
* @param {Object} data - Activity Feed item
* @param {string} data.routeId - Route ID
* @param {string} data.message - A message text for logging into the activity feed
* @param {module:route4me-node~RequestCallback} [callback]
*/
_createClass(ActivityFeed, [{
key: "create",
value: function create(data, callback) {
var body = {};
body["activity_type"] = "user_message";
body["activity_message"] = data["message"];
body["route_id"] = data["routeId"];
return this.r._makeRequest({
method: "POST",
path: "/api.v4/activity_feed.php",
body,
validationContext: utils.CustomInternalPostProcessing.fromJsonWithStatus
}, callback);
}
/**
* Enumerable of all known activity type
* @todo TODO: move to PACKAGE level (to make it easier for usage) see {@link https://github.com/route4me/route4me-nodejs-sdk/issues/40}
*/
}, {
key: "list",
/**
* Log a Specific Message
*
* This example demonstrates how to permanently store a specific message
* directly to the activity feed. For example, this can be used for one or
* two-way chat.
*
* @see {@link https://route4me.io/docs/#log-a-specific-message}
* @since 0.1.12
*
* @todo TODO: convert options to optional
*
* @param {string|Object} criteria - Criteria for event filter. Depending on type will be
* considered as:
* * `string` - criteria is a string representation of [Activity type]{@link ActivityTypeEnum}
* * `Object` - criteria is a set of filters, see below
*
* @param {string} [criteria.activityType] - [Activity type]{@link ActivityTypeEnum}
* @param {string} [criteria.routeId] - Route ID
*
* @param {Object} [options] - Options for activity search
* @param {number} [options.limit] - List limit
* @param {number} [options.offset] - List offset
* @param {boolean} [options.includeTeamActivities=false] - Indicate, whether team
* activities should be included
* @param {module:route4me-node~RequestCallback<jsonschema:ActivityFeed.ActivityFeedResult>}
* [callback]
*/
value: function list(criteria, options, callback) {
var qs = {};
var cri = criteria;
var opt = options;
var cb = callback;
// ARITY
if ("undefined" === typeof cb && "function" === typeof opt) {
// there are two params, and the second is CALLBACK
cb = opt;
opt = undefined;
}
// CRITERIA
if ("string" === typeof cri) {
cri = {
"activityType": cri
};
}
if (!utils.isObject(cri)) {
cri = {};
}
if ("activityType" in cri && cri["activityType"] in aliasedActivityTypeEnum) {
qs["activity_type"] = aliasedActivityTypeEnum[cri["activityType"]];
} else {
this.r.logger.debug({
src: "route4me:activity-feed:list",
msg: "ignore 'activity_type' filter"
});
}
if ("routeId" in cri) {
qs["route_id"] = cri["routeId"];
}
// OPTIONS
if (!utils.isObject(opt)) {
opt = {};
}
if ("offset" in opt) {
qs["offset"] = opt["offset"];
}
if ("limit" in opt) {
qs["limit"] = opt["limit"];
}
if (opt["includeTeamActivities"]) {
qs["team"] = "true";
}
return this.r._makeRequest({
method: "GET",
path: "/api/get_activities.php",
qs,
validationContext: "ActivityFeed.ActivityFeedResult"
}, cb);
}
}, {
key: "ActivityTypeEnum",
get: function get() {
return this.__activityTypeEnum;
}
}]);
return ActivityFeed;
}();
module.exports = ActivityFeed;
;