diff --git a/README.md b/README.md index 0307def..6439ce2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/dist/endpoints/deals.js b/dist/endpoints/deals.js index 2ac26d7..8d99ae4 100644 --- a/dist/endpoints/deals.js +++ b/dist/endpoints/deals.js @@ -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) { @@ -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); + }); } }; }; diff --git a/dist/helpers/errorHandler.js b/dist/helpers/errorHandler.js index 1b529ef..a7d5a4c 100644 --- a/dist/helpers/errorHandler.js +++ b/dist/helpers/errorHandler.js @@ -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 diff --git a/dist/helpers/request.js b/dist/helpers/request.js index b9c5438..e955122 100644 --- a/dist/helpers/request.js +++ b/dist/helpers/request.js @@ -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, [{ diff --git a/dist/helpers/responseHandler.js b/dist/helpers/responseHandler.js index 3cf625d..1ed91c4 100644 --- a/dist/helpers/responseHandler.js +++ b/dist/helpers/responseHandler.js @@ -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); } diff --git a/package.json b/package.json index cd792bf..9b6f89c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/endpoints/deals.js b/src/endpoints/deals.js index cd8212c..261e714 100644 --- a/src/endpoints/deals.js +++ b/src/endpoints/deals.js @@ -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], @@ -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)) + }, } } diff --git a/test/deals.js b/test/deals.js index 9889e75..29c8678 100644 --- a/test/deals.js +++ b/test/deals.js @@ -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 => {