Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Added method to get all deals.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmschreiner committed Jun 13, 2017
1 parent dbe75ff commit bc29c29
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 14 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,35 @@ http://developers.hubspot.com/docs/methods/blogv2/get_blog_posts_blog_post_id

### Deals

#### - Get all deals
Get all of the deals in a portal. Returns a paginated set of deals.

In addition to the list of deals, each request will also return two values, *offset* and *hasMore*. If *hasMore* is *true*, you'll need to make another request, using the offset to get the next page of deal records.

| Parameter | Description |
| --------- | ----------- |
| **limit** | The number of records to return. Defaults to 100, has a maximum value of 250. |
| **offset** | Used to page through the results. If there are more records in your portal than the limit= parameter, you will need to use the offset returned in the first request to get the next set of results. |
| **properties** | Used to include specific deal properties in the results. By default, the results will only include Deal ID and will not include the values for any properties for your Deals. Including this parameter will include the data for the specified property in the results. You can include this parameter multiple times to request multiple properties. Note: Deals that do not have a value set for a property will not include that property, even when you specify the property. A deal without a value for the dealname property would not show the dealname property in the results, even with &properties=dealname in the URL. |
| **propertiesWithHistory** | Works similarly to properties=, but this parameter will include the history for the specified property, instead of just including the current value. Use this parameter when you need the full history of changes to a property's value. |
| **includeAssociations** | If it's set to *true*, it will include the IDs of the associated contacts and companies in the results. This will also automatically include the *num_associated_contacts* property. |

**Usage:**
```javascript
api.deals.getAllDeals({
limit: 100,
offset: null,
properties: 'hubspot_owner_id',
propertiesWithHistory: 'dealname',
includeAssociations: false,
})
.then(response => console.log(response.data.deals))
.catch(error => console.error(error))
```

**Reference:**
https://developers.hubspot.com/docs/methods/deals/get-all-deals

#### - Create a deal
This methods creates a deal on HubSpot. You can create associations between Deals and Contacts and Companies but it's not required.

Expand Down
16 changes: 13 additions & 3 deletions dist/endpoints/deals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var Deals = function Deals() {
if (api === null) throw new Error('Request instance must be provided on constructor.');

return {
createDeal: function createDeal(parameters) {
var properties = parameters.properties,
associations = parameters.associations;
createDeal: function createDeal(params) {
var properties = params.properties,
associations = params.associations;


var mappedProperties = Object.keys(properties).map(function (property) {
Expand All @@ -40,6 +40,16 @@ var Deals = function Deals() {
}).catch(function (error) {
return (0, _errorHandler2.default)(error);
});
},
getAllDeals: function getAllDeals() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};


return api.get('deals/v1/deal/paged', params).then(function (response) {
return (0, _responseHandler2.default)(response);
}).catch(function (error) {
return (0, _errorHandler2.default)(error);
});
}
};
};
Expand Down
5 changes: 2 additions & 3 deletions dist/helpers/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
"use strict";

module.exports = function (error) {

if (error.response) {

// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
console.log('\n ======> Error\n Message: ' + (error.response.data.message || error.response.statusText) + '\n Status Code: ' + error.response.status + '\n Status: ' + (error.response.data.status || '') + '\n headers: ' + JSON.stringify(error.response.headers) + '\n ');

throw new Error(error.response.data.message || error.response.statusText);
} else {
// Something happened in setting up the request that triggered an Error
Expand Down
5 changes: 2 additions & 3 deletions dist/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ var Request = function () {

_classCallCheck(this, Request);

if (apiKey === null) throw new Error('You must provide the API key.');
if (apiKey === null) throw new Error('You must provide the HubSpot API key.');

this.apiKey = apiKey;
this.apiInstance = _axios2.default.create({
baseURL: '' + API_ENDPOINT,
timeout: 10000
});
timeout: 300000 });
}

_createClass(Request, [{
Expand Down
3 changes: 1 addition & 2 deletions dist/helpers/responseHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
module.exports = function (response) {

if (response.statusText === 'error') {

// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
console.log('\n ======> Error\n Message: ' + response.body.message + '\n Status: ' + error.statusCode + '\n headers: ' + JSON.stringify(error.headers) + '\n ');

throw new Error(response.body.message);
}

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-hubspot-api",
"version": "1.0.0",
"version": "1.1.0",
"description": "A wrapper for the HubSpot API based on Node.",
"main": "./dist/index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions src/endpoints/deals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const Deals = (api = null) => {
if (api === null) throw new Error('Request instance must be provided on constructor.')

return {
createDeal(parameters) {
createDeal(params) {

let { properties, associations } = parameters
let { properties, associations } = params

let mappedProperties = Object.keys(properties).map(property => ({
value: properties[property],
Expand All @@ -19,6 +19,12 @@ const Deals = (api = null) => {
.then(response => responseHandler(response))
.catch(error => errorHandler(error))
},
getAllDeals(params = {}) {

return api.get('deals/v1/deal/paged', params)
.then(response => responseHandler(response))
.catch(error => errorHandler(error))
},
}
}

Expand Down
45 changes: 45 additions & 0 deletions test/deals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,51 @@ let expect = chai.expect

describe('Deals', () => {

describe('Get deal', () => {

it('Should return a list of all deals', done => {

api.deals.getAllDeals()
.then(response => {
expect(response.status).to.equal(200)
expect(response.data).to.be.a('object')
expect(response.data.deals).to.be.a('array')
done()
})
.catch(error => done(error))
})

it('Should return a list of all deals, limit by 50', done => {

api.deals.getAllDeals({limit: 50})
.then(response => {
expect(response.status).to.equal(200)
expect(response.data).to.be.a('object')
expect(response.data.deals.length).to.be.equal(50)
expect(response.data.deals).to.be.a('array')
done()
})
.catch(error => done(error))
})

it('Should return a list of all deals, including the IDs of the associated contacts and companies', done => {

api.deals.getAllDeals({includeAssociations: true})
.then(response => {
expect(response.status).to.equal(200)
expect(response.data).to.be.a('object')
expect(response.data.deals).to.be.a('array')
expect(response.data.deals[0].associations).to.be.a('object')
expect(response.data.deals[0].associations.associatedVids).to.be.a('array')
expect(response.data.deals[0].associations.associatedCompanyIds).to.be.a('array')
expect(parseInt(response.data.deals[0].properties.num_associated_contacts.value)).to.be.a('number')
done()
})
.catch(error => done(error))
})

})

describe('Create a deal', () => {

it('Should return the data for the newly created deal', done => {
Expand Down

0 comments on commit bc29c29

Please sign in to comment.