Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed the saveContacts method on the contacts object and did some ref…
…actoring. This concept could be applied across all endpoints. Also removed some console.log statments from the code
  • Loading branch information
Jordan Walsh committed Feb 22, 2017
1 parent efed46b commit 251b083
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 162 deletions.
2 changes: 0 additions & 2 deletions lib/application.js
Expand Up @@ -43,8 +43,6 @@ function Application(options) {
logger.debug('Application::constructor');
this.options = _.merge(_.clone(Application.defaults), options);

console.log(this.options);

this.init();

var core = new Core(this);
Expand Down
6 changes: 4 additions & 2 deletions lib/entities/contact.js
Expand Up @@ -140,7 +140,8 @@ var Contact = Entity.extend(ContactSchema, {
getAttachmentContent: function(fileName, options) {
return this.application.core.attachments.getContent('Contacts/' + this.ContactID, fileName, options);
},
save: function(options) {
save: function() {
var self = this;
var xml = '<Contacts>' + this.toXml() + '</Contacts>';
var path, method;
if (this.ContactID) {
Expand All @@ -150,7 +151,8 @@ var Contact = Entity.extend(ContactSchema, {
path = 'Contacts';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml)

return this.application.putOrPostEntity(method, path, xml, { entityPath: 'Contacts.Contact', entityConstructor: function(data) { return self.application.core.contacts.newContact(data) } })
}
});

Expand Down
191 changes: 89 additions & 102 deletions lib/entities/payroll_employee.js
@@ -1,96 +1,96 @@
var _ = require('lodash')
, Entity = require('./entity')
, logger = require('../logger')
, ExternalLinkSchema = require('./shared').ExternalLinkSchema
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
ExternalLinkSchema = require('./shared').ExternalLinkSchema

var AddressSchema = new Entity.SchemaObject({
StreetAddress: { type: String},
SuiteOrAptOrUnit: { type: String},
City: { type: String},
State: { type: String},
Zip: { type: String},
Lattitude: { type: String},
Longitude: { type: String}
StreetAddress: { type: String },
SuiteOrAptOrUnit: { type: String },
City: { type: String },
State: { type: String },
Zip: { type: String },
Lattitude: { type: String },
Longitude: { type: String }
});

var SalaryAndWageSchema = new Entity.SchemaObject({
SalaryAndWageId: { type: String},
EarningsTypeId: { type: String},
SalaryWagesType: { type: String},
HourlyRate: { type: Number},
AnnualSalary: { type: Number},
StandardHoursPerWeek: { type: Number}
SalaryAndWageId: { type: String },
EarningsTypeId: { type: String },
SalaryWagesType: { type: String },
HourlyRate: { type: Number },
AnnualSalary: { type: Number },
StandardHoursPerWeek: { type: Number }
});

var WorkLocationSchema = new Entity.SchemaObject({
WorkLocationID: { type: String},
IsPrimary: { type: Boolean}
WorkLocationID: { type: String },
IsPrimary: { type: Boolean }
});

var BankAccountSchema = new Entity.SchemaObject({
AccountHolderName: { type: String},
StatementText: {type: String},
AccountType: {type: String},
RoutingNumber: {type: String},
AccountNumber: {type: String},
Amount: {type: Number},
Remainder: {type: Boolean}
AccountHolderName: { type: String },
StatementText: { type: String },
AccountType: { type: String },
RoutingNumber: { type: String },
AccountNumber: { type: String },
Amount: { type: Number },
Remainder: { type: Boolean }
});

var EarningsLineSchema = new Entity.SchemaObject({
EarningsTypeID: { type: String},
UnitsOrHours: {type: Number},
RatePerUnit: {type: Number},
Amount: {type: Number}
EarningsTypeID: { type: String },
UnitsOrHours: { type: Number },
RatePerUnit: { type: Number },
Amount: { type: Number }
});

var DeductionLineSchema = new Entity.SchemaObject({
DeductionTypeID: { type: String},
CalculationType: {type: String},
EmployeeMax: {type: Number},
Percentage: {type: Number},
Amount: {type: Number}
DeductionTypeID: { type: String },
CalculationType: { type: String },
EmployeeMax: { type: Number },
Percentage: { type: Number },
Amount: { type: Number }
});

var ReimbursementLineSchema = new Entity.SchemaObject({
ReimbursementTypeID: { type: String},
Description: {type: String},
Amount: {type: Number}
ReimbursementTypeID: { type: String },
Description: { type: String },
Amount: { type: Number }
});

var BenefitLineSchema = new Entity.SchemaObject({
BenefitTypeID: { type: String},
CalculationType: {type: String},
Amount: {type: Number}
BenefitTypeID: { type: String },
CalculationType: { type: String },
Amount: { type: Number }
});


var PayTemplateSchema = Entity.SchemaObject({
EarningsLines: { type: Array, arrayType: EarningsLineSchema, toObject: 'always'},
DeductionLines: { type: Array, arrayType: DeductionLineSchema, toObject: 'always'},
ReimbursementLines: { type: Array, arrayType: ReimbursementLineSchema, toObject: 'always'},
BenefitLines: { type: Array, arrayType: BenefitLineSchema, toObject: 'always'}
EarningsLines: { type: Array, arrayType: EarningsLineSchema, toObject: 'always' },
DeductionLines: { type: Array, arrayType: DeductionLineSchema, toObject: 'always' },
ReimbursementLines: { type: Array, arrayType: ReimbursementLineSchema, toObject: 'always' },
BenefitLines: { type: Array, arrayType: BenefitLineSchema, toObject: 'always' }
});
var PaymentMethodSchema = new Entity.SchemaObject({
PaymentMethodType: { type: String},
BankAccounts: {type: Array, arrayType: BankAccountSchema, toObject: 'always'}
PaymentMethodType: { type: String },
BankAccounts: { type: Array, arrayType: BankAccountSchema, toObject: 'always' }
});

var OpeningBalanceEarningsLineSchema = Entity.SchemaObject({
EarningsTypeID: { type: String},
Amount: { type: Number}
EarningsTypeID: { type: String },
Amount: { type: Number }
});
var OpeningBalanceBenefitLineSchema = Entity.SchemaObject({
BenefitLineID: { type: String},
Amount: { type: Number}
BenefitLineID: { type: String },
Amount: { type: Number }
});
var OpeningBalanceDeductionLineSchema = Entity.SchemaObject({
DeductionTypeID: { type: String},
Amount: { type: Number}
DeductionTypeID: { type: String },
Amount: { type: Number }
});
var OpeningBalanceReimbursementLineSchema = Entity.SchemaObject({
ReimbursementTypeID: { type: String},
Amount: { type: Number}
ReimbursementTypeID: { type: String },
Amount: { type: Number }
});

var OpeningBalancesSchema = Entity.SchemaObject({
Expand All @@ -103,64 +103,56 @@ var OpeningBalancesSchema = Entity.SchemaObject({
var EmployeeSchema = new Entity.SchemaObject({
EmployeeID: { type: String },
Status: { type: String },
UpdatedDateUTC: { type: Date},
UpdatedDateUTC: { type: Date },
FirstName: { type: String },
LastName: { type: String },
DateOfBirth: { type: String },
HomeAddress: { type: AddressSchema},
MiddleNames: { type: String},
JobTitle: { type: String},
Email: { type: String},
Gender: { type: String},
MailingAddress: { type: AddressSchema},
Phone: { type: String},
EmployeeNumber: { type: String},
SocialSecurityNumber: { type: String},
StartDate: { type: String},
PayScheduleID: { type: String},
EmployeeGroupName: { type: String},
EmploymentBasis: { type: String},
HolidayGroupID: { type: String},
IsAuthorisedToApproveTimeOff: { type: Boolean},
IsAuthorisedToApproveTimesheets: { type: Boolean},
HomeAddress: { type: AddressSchema },
MiddleNames: { type: String },
JobTitle: { type: String },
Email: { type: String },
Gender: { type: String },
MailingAddress: { type: AddressSchema },
Phone: { type: String },
EmployeeNumber: { type: String },
SocialSecurityNumber: { type: String },
StartDate: { type: String },
PayScheduleID: { type: String },
EmployeeGroupName: { type: String },
EmploymentBasis: { type: String },
HolidayGroupID: { type: String },
IsAuthorisedToApproveTimeOff: { type: Boolean },
IsAuthorisedToApproveTimesheets: { type: Boolean },
SalaryAndWages: { type: Array, arrayType: SalaryAndWageSchema, toObject: 'always' },
WorkLocations: { type: Array, arrayType: WorkLocationSchema, toObject: 'always' },
PaymentMethod: { type: PaymentMethodSchema, toObject: 'always'},
PaymentMethod: { type: PaymentMethodSchema, toObject: 'always' },
PayTemplate: { type: PayTemplateSchema, toObject: 'always' },
OpeningBalances: { type: Array, arrayType: OpeningBalancesSchema, toObject: 'always' }
});


var Employee = Entity.extend(EmployeeSchema, {
constructor: function (application, data, options)
{
constructor: function(application, data, options) {
logger.debug('Employee::constructor');
this.Entity.call(this, application,data,options);
this.Entity.call(this, application, data, options);
},
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)
{
console.log(obj);
fromXmlObj: function(obj) {
var self = this;
_.extend(self, _.omit(obj, 'SalaryAndWages', 'WorkLocations','PaymentMethod', 'PayTemplate','OpeningBalances'));
_.extend(self, _.omit(obj, 'SalaryAndWages', 'WorkLocations', 'PaymentMethod', 'PayTemplate', 'OpeningBalances'));
if (obj.SalaryAndWages)
this.extractArray(obj.SalaryAndWages.SalaryAndWage, this.SalaryAndWages);
if (obj.WorkLocations)
this.extractArray(obj.WorkLocations.WorkLocation, this.WorkLocations);

if (obj.PaymentMethod)
{
_.extend(this.PaymentMethod, _.pick(obj.PaymentMethod,'PaymentMethodType'));
if (obj.PaymentMethod) {
_.extend(this.PaymentMethod, _.pick(obj.PaymentMethod, 'PaymentMethodType'));
if (obj.PaymentMethod.BankAccounts)
this.extractArray(obj.PaymentMethod.BankAccounts, this.PaymentMethod.BankAccounts);
}
Expand All @@ -184,33 +176,28 @@ var Employee = Entity.extend(EmployeeSchema, {
if (obj.OpeningBalances.ReimbursementLines)
this.extractArray(obj.OpeningBalances.ReimbursementLines.ReimbursementLine, this.OpeningBalances.ReimbursementLines);
}

return this;
},
toXml: function ()
{
toXml: function() {
// TO-DO
var employee = _.omit(this.toObject());
return this.application.js2xml(employee, 'Employee');
},
save:function(options)
{
save: function(options) {
var xml = '<Employees>' + this.toXml() + '</Employees>';
var path, method;
if (this.EmployeeId)
{
if (this.EmployeeId) {
path = 'Employees/' + this.EmployeeID;
method = 'post'
}
else
{
} else {
path = 'Employees';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml, { api: 'payroll'});
return this.application.putOrPostEntity(method, path, xml, { api: 'payroll' });
}
});


module.exports = Employee;
module.exports.EmployeeSchema = EmployeeSchema;
module.exports.EmployeeSchema = EmployeeSchema;
47 changes: 22 additions & 25 deletions lib/entity_helpers/contacts.js
@@ -1,39 +1,36 @@
var _ = require('lodash')
, logger = require('../logger')
, EntityHelper = require('./entity_helper')
, Contact = require('../entities/contact')
, p = require('../misc/promise')
, util = require('util')
var _ = require('lodash'),
logger = require('../logger'),
EntityHelper = require('./entity_helper'),
Contact = require('../entities/contact'),
p = require('../misc/promise'),
util = require('util')

var Contacts = EntityHelper.extend({
constructor: function (application, options)
{
EntityHelper.call(this, application, _.extend({ entityName:'Contact', entityPlural:'Contacts'}, options));
constructor: function(application, options) {
EntityHelper.call(this, application, _.extend({ entityName: 'Contact', entityPlural: 'Contacts' }, options));
},
newContact: function (data, options)
{
newContact: function(data, options) {
return new Contact(this.application, data, options)
},
getContact: function (id, modifiedAfter)
{
return this.getContacts({ id: id, modifiedAfter: modifiedAfter})
.then(function (contacts)
{
getContact: function(id, modifiedAfter) {
return this.getContacts({ id: id, modifiedAfter: modifiedAfter })
.then(function(contacts) {
return _.first(contacts);
})
},
saveContacts: function (contacts, options)
{
return this.saveEntities(contacts, options)
saveContacts: function(contacts, options) {
return this.saveEntities(contacts, this.setupOptions(options));
},
getContacts: function (options)
{
getContacts: function(options) {
return this.getEntities(this.setupOptions(options));
},
setupOptions: function(options) {
var self = this;
var clonedOptions = _.clone(options || {});
clonedOptions.entityConstructor = function(data) { return self.newContact(data)};
return this.getEntities(clonedOptions)
clonedOptions.entityPath = 'Contacts.Contact';
clonedOptions.entityConstructor = function(data) { return self.newContact(data) };
return clonedOptions;
}
})

module.exports = Contacts;

module.exports = Contacts;

0 comments on commit 251b083

Please sign in to comment.