Skip to content

Commit

Permalink
v1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Feb 21, 2022
1 parent ba3fa8c commit 75ea4d3
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dist/@fleetbase/sdk.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/@fleetbase/sdk.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/fleetbase.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/fleetbase.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/fleetbase.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/fleetbase.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/sdk",
"version": "1.2.1",
"version": "1.2.2",
"description": "Fleetbase JS & Node SDK",
"main": "dist/cjs/fleetbase.js",
"module": "dist/esm/fleetbase.js",
Expand Down
3 changes: 2 additions & 1 deletion src/fleetbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isNodeEnvironment, detectAdapter, isLatitude, isLongitude, Point, isRes
import { pluralize, singularize, classify, dasherize, camelize } from './utils/string';
import { extendStoreActions } from './store';
import { orderActions } from './resources/order';
import { driverActions } from './resources/driver';

/**
* // instance
Expand Down Expand Up @@ -61,7 +62,7 @@ export default class Fleetbase {
this.orders = new Store('order', this.adapter).extendActions(orderActions);
this.entities = new Store('entity', this.adapter);
this.places = new Store('place', this.adapter);
this.drivers = new Store('driver', this.adapter);
this.drivers = new Store('driver', this.adapter).extendActions(driverActions);
this.vehicles = new Store('vehicle', this.adapter);
this.vendors = new Store('vendor', this.adapter);
this.contacts = new Store('contact', this.adapter);
Expand Down
30 changes: 19 additions & 11 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { Place, Payload, Order } from './resources';
import { Contact, Driver, Entity, Order, Payload, Place, TrackingStatus, Vehicle, Vendor, Waypoint, Zone, ServiceArea, ServiceRate, ServiceQuote } from './resources';
import { BrowserAdapter, NodeAdapter, EmberJsAdapter } from './adapters';
import { pluralize, singularize } from './utils/string';

const resources = {
Place,
Contact,
Driver,
Entity,
Order,
Payload,
Order
Place,
TrackingStatus,
Vehicle,
Vendor,
Waypoint,
Zone,
ServiceArea,
ServiceRate,
ServiceQuote,
};

const adapters = {
BrowserAdapter,
NodeAdapter,
EmberJsAdapter
EmberJsAdapter,
};

class Resolver {
constructor () {
constructor() {
this.resources = resources;
this.adapters = adapters;

Expand All @@ -24,7 +35,7 @@ class Resolver {

lookup(type, className) {
const key = pluralize(type);
const params = [ ...arguments ].slice(2);
const params = [...arguments].slice(2);

if (!this[key]) {
throw new Error('Attempted to resolve invalid type');
Expand All @@ -38,11 +49,8 @@ class Resolver {
}
}

const lookup = function() {
const lookup = function () {
return new Resolver(...arguments);
};

export {
Resolver,
lookup
};
export { Resolver, lookup };
13 changes: 11 additions & 2 deletions src/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,22 @@ class Resource {
return this.update(dirtyAttributes);
}

/**
* Returns the resource meta if exists.
*
* @return {Object}
*/
get meta() {
return this.getAttribute('meta', {});
}

/**
* Returns the date instance resource was created.
*
* @return {Date}
*/
get createdAt() {
return this.hasAttribute('created_at') ? new Date(this.getAttribute('created_at')) : null;
return this.isAttributeFilled('created_at') ? new Date(this.getAttribute('created_at')) : null;
}

/**
Expand All @@ -210,7 +219,7 @@ class Resource {
* @return {Date}
*/
get updatedAt() {
return this.hasAttribute('updated_at') ? new Date(this.getAttribute('updated_at')) : null;
return this.isAttributeFilled('updated_at') ? new Date(this.getAttribute('updated_at')) : null;
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/resources/driver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
import Resource from '../resource';
import { StoreActions, isPhone, isEmail } from '../utils';

const driverActions = new StoreActions({
// const { error } = await fleetbase.drivers.login('+1 111-1111');
login: function (identity, password = null, attributes = {}) {
// handle phone number authentication
if (isPhone(identity)) {
return this.adapter.post('drivers/login-with-sms', { phone: identity });
}

if (!password) {
throw new Error('Login requires password!');
}

return this.adapter.post('drivers/login', { identity, password, ...attributes }).then(this.afterFetch.bind(this));
},

verifyCode: function (identity, code, attributes = {}) {
return this.adapter.post('drivers/verify-code', { identity, code, ...attributes }).then(this.afterFetch.bind(this));
},

retrieve: function (id) {
return this.findRecord(id);
},
});

class Driver extends Resource {
constructor(attributes = {}, adapter, options = {}) {
super(attributes, adapter, 'driver', options);
}

get token() {
return this.getAttribute('token');
}

get isOnline() {
return this.getAttribute('online') === true;
}

syncDevice(token) {
return this.adapter
.setHeaders({ 'Driver-Token': this.token })
.post('drivers/register-device', token)
.then(() => {
return this;
});
}
}

export default Driver;
export { driverActions };
107 changes: 102 additions & 5 deletions src/resources/order.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,119 @@
import Resource from '../resource';
import { StoreActions } from '../utils';
import { isValid as isValidDate } from 'date-fns';

const orderActions = new StoreActions({
getDistanceAndTime: function (id, options = {}) {
return this.adapter.get(`${this.namespace}/${id}/distance-and-time`, {}, options);
getDistanceAndTime: function (id, params = {}, options = {}) {
return this.adapter.get(`${this.namespace}/${id}/distance-and-time`, params, options);
},

getNextActivity: function (id, params = {}, options = {}) {
return this.adapter.get(`${this.namespace}/${id}/next-activity`, params, options);
},

dispatch: function (id, params = {}, options = {}) {
return this.adapter.post(`${this.namespace}/${id}/dispatch`, params, options).then(this.afterFetch.bind(this));
},

start: function (id,params = {}, options = {}) {
return this.adapter.post(`${this.namespace}/${id}/start`, params, options).then(this.afterFetch.bind(this));
},

updateActivity: function (id, params = {}, options = {}) {
return this.adapter.post(`${this.namespace}/${id}/update-activity`, params, options).then(this.afterFetch.bind(this));
},

complete: function (id, params = {}, options = {}) {
return this.adapter.post(`${this.namespace}/${id}/complete`, params, options).then(this.afterFetch.bind(this));
},

cancel: function (id, params = {}, options = {}) {
return this.adapter.delete(`${this.namespace}/${id}/cancel`, params, options).then(this.afterFetch.bind(this));
}
});

class Order extends Resource {
constructor(attributes = {}, adapter, options = {}) {
super(attributes, adapter, 'order', { actions: orderActions, ...options });
}

getDistanceAndTime() {
return this.store.getDistanceAndTime(this.id);
getDistanceAndTime(params = {}, options = {}) {
return this.store.getDistanceAndTime(this.id, params, options);
}

dispatch(params = {}, options = {}) {
return this.store.dispatch(this.id, params, options);
}

start(params = {}, options = {}) {
return this.store.start(this.id, params, options);
}

getNextActivity(params = {}, options = {}) {
return this.store.getNextActivity(this.id, params, options);
}

updateActivity(params = {}, options = {}) {
return this.store.updateActivity(this.id, params, options);
}

cancel(params = {}, options = {}) {
return this.store.cancel(this.id, params, options);
}

complete(params = {}, options = {}) {
return this.store.complete(this.id, params, options);
}

get isDispatched() {
return this.getAttribute('dispatched_at') !== null;
}

get isNotDispatched() {
return this.getAttribute('dispatched_at') == null;
}

get isStarted() {
return this.getAttribute('started_at') !== null;
}

get isNotStarted() {
return this.getAttribute('started_at') == null;
}

get isCompleted() {
return this.getAttribute('status') == 'completed';
}

get isCanceled() {
return this.getAttribute('status') == 'canceled';
}

get isEnroute() {
return this.getAttribute('status') == 'driver_enroute' || this.getAttribute('status') === 'enroute';
}

get isInProgress() {
return this.isStarted && !this.isCanceled && !this.isCompleted;
}

get scheduledAt() {
return this.isAttributeFilled('scheduled_at') ? new Date(this.getAttribute('scheduled_at')) : null;
}

get startedAt() {
return this.isAttributeFilled('started_at') ? new Date(this.getAttribute('started_at')) : null;
}

get dispatchedAt() {
return this.isAttributeFilled('dispatched_at') ? new Date(this.getAttribute('dispatched_at')) : null;
}

get status() {
return this.getAttribute('status');
}
}

export default Order;

export { orderActions };
export { orderActions };
4 changes: 4 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import isEmpty from './is-empty';
import isLatitude from './is-latitude';
import isLongitude from './is-longitude';
import isResource from './is-resource';
import isPhone from './is-phone';
import isEmail from './is-email';
import isNodeEnvironment from './is-node-environment';
import detectAdapter from './detect-adapter';
import uuid from './uuid';
Expand All @@ -20,6 +22,8 @@ export {
isLatitude,
isLongitude,
isResource,
isPhone,
isEmail,
uuid,
GoogleAddress,
StoreActions,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/is-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isEmail = (email = '') => {
return /\S+@\S+\.\S+/.test(email);
}

export default isEmail;
5 changes: 5 additions & 0 deletions src/utils/is-phone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isPhone = (phone = '') => {
return /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/g.test(phone);
}

export default isPhone;

0 comments on commit 75ea4d3

Please sign in to comment.