Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickP committed Jun 12, 2016
2 parents 7ff9e7a + a9dfb21 commit 7934c3b
Show file tree
Hide file tree
Showing 26 changed files with 1,059 additions and 633 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"node": true,
"mocha": true
},
"extends": "airbnb"
"extends": "airbnb-base"
}
178 changes: 148 additions & 30 deletions lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ const utils = require('./utils');
* @property {Boolean} success
*/

/**
* @typedef statement
*
* @property {String} id
* @property {String} type `pdf` for pdf buffer or `base64` for pdf in base64
* @property {Buffer|String} pdf PDF encoded
*/

/**
* @typedef statements
*
Expand All @@ -231,6 +239,25 @@ const utils = require('./utils');
* @property {Number} year
*/

/**
* @typedef contact
*
* @property {String} id
* @property {String} name
* @property {String} subtitle
* @property {Object} account
* @property {String} account.accountType
* @property {String} account.iban
* @property {String} account.bic
*/

/**
* @typedef limit
*
* @property {String} limit Limit type, can be ATM_DAILY_ACCOUNT or POS_DAILY_ACCOUNT
* @property {Number} amount
*/

/**
* Number26 Account
*/
Expand Down Expand Up @@ -309,6 +336,15 @@ class Account {
return utils.callApi(this, 'getCards');
}

/**
* Get contacts
*
* @return {Promise<contact[]>}
*/
contacts() {
return utils.callApi(this, 'getContacts');
}

/**
* Get / send invitations emails
*
Expand All @@ -325,6 +361,57 @@ class Account {
return utils.callApi(this, 'getInvitations');
}

/**
* Get / Set limits
*
* @param {Object} [limits]
* @param {Number} [limits.atm] Set daily limit for ATM withdraw
* @param {Number} [limits.pos] Set daily limit for POS payment
*
* @return {Promise|Promise<limit[]>}
*/
limits(limits) {
function validateAmount(amount, max) {
if (!Number.isInteger(amount) || amount < 0 || amount > max) {
return false;
}

return true;
}

return Promise.try(() => {
if (limits) {
const calls = [];

if (limits.atm !== undefined) {
if (!validateAmount(limits.atm, 2500)) {
throw new Error('Limits should be between 0 and 2500');
}

calls.push(utils.callApi(this, 'setLimits', {
limit: 'ATM_DAILY_ACCOUNT',
amount: limits.atm
}));
}

if (limits.pos !== undefined) {
if (!validateAmount(limits.pos, 5000)) {
throw new Error('Limits should be between 0 and 5000');
}

calls.push(utils.callApi(this, 'setLimits', {
limit: 'POS_DAILY_ACCOUNT',
amount: limits.pos
}));
}

return Promise.all(calls);
}

return utils.callApi(this, 'getLimits');
});
}

/**
* Get information about current user
*
Expand All @@ -347,6 +434,29 @@ class Account {
.then((meta) => utils.callApi(this, 'createOrUpdateMemo', {smartLinkId, meta, memo}));
}

/**
* Get statement
*
* @description Return pdf buffer or base64 encoded
*
* @return {Promise<statement>}
*/
statement(id, pdf) {
return Promise.try(() => {
pdf = !!pdf;
if (!id) {
throw new Error('MISSING_PARAMS');
}

return utils.callApi(this, 'getStatement', {id, pdf})
.then(result => ({
id,
type: (pdf) ? 'pdf' : 'base64',
pdf: (pdf) ? new Buffer(result, 'binary') : result.pdf
}));
});
}

/**
* Get statements
*
Expand Down Expand Up @@ -403,17 +513,19 @@ class Account {
* @return {Promise<transaction>}
*/
transaction(id, options) {
if (!id) {
return Promise.reject('MISSING_PARAMS');
}
return Promise.try(() => {
if (!id) {
throw new Error('MISSING_PARAMS');
}

const data = {id};
const data = {id};

if (options && options.meta) {
data.meta = options.meta;
}
if (options && options.meta) {
data.meta = options.meta;
}

return utils.callApi(this, 'getTransaction', data);
return utils.callApi(this, 'getTransaction', data);
});
}

/**
Expand All @@ -430,15 +542,17 @@ class Account {
* @return {Promise<transfer>}
*/
transfer(data) {
if (!data.pin || !data.iban || !data.bic || !data.amount || !data.name || !data.reference) {
return Promise.reject('MISSING_PARAMS');
}
return Promise.try(() => {
if (!data.pin || !data.iban || !data.bic || !data.amount || !data.name || !data.reference) {
throw new Error('MISSING_PARAMS');
}

if (data.reference.length > 135) {
return Promise.reject('REFERENCE_TOO_LONG');
}
if (data.reference.length > 135) {
throw new Error('REFERENCE_TOO_LONG');
}

return utils.callApi(this, 'createTransfer', data);
return utils.callApi(this, 'createTransfer', data);
});
}

/**
Expand All @@ -452,17 +566,19 @@ class Account {
* @return {Promise}
*/
unpairInit(pin, cardNumber) {
const that = this;

if (!pin || !cardNumber) {
return Promise.reject('MISSING_PARAMS');
}

return utils.callApi(that, 'unpairUpstart')
.then(link => utils.callApi(that, 'unpairVerify', link))
.then(() => utils.callApi(that, 'unpairValidationPin', pin))
.then(() => utils.callApi(that, 'unpairValidationCard', cardNumber))
.then(() => utils.callApi(that, 'unpairValidationSms'));
return Promise.try(() => {
const that = this;

if (!pin || !cardNumber) {
throw new Error('MISSING_PARAMS');
}

return utils.callApi(that, 'unpairUpstart')
.then(link => utils.callApi(that, 'unpairVerify', link))
.then(() => utils.callApi(that, 'unpairValidationPin', pin))
.then(() => utils.callApi(that, 'unpairValidationCard', cardNumber))
.then(() => utils.callApi(that, 'unpairValidationSms'));
});
}

/**
Expand All @@ -476,11 +592,13 @@ class Account {
* @return {Promise}
*/
unpairConfirm(smsNumber) {
if (!smsNumber) {
return Promise.reject('MISSING_PARAMS');
}
return Promise.try(() => {
if (!smsNumber) {
throw new Error('MISSING_PARAMS');
}

return utils.callApi(this, 'unpairValidationSmsVerify', smsNumber);
return utils.callApi(this, 'unpairValidationSmsVerify', smsNumber);
});
}
}

Expand Down
43 changes: 43 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ module.exports = {
.catch(errorHandler);
},

getContacts(token) {
return request.get({
url: `${api}/api/smrt/contacts`,
json: true,
headers: {
Authorization: `Bearer ${token}`
}
})
.catch(errorHandler);
},

getLimits(token) {
return request.get({
url: `${api}/api/settings/account/limits`,
json: true,
headers: {
Authorization: `Bearer ${token}`
}
})
.catch(errorHandler);
},

getMe(token, options) {
const qs = {};
if (options.full) {
Expand Down Expand Up @@ -142,6 +164,17 @@ module.exports = {
.catch(errorHandler);
},

getStatement(token, options) {
return request.get({
url: `${api}/api/statements/${(options.pdf) ? '' : 'json/'}${options.id}`,
json: !options.pdf,
headers: {
Authorization: `Bearer ${token}`
}
})
.catch(errorHandler);
},

getStatements(token) {
return request.get({
url: `${api}/api/statements`,
Expand Down Expand Up @@ -254,6 +287,16 @@ module.exports = {
});
},

setLimits(token, limits) {
return request.post({
url: `${api}/api/settings/account/limits`,
json: limits,
headers: {
Authorization: `Bearer ${token}`
}
});
},

/*
Pairing / unpairing
*/
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "number26",
"version": "1.0.0-rc.2",
"version": "1.0.0",
"description": "Un-official node.js module for interact with your number26 account",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,9 +32,9 @@
"chai": "^3.2.0",
"coveralls": "^2.11.9",
"dirty-chai": "^1.2.2",
"docdash": "^0.3.0",
"docdash": "^0.4.0",
"eslint": "^2.8.0",
"eslint-config-airbnb": "^8.0.0",
"eslint-config-airbnb-base": "^3.0.1",
"eslint-plugin-import": "^1.5.0",
"istanbul": "^0.4.2",
"jsdoc": "^3.4.0",
Expand Down
57 changes: 57 additions & 0 deletions tests/account/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';
/* eslint-disable global-require, max-len, arrow-body-style */
const nock = require('nock');
const chai = require('chai');
const dirtyChai = require('dirty-chai');
const expect = chai.expect;

chai.use(dirtyChai);

let n26;
const data = require('../fixtures/data');

beforeEach((done) => {
require('../fixtures/auth')((err, m) => {
n26 = m;

done();
});
});

describe('account', () => {
let api;

beforeEach(() => {
api = nock('https://api.tech26.de')
.defaultReplyHeaders({
'Content-Type': 'application/json'
})
.matchHeader('Authorization', `Bearer ${data.access_token}`)
.get('/api/accounts')
.reply(200, {
status: 'OPEN_PRIMARY_ACCOUNT',
availableBalance: 42.42,
usableBalance: 42.42,
bankBalance: 4242.00,
iban: 'NL72SNSB0931762238',
id: 'e112c309-80df-4016-8079-93ffdea8300e'
});
});

it('should return account', () => {
return n26.account().then((account) => {
expect(account).to.be.eql({
status: 'OPEN_PRIMARY_ACCOUNT',
availableBalance: 42.42,
usableBalance: 42.42,
bankBalance: 4242.00,
iban: 'NL72SNSB0931762238',
id: 'e112c309-80df-4016-8079-93ffdea8300e'
});
});
});

afterEach((done) => {
done((!api.isDone()) ? new Error('Request not done') : null);
});
});
Loading

0 comments on commit 7934c3b

Please sign in to comment.