From 962d96d70e3f2809999fd31acd0586abe14d3edf Mon Sep 17 00:00:00 2001 From: Henrique Schreiner Date: Mon, 29 May 2017 16:01:42 -0300 Subject: [PATCH] Released version 1.0 --- README.md | 105 ++++++++++++++++++++++++++++------------ dist/endpoints/deals.js | 47 ++++++++++++++++++ dist/index.js | 6 ++- package.json | 2 +- src/endpoints/deals.js | 25 ++++++++++ src/index.js | 3 +- test/deals.js | 38 +++++++++++++++ 7 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 dist/endpoints/deals.js create mode 100644 src/endpoints/deals.js create mode 100644 test/deals.js diff --git a/README.md b/README.md index 1d28a81..0307def 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ A wrapper for the HubSpot API based on Node - http://developers.hubspot.com/docs/overview +## Version 1.0 +First stable release is now available [here](https://github.com/hmschreiner/node-hubspot-api/releases/tag/v1.0.0). + ## Installation `npm install node-hubspot-api --save` @@ -26,12 +29,12 @@ const api = new NodeHubSpotApi('your_api_key') #### - Get all contacts Return all contacts that have been created. A paginated list of contacts will be returned with a maximum of 100 contacts per page. -Parameter | Description | Required | Default ---------- | ----------- | :------: | :-----: -**count** | Specify the amount of contacts to return. | No | 20 -**vidOffset** | Used to page through the contacts. | No | - -**property** | By default, only a few standard properties will be included in the response data. Include the 'property' parameter to get the specified property in the response. | No | - -**showListMemberships** | Indicate whether current list memberships should be fetched for the contact. | No | false +| Parameter | Description | Required | Default | +| --------- | ----------- | :------: | :-----: | +| **count** | Specify the amount of contacts to return. | No | 20 | +| **vidOffset** | Used to page through the contacts. | No | - | +| **property** | By default, only a few standard properties will be included in the response data. Include the 'property' parameter to get the specified property in the response. | No | - | +| **showListMemberships** | Indicate whether current list memberships should be fetched for the contact. | No | false | **Usage:** ```javascript @@ -140,13 +143,13 @@ https://developers.hubspot.com/docs/methods/contacts/create_or_update #### - Get all blogs List all of the blogs for a portal. Supports paging and filtering. -Parameter | Description | Required | Default ---------- | ----------- | :------: | :-----: -**limit** | The number of items to return. | No | 20 -**offset** | The offset set to start returning rows from. | No | 0 -**created** | exact, range, gt, gte, lt, lte - When the post was first created, in milliseconds since the epoch. | No | - -**deleted_at** | exact, gt, gte - When the post was deleted, in milliseconds since the epoch. Zero if the blog post was never deleted. | No | - -**name** | exact, in - The internal name of the blog | No | - +| Parameter | Description | Required | Default | +| --------- | ----------- | :------: | :-----: | +| **limit** | The number of items to return. | No | 20 | +| **offset** | The offset set to start returning rows from. | No | 0 +| **created** | exact, range, gt, gte, lt, lte - When the post was first created, in milliseconds since the epoch. | No | - | +| **deleted_at** | exact, gt, gte - When the post was deleted, in milliseconds since the epoch. Zero if the blog post was never deleted. | No | - | +| **name** | exact, in - The internal name of the blog | No | - | **Usage:** ```javascript @@ -166,21 +169,21 @@ api.blog.getAllBlogs({ #### - List blog posts Get the posts from your blogs. -Parameter | Description | Required | Default ---------- | ----------- | :------: | :-----: -**limit** | The number of items to return. | No | 20 -**offset** | The offset set to start returning rows from. | No | 0 -**archived** | Returns the posts that match the boolean lookup (e.g. archived=false returns all posts currently not archived). | No | false -**blog_author_id** | Returns the posts that match a particular blog author ID value. | No | - -**campaign** | Returns the posts that match the campaign guid. | No | - -**content_group_id** | Returns the posts that match the blog guid. The blog guid can be found in the blog dashboard URL (e.g. https://app.hubspot.com/blog/:portal_id/dashboard/:blog_guid). | No | - -**created** | Returns the posts that match a particular created time value. Supports exact, range, gt, gte, lt, lte lookups. | No | - -**deleted_at** | Returns the posts that match a particular deleted time value. Supports exact, gt, gte, lt, lte lookups. | No | - -**name** | Returns the posts that match the name value. Supports exact, contains, icontains, ne lookups. | No | - -**slug** | Returns the posts that match a particular slug value. | No | - -**updated** | Returns the posts that match a particular updated time. Supports exact, range, gt, gte, lt, lte lookups. | No | - -**state** | DRAFT, PUBLISHED, or SCHEDULED. | No | PUBLISHED -**order_by** | Return the posts ordered by a particular field value. Blog posts can currently only be sorted by publish_date. Use a negative value to sort in descending order (e.g. order_by=-publish_date). | No | publish_date +| Parameter | Description | Required | Default | +| --------- | ----------- | :------: | :-----: | +| **limit** | The number of items to return. | No | 20 | +| **offset** | The offset set to start returning rows from. | No | 0 | +| **archived** | Returns the posts that match the boolean lookup (e.g. archived=false returns all posts currently not archived). | No | false | +| **blog_author_id** | Returns the posts that match a particular blog author ID value. | No | - | +| **campaign** | Returns the posts that match the campaign guid. | No | - | +| **content_group_id** | Returns the posts that match the blog guid. The blog guid can be found in the blog dashboard URL (e.g. https://app.hubspot.com/blog/:portal_id/dashboard/:blog_guid). | No | - | +| **created** | Returns the posts that match a particular created time value. Supports exact, range, gt, gte, lt, lte lookups. | No | - | +| **deleted_at** | Returns the posts that match a particular deleted time value. Supports exact, gt, gte, lt, lte lookups. | No | - | +| **name** | Returns the posts that match the name value. Supports exact, contains, icontains, ne lookups. | No | - | +| **slug** | Returns the posts that match a particular slug value. | No | - +| **updated** | Returns the posts that match a particular updated time. Supports exact, range, gt, gte, lt, lte lookups. | No | - | +| **state** | DRAFT, PUBLISHED, or SCHEDULED. | No | PUBLISHED | +| **order_by** | Return the posts ordered by a particular field value. Blog posts can currently only be sorted by publish_date. Use a negative value to sort in descending order (e.g. order_by=-publish_date). | No | publish_date | **Usage:** ```javascript @@ -217,7 +220,47 @@ api.blog.getPostById(3198892953) **Reference:** http://developers.hubspot.com/docs/methods/blogv2/get_blog_posts_blog_post_id -## Disclaimer -This tool is under development and don't have a stable version yet. +### 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. + +The **dealstage** property is required when creating a deal. If the **pipeline** property is not specified, the default pipeline is assumed. However, it is recommended to always specify the pipeline, especially on portals with multiple pipelines. + +Returns a 200 on success with the data for the newly created deal in the response. -*DO NOT USE IT IN PRODUCTION!* +You must pass an object to the method with these parameters: + +| Parameter | Description | +| --------- | ----------- | +| Associated records | `"associations": {}` - A set of IDs for records that the new deal should be associated with. Deals can be associated with a single company (associatedCompanyIds) and any number of contacts (associatedVids). | +| Deal properties | `"properties": []` - A list of property names, and the value you want to set for the property. | + +**Usage:** +```javascript +api.deals.createDeal({ + associations: { + associatedCompanyIds: [ + 123456 + ], + associatedVids: [ + 1234, 12345, 123456, ... + ], + }, + properties: { + dealname: 'This is a brand new deal. Awesome!', + hubspot_owner_id: 123456, + amount: '50000', + dealtype: 'newbusiness', + dealstage: 'appointmentscheduled', + }, +}) +.then(response => console.log(response.data)) +.catch(error => console.error(error)) +``` + +**Reference:** +https://developers.hubspot.com/docs/methods/deals/create_deal + +## Disclaimer +This tool is under development and don't have all HubSpot API endpoints implemented yet. diff --git a/dist/endpoints/deals.js b/dist/endpoints/deals.js new file mode 100644 index 0000000..2ac26d7 --- /dev/null +++ b/dist/endpoints/deals.js @@ -0,0 +1,47 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _errorHandler = require('../helpers/errorHandler'); + +var _errorHandler2 = _interopRequireDefault(_errorHandler); + +var _responseHandler = require('../helpers/responseHandler'); + +var _responseHandler2 = _interopRequireDefault(_responseHandler); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + +var Deals = function Deals() { + var api = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; + + + 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; + + + var mappedProperties = Object.keys(properties).map(function (property) { + return { + value: properties[property], + name: property + }; + }); + + return api.post('deals/v1/deal', { properties: [].concat(_toConsumableArray(mappedProperties)), associations: associations }).then(function (response) { + return (0, _responseHandler2.default)(response); + }).catch(function (error) { + return (0, _errorHandler2.default)(error); + }); + } + }; +}; + +exports.default = Deals; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 736dc52..e2738cc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -12,6 +12,10 @@ var _blog = require('./endpoints/blog'); var _blog2 = _interopRequireDefault(_blog); +var _deals = require('./endpoints/deals'); + +var _deals2 = _interopRequireDefault(_deals); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var NodeHubSpotApi = function NodeHubSpotApi() { @@ -27,7 +31,7 @@ var NodeHubSpotApi = function NodeHubSpotApi() { blog: (0, _blog2.default)(api), domains: null, files: null, - deals: null + deals: (0, _deals2.default)(api) }; }; diff --git a/package.json b/package.json index 80cb514..cd792bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-hubspot-api", - "version": "0.4.0", + "version": "1.0.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 new file mode 100644 index 0000000..cd8212c --- /dev/null +++ b/src/endpoints/deals.js @@ -0,0 +1,25 @@ +import errorHandler from '../helpers/errorHandler' +import responseHandler from '../helpers/responseHandler' + +const Deals = (api = null) => { + + if (api === null) throw new Error('Request instance must be provided on constructor.') + + return { + createDeal(parameters) { + + let { properties, associations } = parameters + + let mappedProperties = Object.keys(properties).map(property => ({ + value: properties[property], + name: property, + })) + + return api.post('deals/v1/deal', {properties: [ ...mappedProperties ], associations}) + .then(response => responseHandler(response)) + .catch(error => errorHandler(error)) + }, + } +} + +export default Deals diff --git a/src/index.js b/src/index.js index 1cbb199..7dce870 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ import Request from './helpers/request' import Contacts from './endpoints/contacts' import Blog from './endpoints/blog' +import Deals from './endpoints/deals' const NodeHubSpotApi = (apiKey = null) => { @@ -13,7 +14,7 @@ const NodeHubSpotApi = (apiKey = null) => { blog: Blog(api), domains: null, files: null, - deals: null, + deals: Deals(api), } } diff --git a/test/deals.js b/test/deals.js new file mode 100644 index 0000000..9889e75 --- /dev/null +++ b/test/deals.js @@ -0,0 +1,38 @@ +import chai from 'chai' +import api from './setup' + +let expect = chai.expect + +describe('Deals', () => { + + describe('Create a deal', () => { + + it('Should return the data for the newly created deal', done => { + + let dealInfo = { + associations: { + associatedCompanyIds: [], // Optional - companies association ID's + associatedVids: [], // Optional - contacts association ID's + }, + properties: { + dealname: 'This is a brand new deal. Awesome!', + hubspot_owner_id: '70', // demo user ID, + amount: '50000', + dealtype: 'newbusiness', + }, + } + + api.deals.createDeal(dealInfo) + .then(response => { + expect(response.status).to.equal(200) + expect(response.data).to.be.a('object') + expect(response.data.properties.dealname.value).to.be.equal(dealInfo.properties.dealname) + expect(response.data.properties.hubspot_owner_id.value).to.be.equal(dealInfo.properties.hubspot_owner_id) + expect(response.data.properties.amount.value).to.be.equal(dealInfo.properties.amount) + expect(response.data.properties.dealtype.value).to.be.equal(dealInfo.properties.dealtype) + done() + }) + .catch(error => done(error)) + }) + }) +})