Skip to content

Commit

Permalink
Merge pull request eversign#1 from eversign/fileupload-formfield-fixes
Browse files Browse the repository at this point in the history
File Upload and FormField fixes
  • Loading branch information
om3g4 authored Jul 14, 2017
2 parents f55bce7 + 5280bb5 commit 70d873b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
38 changes: 35 additions & 3 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var async = require('async');
var ApiRequest = require("./Request");
var config = require('./config');
var _ = require('lodash');


/**
Expand Down Expand Up @@ -101,17 +102,48 @@ function Client(accessKey, businessId){
"business_id" : selectedBusiness ? selectedBusiness.getBusinessId(): businessId,
};

async.each(document.getFiles(), function (file, callback) {
self.uploadFile(file)
async.forEachOf(document.getFiles(), function (file, index, callback) {
if(file.getFilePath() || file.getFileBase64()) {
self.uploadFile(file)
.then(function (response) {

var changedFile = document.files[index];
changedFile.filePath = '';
changedFile.fileUrl = '';
changedFile.fileId = '';
changedFile.fileBase64 = '';

changedFile.fileId = response.file_id;
callback();
})
.catch(function (error) {
callback(error);
});
}
else {
callback();
}
}, function(err){
// All files saved. Now save the document
var request = new ApiRequest("POST", accessKey, config.DOCUMENT_URL,undefined, parameters, document);
console.log(JSON.stringify(document));

_.forEach(document.files, function(file, index) {
var sendFile = _.mapKeys(file, function(value, key) {
return _.snakeCase(key);
});
document.files[index] = sendFile;
});

_.forEach(document.fields, function(field, index) {
_.forEach(document.fields[index], function(innerField, innerIndex) {
var sendField = _.mapKeys(innerField, function(value, key) {
return _.snakeCase(key);
});
document.fields[index][innerIndex] = sendField;
});
});

var request = new ApiRequest("POST", accessKey, config.DOCUMENT_URL, undefined, parameters, document);
return request.startRequest();
});

Expand Down
4 changes: 2 additions & 2 deletions lib/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ function Document(newDocument = {}) {
}

if(!formField.getIdentifier())
formField.setIdentifier(formField.constructor.name + "_" + (this.getFields().length + 1));
formField.identifier = formField.constructor.name + "_" + (this.getFields().length + 1);

if(!document.fields[formField.getFileIndex()])
document.fields[formField.getFileIndex()] = [];

document.fields[formField.getFileIndex()].push(formField);
};

Expand Down
43 changes: 42 additions & 1 deletion lib/FormField.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function FormField(newFormField) {
* @type {String}
*/
identifier: undefined,

/**
* The number of the page where the FormField should be displayed
* @type {Number}
Expand Down Expand Up @@ -56,7 +57,13 @@ function FormField(newFormField) {
* The FormField's file index on which it will be placed
* @type {Number}
*/
fileIndex: 0
fileIndex: 0,

/**
* @type {String}
*
*/
type: ""
};

Object.assign(formField, newFormField);
Expand All @@ -65,9 +72,43 @@ function FormField(newFormField) {
if(!this.getHeight() || !this.getWidth() || !this.getX() || !this.getY() || !this.getPage()){
return false;
}
this.generateType();
return true;
};

this.generateType = function () {
if(this.constructor.name == "SignatureField") {
this.type = "signature";
}
else if(this.constructor.name == "AttachmentField") {
this.type = "attachment";
}
else if(this.constructor.name == "NoteField") {
this.type = "note";
}
else if(this.constructor.name == "RadioField") {
this.type = "radio";
}
else if(this.constructor.name == "TextField") {
this.type = "text";
}
else if(this.constructor.name == "TextFormField") {
this.type = "text";
}
else if(this.constructor.name == "DropdownField") {
this.type = "dropdown";
}
else if(this.constructor.name == "DateSignedField") {
this.type = "date_signed";
}
else if(this.constructor.name == "InitialsField") {
this.type = "initials";
}
else {
this.type = "";
}
}

this.getIdentifier = function () {
return formField.identifier;
};
Expand Down
5 changes: 5 additions & 0 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ ApiRequest.prototype.startMultipartUpload = function () {
ApiRequest.prototype.startRequest = function(){
if(config.DEBUG_MODE) console.log(config.API_URL + this.endPoint);
var self = this;

this.payload = _.mapKeys(this.payload, function(value, key) {
return _.snakeCase(key);
});

var requestOptions = {
method: this.httpType,
uri: config.API_URL + this.endPoint + "?" + qs.stringify(createQuery(this)),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eversign",
"version": "1.1.1",
"version": "1.2",
"description": "Nodejs client for eversign api",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 70d873b

Please sign in to comment.