diff --git a/src/endpoints/contacts.js b/src/endpoints/contacts.js index 96fa1df..8dc376e 100644 --- a/src/endpoints/contacts.js +++ b/src/endpoints/contacts.js @@ -22,6 +22,18 @@ class Contacts { .then(response => responseHandler(response)) .catch(error => errorHandler(error)) } + + createContact(properties) { + + let mappedProperties = Object.keys(properties).map((property) => ({ + property: property, + value: properties[property], + })) + + return this.api.post('contacts/v1/contact', {properties: [ ...mappedProperties ]}) + .then(response => responseHandler(response)) + .catch(error => errorHandler(error)) + } } export default Contacts diff --git a/src/helpers/request.js b/src/helpers/request.js index 5ec8e18..9de7570 100644 --- a/src/helpers/request.js +++ b/src/helpers/request.js @@ -24,8 +24,8 @@ class Request { } // TODO - post() { - + post(endPoint, params = {}) { + return this.apiInstance.post(`${endPoint}?hapikey=${this.apiKey}`, this.normalizeParams(params)) } // TODO diff --git a/test/config.js b/test/config.js index 542d05a..18bc751 100644 --- a/test/config.js +++ b/test/config.js @@ -3,4 +3,8 @@ */ module.exports = { apiKey: 'demo', + credentials: { + username: 'testapi@hubspot.com', + password: 'HubSpot', + } } diff --git a/test/contacts.js b/test/contacts.js index 22ef893..fffd39b 100644 --- a/test/contacts.js +++ b/test/contacts.js @@ -22,4 +22,32 @@ describe('Contacts', () => { .catch(error => done(error)) }) }) + + describe('Create a contact', () => { + + let newContact = { + email: Math.random().toString(36).substring(2,11) + '@domain.com', + firstname: 'James', + lastname: 'Bond', + website: 'http://www.mycompany.com', + company: 'My Company', + } + + it('Should return the details of the new contact record', done => { + + api.contacts.createContact(newContact) + .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) + done() + }) + .catch(error => done(error)) + }) + }) })