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 a contact.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmschreiner committed May 10, 2017
1 parent ec438f5 commit b32765d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/endpoints/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Request {
}

// TODO
post() {

post(endPoint, params = {}) {
return this.apiInstance.post(`${endPoint}?hapikey=${this.apiKey}`, this.normalizeParams(params))
}

// TODO
Expand Down
4 changes: 4 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
*/
module.exports = {
apiKey: 'demo',
credentials: {
username: '[email protected]',
password: 'HubSpot',
}
}
28 changes: 28 additions & 0 deletions test/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
})

0 comments on commit b32765d

Please sign in to comment.