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 create or update a contact by email. Updates to versi…
Browse files Browse the repository at this point in the history
…on v0.4.0
  • Loading branch information
hmschreiner committed May 29, 2017
1 parent 3721f09 commit 97553f9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 protected]')
.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
Expand Down
10 changes: 10 additions & 0 deletions dist/endpoints/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
};
};
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": "0.3.0",
"version": "0.4.0",
"description": "A wrapper for the HubSpot API based on Node.",
"main": "./dist/index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/endpoints/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

},
}
}

Expand Down
20 changes: 17 additions & 3 deletions test/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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))

})

})
})

0 comments on commit 97553f9

Please sign in to comment.