Skip to content

Commit

Permalink
added support for tracking categories but tracking options is not cur…
Browse files Browse the repository at this point in the history
…rently working
  • Loading branch information
Jordan Walsh committed Feb 23, 2017
1 parent 882fedf commit 34ab49d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 55 deletions.
64 changes: 34 additions & 30 deletions lib/entities/trackingcategory.js
@@ -1,64 +1,68 @@
var _ = require('lodash')
, Entity = require('./entity')
, logger = require('../logger')
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')

var TrackingOptionSchema = new Entity.SchemaObject({
TrackingOptionID: {type: String},
Name: {type: String},
Status: {type: String}
TrackingOptionID: { type: String, toObject: 'always' },
Name: { type: String, toObject: 'always' },
Status: { type: String, toObject: 'always' }
});

var TrackingCategorySchema = new Entity.SchemaObject({
TrackingCategoryID: {type: String},
Name: {type: String},
Status: {type: String},
Options: {type: Array, arrayType: TrackingOptionSchema, toObject: 'always'}
TrackingCategoryID: { type: String, toObject: 'always' },
Name: { type: String, toObject: 'always' },
Status: { type: String, toObject: 'always' },
Options: { type: Array, arrayType: TrackingOptionSchema, toObject: 'always' }
});


var TrackingCategory = Entity.extend(TrackingCategorySchema, {
constructor: function (application, data, options)
{
constructor: function(application, data, options) {
logger.debug('TrackingCategory::constructor');
this.Entity.apply(this, arguments);
},
initialize: function (data, options)
{
},
changes: function (options)
{
initialize: function(data, options) {},
changes: function(options) {
return this._super(options);
},
_toObject: function (options)
{
_toObject: function(options) {
return this._super(options);
},
fromXmlObj: function (obj)
{
fromXmlObj: function(obj) {
var self = this;
_.extend(self, _.omit(obj, 'Options'));
if (obj.Options) {
_.each(this.application.asArray(obj.Options.Option), function (option)
{
_.each(this.application.asArray(obj.Options.Option), function(option) {
self.Options.push(option);
})
}

return this;
},
toXml: function ()
{
toXml: function() {
var trackingCategory = _.omit(this.toObject(), 'Options');
_.extend(trackingCategory, { Options: []});
_.forEach(this.Options, function(option)
{
trackingCategory.Options.push({ Option: option.toObject()})
_.extend(trackingCategory, { Options: [] });
_.forEach(this.Options, function(option) {
trackingCategory.Options.push({ Option: option.toObject() })
})

return this.application.js2xml(trackingCategory, 'TrackingCategory');
},
save: function(options) {
var self = this;
var xml = '<TrackingCategories>' + this.toXml() + '</TrackingCategories>';
var path, method;
if (this.TrackingCategoryID) {
path = 'TrackingCategories/' + this.TrackingCategoryID;
method = 'post'
} else {
path = 'TrackingCategories';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml, { entityPath: 'TrackingCategories.TrackingCategory', entityConstructor: function(data) { return self.application.core.trackingCategories.newTrackingCategory(data) } });
}
});


module.exports = TrackingCategory;
module.exports.TrackingCategorySchema = TrackingCategorySchema;
module.exports.TrackingCategorySchema = TrackingCategorySchema;
43 changes: 19 additions & 24 deletions lib/entity_helpers/trackingcategories.js
@@ -1,35 +1,30 @@
var _ = require('lodash')
, logger = require('../logger')
, EntityHelper = require('./entity_helper')
, TrackingCategory = require('../entities/trackingcategory')
, p = require('../misc/promise')
, util = require('util')
var _ = require('lodash'),
logger = require('../logger'),
EntityHelper = require('./entity_helper'),
TrackingCategory = require('../entities/trackingcategory'),
p = require('../misc/promise'),
util = require('util')

var TrackingCategorys = EntityHelper.extend({
constructor: function (application, options)
{
EntityHelper.call(this, application, _.extend({ entityName:'TrackingCategory', entityPlural:'TrackingCategories'}, options));
var TrackingCategories = EntityHelper.extend({
constructor: function(application, options) {
EntityHelper.call(this, application, _.extend({ entityName: 'TrackingCategory', entityPlural: 'TrackingCategories' }, options));
},
newTrackingCategory: function (data, options)
{
newTrackingCategory: function(data, options) {
return new TrackingCategory(this.application, data, options)
},
getTrackingCategory: function (id, where, order, includeArchived)
{
return this.getTrackingCategories({id: id, other: {includeArchived: includeArchived}, where: where, order: order})
.then(function (timesheets)
{
return _.first(timesheets);
getTrackingCategory: function(id, where, order, includeArchived) {
return this.getTrackingCategories({ id: id, other: { includeArchived: includeArchived }, where: where, order: order })
.then(function(trackingCategories) {
return _.first(trackingCategories);
})
},
getTrackingCategories: function (options)
{
getTrackingCategories: function(options) {
var self = this;
var clonedOptions = _.clone(options || {});
clonedOptions.entityConstructor = function(data) { return self.newTrackingCategory(data)};
return this.getEntities(clonedOptions)
clonedOptions.entityPath = 'TrackingCategories.TrackingCategory';
clonedOptions.entityConstructor = function(data) { return self.newTrackingCategory(data) };
return this.getEntities(clonedOptions);
}
})

module.exports = TrackingCategorys;

module.exports = TrackingCategories;
64 changes: 63 additions & 1 deletion test/accountingtests.js
Expand Up @@ -776,7 +776,69 @@ describe('regression tests', function() {

});

describe.skip('tracking categories', function() {});
describe('tracking categories', function() {
this.timeout(20000);

var sampleTrackingCategory = {
Name: "My First Category"
};

it('creates a tracking category', function(done) {

var myTrackingCategory = currentApp.core.trackingCategories.newTrackingCategory(sampleTrackingCategory);

myTrackingCategory.save()
.then(function(response) {
expect(response.entities).to.have.length.greaterThan(0);
expect(response.entities[0].TrackingCategoryID).to.not.equal("");
expect(response.entities[0].TrackingCategoryID).to.not.equal(undefined);
expect(response.entities[0].Name).to.equal(sampleTrackingCategory.Name);
sampleTrackingCategory = response.entities[0];
done();
})
.fail(function(err) {
console.log(util.inspect(err, null, null));
done(wrapError(err));
})
});

it('adds options to the tracking category', function(done) {

sampleTrackingCategory.Options = [{
Name: "Option 1"
}, {
Name: "Option 2"
}, {
Name: "Option 3"
}, {
Name: "Option 4"
}];

sampleTrackingCategory.save()
.then(function(response) {
expect(response.entities).to.have.length.greaterThan(0);
expect(response.entities[0].TrackingCategoryID).to.not.equal("");
expect(response.entities[0].TrackingCategoryID).to.not.equal(undefined);
expect(response.entities[0].Name).to.equal(sampleTrackingCategory.Name);

expect(response.entities[0].Options).to.have.length.greaterThan(0);

var count = 0;

_.each(response.entities[0].Options, function(trackingOption) {
expect(trackingOption.TrackingOptionID).to.not.equal("");
expect(trackingOption.TrackingOptionID).to.not.equal(undefined);
expect(trackingOption.Name).to.equal(sampleTrackingCategory.Options[count++].Name);
});

done();
})
.fail(function(err) {
console.log(util.inspect(err, null, null));
done(wrapError(err));
})
});
});

describe.skip('items', function() {
this.timeout(10000);
Expand Down

0 comments on commit 34ab49d

Please sign in to comment.