Skip to content

Commit

Permalink
updated various elements to have consistent responses on when save() …
Browse files Browse the repository at this point in the history
…is called. Updated Items tests to have 100% coverage.. woohoo
  • Loading branch information
Jordan Walsh committed Feb 22, 2017
1 parent b5b82ae commit 5a64852
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 123 deletions.
29 changes: 15 additions & 14 deletions lib/application.js
Expand Up @@ -168,22 +168,23 @@ _.extend(Application.prototype, {
return;
}

if (err) {
var errObj = { statusCode: err.statusCode, exception: obj.ApiException };
deferred.reject(errObj);
callback && callback(errObj);
return;
}

//Some delete operations don't return any content (e.g. HTTP204) so simply resolve the promise
if (!data || data === "") {
return deferred.resolve();
}

self.xml2js(data)
.then(function(obj) {
var ret = { response: obj.Response, res: res };
if (err) {
var errObj = { statusCode: err.statusCode, exception: obj.ApiException };
deferred.reject(errObj);
callback && callback(errObj);
return;
}

done();

function done() {
deferred.resolve(ret);
callback && callback(null, obj, res);
}
deferred.resolve(ret);
callback && callback(null, obj, res);
})
}, { stream: options.stream });

Expand Down Expand Up @@ -338,7 +339,7 @@ _.extend(Application.prototype, {
return this.delete(path, options)
.then(function(ret) {
if (ret && ret.response)
return ret;
return ret.response;
})

},
Expand Down
3 changes: 2 additions & 1 deletion lib/entities/account.js
Expand Up @@ -41,6 +41,7 @@ var Account = Entity.extend(AccountSchema, {
return this.application.core.attachments.getContent('Accounts/' + this.AccountID, fileName, options);
},
save: function(options) {
var self = this;
var xml = '<Accounts>' + this.toXml() + '</Accounts>';
var path, method;
if (this.AccountID) {
Expand All @@ -50,7 +51,7 @@ var Account = Entity.extend(AccountSchema, {
path = 'Accounts';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml)
return this.application.putOrPostEntity(method, path, xml, { entityPath: 'Accounts.Account', entityConstructor: function(data) { return self.application.core.accounts.newAccount(data) } })
}
});

Expand Down
13 changes: 11 additions & 2 deletions lib/entities/banktransaction.js
Expand Up @@ -70,9 +70,18 @@ var BankTransaction = Entity.extend(BankTransactionSchema, {
});
return this.application.js2xml(transaction, 'BankTransaction');
},
save: function(cb) {
save: function() {
var self = this;
var xml = '<BankTransactions>' + this.toXml() + '</BankTransactions>';
return this.application.putOrPostEntity('put', 'BankTransactions', xml, {}, cb);
var path, method;
if (this.BankTransactionID) {
path = 'BankTransactions/' + this.BankTransactionID;
method = 'post'
} else {
path = 'BankTransactions';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml, { entityPath: 'BankTransactions.BankTransaction', entityConstructor: function(data) { return self.application.core.bankTransactions.newBankTransaction(data) } })
}
});

Expand Down
7 changes: 5 additions & 2 deletions lib/entities/banktransfer.js
Expand Up @@ -54,9 +54,12 @@ var BankTransfer = Entity.extend(BankTransferSchema, {
transaction.ToBankAccount = this.ToBankAccount.toObject();
return this.application.js2xml(transaction, 'BankTransfer');
},
save: function(cb) {
save: function() {
var self = this;
var xml = '<BankTransfers>' + this.toXml() + '</BankTransfers>';
return this.application.putOrPostEntity('put', 'BankTransfers', xml, {}, cb);
//JWalsh 22 February 2017
//Only PUT is supported (beta), no POST..
return this.application.putOrPostEntity('put', 'BankTransfers', xml, { entityPath: 'BankTransfers.BankTransfer', entityConstructor: function(data) { return self.application.core.bankTransfers.newBankTransfer(data) } });
}
});

Expand Down
4 changes: 2 additions & 2 deletions lib/entities/item.js
Expand Up @@ -17,13 +17,13 @@ var ItemSchema = new Entity.SchemaObject({
IsPurchased: { type: Boolean, toObject: 'always' },
Description: { type: String, toObject: 'always' },
PurchaseDescription: { type: String, toObject: 'always' },
ItemID: { type: String, toObject: 'never' },
PurchaseDetails: { type: ItemDetailSchema, toObject: 'always' },
SalesDetails: { type: ItemDetailSchema, toObject: 'always' },
IsTrackedAsInventory: { type: Boolean, toObject: 'hasValue' },
TotalCostPool: { type: String, toObject: 'hasValue' },
QuantityOnHand: { type: String, toObject: 'hasValue' },
UpdatedDateUTC: { type: String, toObject: 'hasValue' }
UpdatedDateUTC: { type: String, toObject: 'hasValue' },
ItemID: { type: String, toObject: 'always' }
});


Expand Down
45 changes: 21 additions & 24 deletions lib/entity_helpers/items.js
@@ -1,38 +1,35 @@
var _ = require('lodash')
, logger = require('../logger')
, EntityHelper = require('./entity_helper')
, Item = require('../entities/item')
, p = require('../misc/promise')
, util = require('util')
var _ = require('lodash'),
logger = require('../logger'),
EntityHelper = require('./entity_helper'),
Item = require('../entities/item'),
p = require('../misc/promise'),
util = require('util')

var Items = EntityHelper.extend({
constructor: function (application, options)
{
EntityHelper.call(this, application, _.extend({ entityName:'Item', entityPlural:'Items'}, options));
constructor: function(application, options) {
EntityHelper.call(this, application, _.extend({ entityName: 'Item', entityPlural: 'Items' }, options));
},
newItem: function (data, options)
{
newItem: function(data, options) {
return new Item(this.application, data, options)
},
getItem: function (id, modifiedAfter)
{
return this.getItems({ id: id, modifiedAfter: modifiedAfter})
.then(function (items)
{
getItem: function(id, modifiedAfter) {
return this.getItems({ id: id, modifiedAfter: modifiedAfter })
.then(function(items) {
return _.first(items);
})
},
saveItems: function (items, options)
{
return this.saveEntities(items, options)
},
getItems: function (options)
{
getItems: function(options) {
var self = this;
var clonedOptions = _.clone(options || {});
clonedOptions.entityConstructor = function(data) { return self.newItem(data)};
clonedOptions.entityConstructor = function(data) { return self.newItem(data) };
return this.getEntities(clonedOptions)
},
deleteItem: function(id) {
var options = {
id: id
};
return this.deleteEntities(options);
}
})

module.exports = Items;
module.exports = Items;

0 comments on commit 5a64852

Please sign in to comment.