diff --git a/README.md b/README.md index 5fcbbb8..1d28a81 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # node-hubspot-api ![version](http://img.shields.io/npm/v/node-hubspot-api.svg) [![Build Status](https://api.travis-ci.org/hmschreiner/node-hubspot-api.svg)](https://travis-ci.org/hmschreiner/node-hubspot-api.svg?branch=master) -[![Coverage Status](https://coveralls.io/repos/github/hmschreiner/node-hubspot-api/badge.svg?branch=master)](https://coveralls.io/github/hmschreiner/node-hubspot-api?branch=master) A wrapper for the HubSpot API based on Node - http://developers.hubspot.com/docs/overview @@ -93,6 +92,9 @@ api.contacts.updateContactById({ https://developers.hubspot.com/docs/methods/contacts/update_contact #### - Update a contact by email +Update an existing contact in HubSpot, identified by email. This method lets you update the properties of a contact in HubSpot. You must pass the Contact's email that you're updating as second parameter. + +If the request succeeds, you'll get an HTTP 204 response, which represents that you have successfully updated the contact in the system. There will be no data in the response body. **Usage:** ```javascript @@ -110,6 +112,29 @@ api.contacts.updateContactByEmail({ **Reference:** https://developers.hubspot.com/docs/methods/contacts/update_contact-by-email +#### - Create or update a contact +Create a contact if it doesn't exist already, or update it with the latest property values if it does. + +This returns a 200 response on success. The response will contain a vid of the updated or created record, and an isNew field that indicates if a new record was created. If the field is false, an existing record was updated. + +This will return a 409 Conflict error response if you are trying to update the email address of a record, and there is an existing record with the new email address. + +**Usage:** +```javascript +api.contacts.createOrUpdateContact({ + firstname: 'Jon', + lastname: 'Doe', + website: 'http://www.my-new-company.com', + company: 'My Company 2', + ... +}, 'email@domain.com') +.then(response => console.log(response.status)) +.catch(error => console.error(error)) +``` + +**Reference:** +https://developers.hubspot.com/docs/methods/contacts/create_or_update + ### Blog #### - Get all blogs diff --git a/dist/endpoints/contacts.js b/dist/endpoints/contacts.js index f1f27c1..475b246 100644 --- a/dist/endpoints/contacts.js +++ b/dist/endpoints/contacts.js @@ -76,6 +76,16 @@ var Contacts = function Contacts() { }).catch(function (error) { return (0, _errorHandler2.default)(error); }); + }, + createOrUpdateContact: function createOrUpdateContact(properties, email) { + + var mappedProperties = this.mapProperties(properties); + + return api.post('contacts/v1/contact/createOrUpdate/email/' + email, { properties: [].concat(_toConsumableArray(mappedProperties)) }).then(function (response) { + return (0, _responseHandler2.default)(response); + }).catch(function (error) { + return (0, _errorHandler2.default)(error); + }); } }; }; diff --git a/package.json b/package.json index a2a016b..9504a56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-hubspot-api", - "version": "0.3.0", + "version": "0.4.0", "description": "A wrapper for the HubSpot API based on Node.", "main": "./dist/index.js", "scripts": { diff --git a/src/endpoints/contacts.js b/src/endpoints/contacts.js index 935337e..704c27a 100644 --- a/src/endpoints/contacts.js +++ b/src/endpoints/contacts.js @@ -49,6 +49,15 @@ const Contacts = (api = null) => { .then(response => responseHandler(response)) .catch(error => errorHandler(error)) }, + createOrUpdateContact(properties, email) { + + let mappedProperties = this.mapProperties(properties) + + return api.post(`contacts/v1/contact/createOrUpdate/email/${email}`, {properties: [ ...mappedProperties ]}) + .then(response => responseHandler(response)) + .catch(error => errorHandler(error)) + + }, } } diff --git a/test/contacts.js b/test/contacts.js index 3dc106e..88b0b11 100644 --- a/test/contacts.js +++ b/test/contacts.js @@ -30,7 +30,7 @@ describe('Contacts', () => { company: 'My Company', } - it('Should return the details of the new contact record', done => { + it('Should return new contact record', done => { api.contacts.createContact(contactInfo) .then(response => { @@ -51,7 +51,7 @@ describe('Contacts', () => { .catch(error => done(error)) }) - it('Should return the details of the updated contact record by ID', done => { + it('Should return updated contact record by ID', done => { contactInfo = { ...contactInfo, @@ -67,7 +67,7 @@ describe('Contacts', () => { .catch(error => done(error)) }) - it('Should return the details of the updated contact record by email', done => { + it('Should return updated contact record by email', done => { contactInfo = { ...contactInfo, @@ -82,5 +82,19 @@ describe('Contacts', () => { }) .catch(error => done(error)) }) + + it('Should return the created or updated contact record by email and indicates if a new contact was created', done => { + + api.contacts.createOrUpdateContact(contactInfo, contactInfo.email) + .then(response => { + expect(response.status).to.equal(200) + expect(response.data.vid).to.be.a('number') + expect(response.data.isNew).to.be.a('boolean') + done() + }) + .catch(error => done(error)) + + }) + }) })