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

Commit

Permalink
Closes #5 and updates to version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hmschreiner committed May 28, 2017
1 parent aa23c1f commit 7a0b24f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,25 @@ api.contacts.updateContact({
company: 'My Company 2',
...
}, 123456)
.then(response => console.log(response.data.properties))
.then(response => console.log(response.status))
.catch(error => console.error(error))
```

**Reference:**
https://developers.hubspot.com/docs/methods/contacts/update_contact

#### - Update a contact by email

**Usage:**
```javascript
api.contacts.updateContactByEmail({
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))
```

Expand Down
12 changes: 11 additions & 1 deletion dist/endpoints/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var Contacts = function Contacts() {
return (0, _errorHandler2.default)(error);
});
},
updateContact: function updateContact(properties, id) {
updateContactById: function updateContactById(properties, id) {

var mappedProperties = this.mapProperties(properties);

Expand All @@ -66,6 +66,16 @@ var Contacts = function Contacts() {
}).catch(function (error) {
return (0, _errorHandler2.default)(error);
});
},
updateContactByEmail: function updateContactByEmail(properties, email) {

var mappedProperties = this.mapProperties(properties);

return api.post('contacts/v1/contact/email/' + email + '/profile', { 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.2.2",
"version": "0.3.0",
"description": "A wrapper for the HubSpot API based on Node.",
"main": "./dist/index.js",
"scripts": {
Expand Down
12 changes: 10 additions & 2 deletions src/endpoints/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ const Contacts = (api = null) => {
.then(response => responseHandler(response))
.catch(error => errorHandler(error))
},
updateContact(properties, id) {
updateContactById(properties, id) {

let mappedProperties = this.mapProperties(properties)

return api.post(`contacts/v1/contact/vid/${id}/profile`, {properties: [ ...mappedProperties ]})
.then(response => responseHandler(response))
.catch(error => errorHandler(error))
}
},
updateContactByEmail(properties, email) {

let mappedProperties = this.mapProperties(properties)

return api.post(`contacts/v1/contact/email/${email}/profile`, {properties: [ ...mappedProperties ]})
.then(response => responseHandler(response))
.catch(error => errorHandler(error))
},
}
}

Expand Down
42 changes: 28 additions & 14 deletions test/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,60 @@ describe('Contacts', () => {

describe('Create or update a contact', () => {

let newContact = {
let contactInfo = {
email: Math.random().toString(36).substring(2,11) + '@domain.com',
firstname: 'James',
lastname: 'Bond',
website: 'http://www.mycompany.com',
company: 'My Company',
}

let existingContact = {}

it('Should return the details of the new contact record', done => {

api.contacts.createContact(newContact)
api.contacts.createContact(contactInfo)
.then(response => {
expect(response.status).to.equal(200)
expect(response.data).to.be.a('object')
expect(response.data.properties).to.be.a('object')
expect(response.data.properties.email.value).to.be.equal(newContact.email)
expect(response.data.properties.firstname.value).to.be.equal(newContact.firstname)
expect(response.data.properties.lastname.value).to.be.equal(newContact.lastname)
expect(response.data.properties.website.value).to.be.equal(newContact.website)
expect(response.data.properties.company.value).to.be.equal(newContact.company)
expect(response.data.properties.email.value).to.be.equal(contactInfo.email)
expect(response.data.properties.firstname.value).to.be.equal(contactInfo.firstname)
expect(response.data.properties.lastname.value).to.be.equal(contactInfo.lastname)
expect(response.data.properties.website.value).to.be.equal(contactInfo.website)
expect(response.data.properties.company.value).to.be.equal(contactInfo.company)

// Save created contact ID
existingContact.id = response.data.vid
contactInfo.id = response.data.vid

done()
})
.catch(error => done(error))
})

it('Should return the details of the updated contact record', done => {
it('Should return the details of the updated contact record by ID', done => {

existingContact = {
...existingContact,
contactInfo = {
...contactInfo,
firstname: 'Jon',
lastname: 'Doe',
}

api.contacts.updateContact(existingContact, existingContact.id)
api.contacts.updateContactById(contactInfo, contactInfo.id)
.then(response => {
expect(response.status).to.equal(204)
done()
})
.catch(error => done(error))
})

it('Should return the details of the updated contact record by email', done => {

contactInfo = {
...contactInfo,
firstname: 'Jon Update',
lastname: 'Doe Update',
}

api.contacts.updateContactByEmail(contactInfo, contactInfo.email)
.then(response => {
expect(response.status).to.equal(204)
done()
Expand Down

0 comments on commit 7a0b24f

Please sign in to comment.