svb-wires
Version:
Helper API to issue wire transfers
109 lines (97 loc) • 2.75 kB
JavaScript
//
// svb-ach
// index.js
//
// For full documentation: http://docs.svbplatform.com/authentication/
// Email apisupport@svb.com for help!
//
const _genericGet = function(client, type, id, callback) {
let url = ['/v1', type, id];
if (!id) {
url.pop();
}
client.get(url.join('/'), '', (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
const _genericPatch = function(client, type, id, data, callback) {
client.patch(['/v1', type, id].join('/'), data, (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
const _genericPost = function(client, type, data, callback) {
client.post(['/v1', type].join('/'), data, (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
function SVBWires(client) {
if (!client) {
throw Error('SVB Wire Transfers helper requires an SVB Client object');
}
this.client = client;
}
SVBWires.prototype = {
create: function(wireData, callback) {
_genericPost(this.client, 'wire', wireData, callback);
},
get: function(wireID, callback) {
_genericGet(this.client, 'wire', wireID, callback);
},
updateStatus: function(wireID, wireStatus, callback) {
if (typeof wireStatus === 'object') {
if (wireStatus.status) {
wireStatus = wireStatus.status;
} else {
throw Error('send a wire transfer status string or { status: status } parameter');
}
}
_genericPatch(this.client, 'wire', wireID, { status: wireStatus }, callback);
},
find: function(filters, callback) {
let filterURL = [];
if (filters) {
if (filters.status) {
filterURL.push('filter%5Bstatus%5D=' + filters.status);
}
if (filters.effective_date_start) {
filterURL.push('filter%5Beffective_date_start%5D=' + filters.effective_date_start);
}
if (filters.effective_date_end) {
filterURL.push('filter%5Beffective_date_end%5D=' + filters.effective_date_end);
}
}
if (!filterURL.length) {
throw Error('send a Wires filter object ({ status: status } or { effective_date_start: "YYYY-MM-DD" } or { effective_date_end: "YYYY-MM-DD" })');
}
this.client.get('/v1/wire', filterURL.join('&'), (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
},
filtered: function(filters, callback) {
this.find(filters, callback);
},
all: function(callback) {
this.get('', callback);
}
};
module.exports = SVBWires;