From 7a0b24f6e908d483281677bbec0b2cddb1c068e4 Mon Sep 17 00:00:00 2001 From: Henrique Schreiner Date: Sat, 27 May 2017 22:12:25 -0300 Subject: [PATCH] Closes #5 and updates to version 0.3.0 --- README.md | 20 +++++++++++++++++- dist/endpoints/contacts.js | 12 ++++++++++- package.json | 2 +- src/endpoints/contacts.js | 12 +++++++++-- test/contacts.js | 42 +++++++++++++++++++++++++------------- 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 9aa60ca..04320fa 100644 --- a/README.md +++ b/README.md @@ -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@domain.com') +.then(response => console.log(response.status)) .catch(error => console.error(error)) ``` diff --git a/dist/endpoints/contacts.js b/dist/endpoints/contacts.js index 86841d4..f1f27c1 100644 --- a/dist/endpoints/contacts.js +++ b/dist/endpoints/contacts.js @@ -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); @@ -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); + }); } }; }; diff --git a/package.json b/package.json index 69034dc..a2a016b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/endpoints/contacts.js b/src/endpoints/contacts.js index ac4e387..935337e 100644 --- a/src/endpoints/contacts.js +++ b/src/endpoints/contacts.js @@ -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)) + }, } } diff --git a/test/contacts.js b/test/contacts.js index 55b315c..3dc106e 100644 --- a/test/contacts.js +++ b/test/contacts.js @@ -22,7 +22,7 @@ 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', @@ -30,38 +30,52 @@ describe('Contacts', () => { 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()