Skip to content

Commit

Permalink
Merge pull request #21 from jordanwalsh23/add_reports_endpoint_support
Browse files Browse the repository at this point in the history
Add reports endpoint support and refactor directories.
  • Loading branch information
jordanwalsh23 committed Mar 15, 2017
2 parents 16c5b96 + bf11e67 commit a321c23
Show file tree
Hide file tree
Showing 48 changed files with 834 additions and 527 deletions.
2 changes: 1 addition & 1 deletion lib/application.js
Expand Up @@ -267,7 +267,7 @@ Object.assign(Application.prototype, {
function getResource(offset) {
var endPointUrl = options.api === 'payroll' ? self.options.payrollAPIEndPointUrl : self.options.coreAPIEndPointUrl;
var url = self.options.baseUrl + endPointUrl + path;
var params = {};
var params = options.params || {};
if (offset) {
params[options.pager.paramName || 'page'] = offset;
if (options.other) {
Expand Down
5 changes: 3 additions & 2 deletions lib/core.js
Expand Up @@ -14,7 +14,8 @@ var HELPERS = {
trackingCategories: { file: 'trackingcategories' },
users: { file: 'users' },
payments: { file: 'payments' },
taxrates: { file: 'taxrates' }
taxrates: { file: 'taxrates' },
reports: { file: 'reports' }
};

function Core(application, options) {
Expand All @@ -23,7 +24,7 @@ function Core(application, options) {
this._application = application;

_.each(HELPERS, function(entityHelper, id) {
var instance = new(require('./entity_helpers/' + entityHelper.file))(application);
var instance = new(require('./entity_helpers/accounting/' + entityHelper.file))(application);
Object.defineProperty(self, id, {
get: function() { return instance }
})
Expand Down
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')
Entity = require('../entity'),
logger = require('../../logger')

var AccountSchema = new Entity.SchemaObject({
Code: { type: String, toObject: 'always' },
Expand Down
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
Entity = require('../entity'),
logger = require('../../logger'),
dateformat = require('dateformat'),
fs = require('fs')

Expand Down
@@ -1,9 +1,9 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
Entity = require('../entity'),
logger = require('../../logger'),
ContactSchema = require('./contact').ContactSchema,
Contact = require('./contact'),
LineItemSchema = require('./shared').LineItemSchema
LineItemSchema = require('../shared').LineItemSchema

var BankTransactionSchema = new Entity.SchemaObject({
Type: { type: String, toObject: 'always' },
Expand Down
@@ -1,9 +1,9 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
Entity = require('../entity'),
logger = require('../../logger'),
ContactSchema = require('./contact').ContactSchema,
Contact = require('./contact'),
LineItemSchema = require('./shared').LineItemSchema
LineItemSchema = require('../shared').LineItemSchema

var BankTransferSchema = new Entity.SchemaObject({
FromBankAccount: {
Expand Down
12 changes: 6 additions & 6 deletions lib/entities/contact.js → lib/entities/accounting/contact.js
@@ -1,10 +1,10 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
AddressSchema = require('./shared').AddressSchema,
PhoneSchema = require('./shared').PhoneSchema,
ContactPersonSchema = require('./shared').ContactPerson,
PaymentTermSchema = require('./shared').PaymentTermSchema,
Entity = require('../entity'),
logger = require('../../logger'),
AddressSchema = require('../shared').AddressSchema,
PhoneSchema = require('../shared').PhoneSchema,
ContactPersonSchema = require('../shared').ContactPerson,
PaymentTermSchema = require('../shared').PaymentTermSchema,
dateformat = require('dateformat')

var BatchPaymentSchema = new Entity.SchemaObject({
Expand Down
@@ -1,9 +1,9 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
Entity = require('../entity'),
logger = require('../../logger'),
ContactSchema = require('./contact').ContactSchema,
Contact = require('./contact'),
PaymentSchema = require('./shared').PaymentSchema
PaymentSchema = require('../shared').PaymentSchema

var TrackingCategoryOptionsSchema = new Entity.SchemaObject({
TrackingCategoryID: { type: String, toObject: 'hasValue' },
Expand Down
4 changes: 2 additions & 2 deletions lib/entities/item.js → lib/entities/accounting/item.js
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')
Entity = require('../entity'),
logger = require('../../logger');

var ItemDetailSchema = new Entity.SchemaObject({
UnitPrice: { type: Number, toObject: 'hasValue' },
Expand Down
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')
Entity = require('../entity'),
logger = require('../../logger');

var TrackingCategorySchema = new Entity.SchemaObject({
TrackingCategoryID: { type: String },
Expand Down
@@ -1,10 +1,10 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
AddressSchema = require('./shared').AddressSchema,
PhoneSchema = require('./shared').PhoneSchema,
ExternalLinkSchema = require('./shared').ExternalLinkSchema,
PaymentTermSchema = require('./shared').PaymentTermSchema
Entity = require('../entity'),
logger = require('../../logger'),
AddressSchema = require('../shared').AddressSchema,
PhoneSchema = require('../shared').PhoneSchema,
ExternalLinkSchema = require('../shared').ExternalLinkSchema,
PaymentTermSchema = require('../shared').PaymentTermSchema

var OrganisationSchema = new Entity.SchemaObject({
APIKey: { type: String },
Expand Down
@@ -1,7 +1,7 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
PaymentSchema = require('./shared').PaymentSchema
Entity = require('../entity'),
logger = require('../../logger'),
PaymentSchema = require('../shared').PaymentSchema

var Payment = Entity.extend(PaymentSchema, {
constructor: function(application, data, options) {
Expand Down
96 changes: 96 additions & 0 deletions lib/entities/accounting/report.js
@@ -0,0 +1,96 @@
var _ = require('lodash'),
Entity = require('../entity'),
logger = require('../../logger');

var ReportSchema = new Entity.SchemaObject({
ReportID: { type: String, toObject: 'always' },
ReportName: { type: String, toObject: 'always' },
ReportType: { type: String, toObject: 'always' },
ReportTitles: { type: Array, arrayType: ReportTitleSchema, toObject: 'always' },
ReportDate: { type: String, toObject: 'always' },
UpdatedDateUTC: { type: String, toObject: 'always' },
Rows: { type: Array, arrayType: ReportRowSchema, toObject: 'always' }
});

var ReportTitleSchema = new Entity.SchemaObject({
ReportTitle: { type: String, toObject: 'always' }
});

var ReportRowSchema = new Entity.SchemaObject({
RowType: { type: String, toObject: 'always' },
Title: { type: String, toObject: 'always' },
Cells: { type: Array, arrayType: ReportCellSchema, toObject: 'always' }
});

var ReportCellSchema = new Entity.SchemaObject({
Value: { type: String, toObject: 'always' },
Attributes: { type: Array, arrayType: ReportAttributeSchema, toObject: 'always' }
});

var ReportAttributeSchema = new Entity.SchemaObject({
Value: { type: String, toObject: 'always' },
Id: { type: String, toObject: 'always' }
});

var Report = Entity.extend(ReportSchema, {
constructor: function(application, data, options) {
logger.debug('Report::constructor');
this.Entity.apply(this, arguments);
},
initialize: function(data, options) {},
fromXmlObj: function(obj) {
var self = this;
Object.assign(self, _.omit(obj, 'Rows', 'Cells', 'ReportTitles'));

if (obj.ReportTitles) {
var reportTitles = this.application.asArray(obj.ReportTitles.ReportTitle);
_.each(reportTitles, function(reportTitle) {
self.ReportTitles.push(reportTitle);
})
}

if (hasMoreRows(obj)) {
//Loop through the rows to find the next level down
obj.Rows.Row.forEach(function(level1Row) {

if (hasMoreRows(level1Row)) {
var level2RowsArray = [];
level1Row.Rows.Row.forEach(function(level2row) {
level2row.Cells ? level2row.Cells = extractCells(level2row) : false;
level2RowsArray.push(level2row);
});
level1Row.Rows = level2RowsArray;
}

level1Row.Cells ? level1Row.Cells = extractCells(level1Row) : false;
self.Rows.push(level1Row);
});
} else {
//No more rows, but we should check for cells in this row
if (obj.Rows && obj.Rows.Row && obj.Rows.Row.Cells && obj.Rows.Row.Cells.Cell && obj.Rows.Row.Cells.Cell.length > 0) {
obj.Rows.Row.Cells = extractCells(obj.Rows.Row);
self.Rows.push(obj.Rows.Row);
}
}

return this;

function hasMoreRows(obj) {
return obj.Rows && obj.Rows.Row && obj.Rows.Row.length >= 0;
}

function extractCells(row) {
var cells = [];
if (row.Cells) {
row.Cells.Cell.forEach(function(cell) {
cells.push(cell);
});
row.Cells = cells;
}
return cells;
}
}
});

module.exports = Report;
module.exports.ReportSchema = ReportSchema;
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger');
Entity = require('../entity'),
logger = require('../../logger');

var TaxRateSchema = new Entity.SchemaObject({
Name: { type: String, toObject: 'always' },
Expand Down
@@ -1,7 +1,7 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
TrackingOptionSchema = require('./shared').TrackingOptionSchema;
Entity = require('../entity'),
logger = require('../../logger'),
TrackingOptionSchema = require('../shared').TrackingOptionSchema;

var TrackingCategorySchema = new Entity.SchemaObject({
TrackingCategoryID: { type: String, toObject: 'always' },
Expand Down
53 changes: 53 additions & 0 deletions lib/entities/accounting/trackingoption.js
@@ -0,0 +1,53 @@
var _ = require('lodash'),
Entity = require('../entity'),
logger = require('../../logger'),
dateformat = require('dateformat'),
fs = require('fs')

var AttachmentSchema = new Entity.SchemaObject({
AttachmentID: { type: String, toObject: 'never' },
FileName: { type: String, toObject: 'always' },
Url: { type: String, toObject: 'always' },
MimeType: { type: String, toObject: 'always' },
ContentLength: { type: Number, toObject: 'always' }
});


var Attachment = Entity.extend(AttachmentSchema, {
constructor: function(application, data, options) {
logger.debug('Attachment::constructor');
this.Entity.apply(this, arguments);
},
initialize: function(data, options) {},
getContent: function(ownerPath) {
return this.application.core.attachments.getContent(ownerPath, this.FileName);
},
save: function(ownerPath, streamOrFilePath) {
var self = this;
var path = ownerPath + '/Attachments/' + this.FileName;

var base64string = base64_encode(streamOrFilePath);
console.log(base64string);
console.log(path);

return this.application.postEntity(path, base64string, { type: this.MimeType })
.then(function(ret) {
console.log(ret);
return ret.response.Attachments.Attachment;
})
.catch(function(err) {
console.log(err);
})

function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
}
});


module.exports = Attachment;
module.exports.AttachmentSchema = AttachmentSchema;
4 changes: 2 additions & 2 deletions lib/entities/user.js → lib/entities/accounting/user.js
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')
Entity = require('../entity'),
logger = require('../../logger');

var UserSchema = new Entity.SchemaObject({
UserID: {
Expand Down
4 changes: 2 additions & 2 deletions lib/entities/payitems.js → lib/entities/payroll/payitems.js
@@ -1,6 +1,6 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger')
Entity = require('../entity'),
logger = require('../../logger')

var EarningTypeSchema = new Entity.SchemaObject({
EarningsType: { type: String },
Expand Down
@@ -1,7 +1,7 @@
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
ExternalLinkSchema = require('./shared').ExternalLinkSchema
Entity = require('../entity'),
logger = require('../../logger'),
ExternalLinkSchema = require('../shared').ExternalLinkSchema

var AddressSchema = new Entity.SchemaObject({
StreetAddress: { type: String },
Expand Down

0 comments on commit a321c23

Please sign in to comment.