@fye/xservices-client
Version:
FYE Micros Xservices Client
214 lines (178 loc) • 4.46 kB
JavaScript
/*!
* twec-xservices-client
* Copyright(c) 2014-2020 F.Y.E
* Created by Nicholas Penree
*/
/**
* Module dependencies.
*/
var XService = require('./xservice');
var inherits = require('util').inherits;
/**
* Creates an instance of an `CustomerService`.
*
* @constructor
* @augments XService
* @this {CustomerService}
* @param {Object} opts
* @api public
*/
function CustomerService(opts, conns) {
XService.call(this, opts, conns);
this.serviceName = 'CustomerServices';
}
/**
* Inherit from `XService`.
*/
inherits(CustomerService, XService);
/**
* Transforms a raw customer, converting any required data types.
*
* @param {Object} customer
* @return {Object}
* @api private
*/
CustomerService.prototype._formatCustomer = function(customer) {
return this._convertFields({
obj: customer,
bools: [
'active',
'commercialCustomer',
'cellPhoneContact',
'emailContact',
'faxContact',
'homePhoneContact',
'mailingList',
'privacyCard',
'workPhoneContact'
],
dates: [ 'birthday' ]
});
};
/**
* Adds a new customer to the system and returns information about the new
* customer record
*
* Options:
* - `serviceContext` - required
* - `customerParty` - required
*
* @param {Object} opts
* @param {Function} fn
* @return {TransactionService}
* @api public
*/
CustomerService.prototype.addNewCustomer = function(opts, fn) {
this.invoke(this._locals(opts), function(err, res) {
if (err) return fn(err);
res = (res || {}).CustomerLookupResponse || {};
if (res.failureMessage) {
err = new Error(res.failureMessage);
err.code = res.failureCode;
err.res = res;
return fn(err, null);
}
var customer = res.customer;
if (customer) {
customer = this._formatCustomer(customer);
}
fn(null, customer);
}.bind(this));
return this;
};
/**
* Retrieves all customers that match a set of search criteria.
*
* Options:
* - `serviceContext` - required
* - `name` - optional
* - `phone` - optional
* - `customerNumberInputs` - optional
* - `customerInputType` - optional
* - `postalCode` - optional
*
* @param {Object} opts
* @param {Function} fn
* @return {TransactionService}
* @api public
*/
CustomerService.prototype.searchCustomers = function(opts, fn) {
this.invoke(this._locals(opts), function(err, res) {
if (err) return fn(err);
res = (res || {}).CustomerSearchResponse || {};
if (res.failureMessage) {
err = new Error(res.failureMessage);
err.code = res.failureCode;
err.res = res;
return fn(err, null);
}
if (res.customers && !Array.isArray(res.customers)) {
res.customers = [ res.customers ];
}
res.customers = (res.customers || []).map(function(customer) {
return this._formatCustomer(customer);
}, this);
fn(null, res.customers);
}.bind(this));
return this;
};
/**
* Retrieves customer information by the customer’s party ID.
*
* Options:
* - `serviceContext` - required
* - `partyId` - required
*
* @param {Object} opts
* @param {Function} fn
* @return {CustomerService}
* @api public
*/
CustomerService.prototype.getCustomerByParty = function(opts, fn) {
this.invoke(this._locals(opts), function(err, res) {
if (err) return fn(err);
res = (res || {}).CustomerLookupResponse || {};
if (res.failureMessage) {
err = new Error(res.failureMessage);
err.code = res.failureCode;
err.res = res;
return fn(err, null);
}
var customer = res.customer;
if (customer) {
customer = this._formatCustomer(customer);
}
fn(null, customer);
}.bind(this));
return this;
};
/**
* Retrieves the transaction history for a customer.
*
* Options:
* - `serviceContext` - required
* - `partyId` - required
*
* @param {Object} opts
* @param {Function} fn
* @return {CustomerService}
* @api public
*/
CustomerService.prototype.getCustomerHistory = function(opts, fn) {
this.invoke(this._locals(opts), function(err, res) {
if (err) return fn(err);
res = (res || {}).CustomerHistoryResponse || {};
if (res.failureMessage) {
err = new Error(res.failureMessage);
err.code = res.failureCode;
err.res = res;
return fn(err, null);
}
fn(null, res);
}.bind(this));
return this;
};
/**
* Expose `CustomerService`.
*/
module.exports = CustomerService;