Skip to content

Commit

Permalink
add more methods to decision API
Browse files Browse the repository at this point in the history
  • Loading branch information
officert committed Jun 14, 2017
1 parent 55f685e commit df74aca
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 4 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ client.events.create({

- [Labels](#labels)
- [Score](#score)
- [Decisions](#decisions)

## Debug mode

Expand Down Expand Up @@ -286,3 +287,31 @@ client.events.create({
$abuse_types: 'payment_abuse,promotion_abuse'
});
```

## Decisions

### [Apply Decisions](https://siftscience.com/developers/docs/curl/decisions-api/apply-decisions)

```javascript
client.decisions.createByAccountIdAndUserId('accountId', 'userId', {
decision_id : 'user_looks_ok_payment_abuse'
});
```

### [User Decisions](https://siftscience.com/developers/docs/curl/decisions-api/decision-status)

```javascript
client.decisions.getByAccountIdAndUserId('accountId', 'userId');
```

### [Order Decisions](https://siftscience.com/developers/docs/curl/decisions-api/decision-status)

```javascript
client.decisions.getByAccountIdAndOrderId('accountId', 'orderId');
```

### [Decisions List](https://siftscience.com/developers/docs/curl/decisions-api/decisions-list)

```javascript
client.decisions.listByAccountId('accountId');
```
45 changes: 42 additions & 3 deletions lib/client/decisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ function getDecisionsApi(siftScienceClient) {
const logger = siftScienceClient._logger;

return {
/**
* @param {Object} [data]
* @param {Object} [params]
* https://siftscience.com/developers/docs/curl/decisions-api/apply-decisions
*/
createByAccountIdAndUserId(accountId, userId, data = {}, params = {}) {
if (!accountId) return Promise.reject(new Error('accountId is required'));
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(data, {
$api_key: key
});

return Promise.resolve(v3HttpClient.post(`/accounts/${accountId}/users/${userId}/decisions`, data, params))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
throw error;
});
},
/**
* @param {Object} [data]
* @param {Object} [params]
Expand All @@ -17,7 +37,7 @@ function getDecisionsApi(siftScienceClient) {
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(params, {
$api_key: key
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/users/${userId}/decisions`, params))
Expand All @@ -37,7 +57,7 @@ function getDecisionsApi(siftScienceClient) {
if (!orderId) return Promise.reject(new Error('orderId is required'));

_.extend(params, {
$api_key: key
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/orders/${orderId}/decisions`, params))
Expand All @@ -46,7 +66,26 @@ function getDecisionsApi(siftScienceClient) {
logger.logRequestError(error);
throw error;
});
}
},
/**
* @param {Object} [data]
* @param {Object} [params]
* https://siftscience.com/developers/docs/curl/decisions-api/decisions-list
*/
listByAccountId(accountId, params = {}) {
if (!accountId) return Promise.reject(new Error('accountId is required'));

_.extend(params, {
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/decisions`, params))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
throw error;
});
},
};
}

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": "node-siftscience",
"version": "0.0.13",
"version": "0.0.14",
"description": "Node HTTP client for Sift Science's API",
"main": "index.js",
"scripts": {
Expand Down
124 changes: 124 additions & 0 deletions tests/lib/client/decision/createByAccountIdAndUserId-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const _ = require('lodash');
const should = require('should');
const sinon = require('sinon');

let siftScience;
let v3HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v3HttpClient = require('../../../../lib/client/v3HttpClient');

sandbox = sinon.sandbox.create();
});

describe('lib', () => {
describe('client', () => {
describe('decisions', () => {
describe('createByAccountIdAndUserId', () => {
afterEach(() => {
sandbox.restore();
});

const key = '123';
let siftScienceClient;

before('create siftScienceClient', () => {
siftScienceClient = siftScience.init(key);
});

describe('when called without accountId', () => {
let accountId = null;

it('should reject with an error', () => {
return siftScienceClient.decisions.createByAccountIdAndUserId(accountId)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('accountId is required');
});
});
});

describe('when called without userId', () => {
let accountId = '123';
let userId = null;

it('should reject with an error', () => {
return siftScienceClient.decisions.createByAccountIdAndUserId(accountId, userId)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('userId is required');
});
});
});

describe('when called with an accountId and userId', () => {
let accountId = '123';
let userId = '123';
let data = {};
let response = {
data: {}
};

let postStub;

before('stub v3HttpClient.post()', () => {
postStub = sandbox.stub(v3HttpClient, 'post')
.returns(Promise.resolve(response));
});

it('should call v3HttpClient.post()', () => {
return siftScienceClient.decisions.createByAccountIdAndUserId(accountId, userId, data)
.then(result => {
should.exist(result);
result.should.deepEqual(response.data);

postStub.callCount.should.equal(1);
postStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/decisions`);
postStub.args[0][1].should.deepEqual(data);
});
});
});

describe('when called with params', () => {
let accountId = '123';
let userId = '123';
let data = {};
let params = {
foo: 'bar'
};
let response = {
data: {}
};

let postStub;

before('stub v3HttpClient.post()', () => {
postStub = sandbox.stub(v3HttpClient, 'post')
.returns(Promise.resolve(response));
});

it('should call v3HttpClient.post()', () => {
return siftScienceClient.decisions.createByAccountIdAndUserId(accountId, userId, data, params)
.then(result => {
should.exist(result);
result.should.deepEqual(response.data);

postStub.callCount.should.equal(1);
postStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/decisions`);
postStub.args[0][1].should.deepEqual(_.extend(data, {
$api_key: key
}));
postStub.args[0][2].should.deepEqual(params);
});
});
});
});
});
});
});
100 changes: 100 additions & 0 deletions tests/lib/client/decision/listByAccountId-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const should = require('should');
const sinon = require('sinon');

let siftScience;
let v3HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v3HttpClient = require('../../../../lib/client/v3HttpClient');

sandbox = sinon.sandbox.create();
});

describe('lib', () => {
describe('client', () => {
describe('decisions', () => {
describe('listByAccountId', () => {
afterEach(() => {
sandbox.restore();
});

const key = '123';
let siftScienceClient;

before('create siftScienceClient', () => {
siftScienceClient = siftScience.init(key);
});

describe('when called without accountId', () => {
let accountId = null;

it('should reject with an error', () => {
return siftScienceClient.decisions.listByAccountId(accountId)
.then(should.not.exist)
.catch(err => {
should.exist(err);
err.should.be.instanceOf(Error);
err.message.should.equal('accountId is required');
});
});
});

describe('when called with an accountId', () => {
let accountId = '123';
let response = {
data: {}
};

let getStub;

before('stub v3HttpClient.get()', () => {
getStub = sandbox.stub(v3HttpClient, 'get')
.returns(Promise.resolve(response));
});

it('should call v3HttpClient.get()', () => {
return siftScienceClient.decisions.listByAccountId(accountId)
.then(result => {
should.exist(result);
result.should.deepEqual(response.data);

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/decisions`);
});
});
});

describe('when called with params', () => {
let accountId = '123';
let params = {
foo: 'bar'
};
let response = {
data: {}
};

let getStub;

before('stub v3HttpClient.get()', () => {
getStub = sandbox.stub(v3HttpClient, 'get')
.returns(Promise.resolve(response));
});

it('should call v3HttpClient.get()', () => {
return siftScienceClient.decisions.listByAccountId(accountId, params)
.then(result => {
should.exist(result);
result.should.deepEqual(response.data);

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/decisions`);
getStub.args[0][1].should.deepEqual(params);
});
});
});
});
});
});
});

0 comments on commit df74aca

Please sign in to comment.