diff --git a/README.md b/README.md index 7fa5b28..119e0e8 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,9 @@ ChartMogul.Customer.merge(config, { ChartMogul.Customer.contacts(config, customerUuid, { per_page: 10, cursor: 'cursor==' }) ChartMogul.Customer.createContact(config, customerUuid, data) + +ChartMogul.Customer.notes(config, customerUuid, { per_page: 10, cursor: 'cursor==' }) +ChartMogul.Customer.createNote(config, customerUuid, data) ``` #### [Contacts](https://dev.chartmogul.com/docs/contacts) @@ -137,6 +140,18 @@ ChartMogul.Contact.all(config, { per_page: 10, cursor: 'cursor==' }) ``` +#### [Customer Notes](https://dev.chartmogul.com/docs/customer_notes) + +```js +ChartMogul.CustomerNote.create(config, data) +ChartMogul.CustomerNote.retrieve(config, noteUuid) +ChartMogul.CustomerNote.patch(config, noteUuid) +ChartMogul.CustomerNote.destroy(config, noteUuid) +ChartMogul.CustomerNote.all(config, { per_page: 10, cursor: 'cursor==', customer_uuid: customerUuid}) + +``` + + #### [Plans](https://dev.chartmogul.com/docs/plans) ```js diff --git a/lib/chartmogul.js b/lib/chartmogul.js index 98690f6..e8bb0a7 100644 --- a/lib/chartmogul.js +++ b/lib/chartmogul.js @@ -4,6 +4,7 @@ const errors = require('./chartmogul/errors'); const Metrics = require('./chartmogul/metrics'); const Customer = require('./chartmogul/customer'); +const CustomerNote = require('./chartmogul/customer-note'); const Contact = require('./chartmogul/contact'); const DataSource = require('./chartmogul/data-source'); const Plan = require('./chartmogul/plan'); @@ -30,6 +31,7 @@ const ChartMogul = { Config, CustomAttribute, Customer, + CustomerNote, Contact, DataSource, Enrichment, diff --git a/lib/chartmogul/customer-note.js b/lib/chartmogul/customer-note.js new file mode 100644 index 0000000..3fed5b2 --- /dev/null +++ b/lib/chartmogul/customer-note.js @@ -0,0 +1,11 @@ +'use strict'; + +const Resource = require('./resource'); + +class CustomerNote extends Resource { + static get path () { + return '/v1/customer_notes{/noteUuid}'; + } +} + +module.exports = CustomerNote; diff --git a/lib/chartmogul/customer.js b/lib/chartmogul/customer.js index b4784c7..d83a50c 100644 --- a/lib/chartmogul/customer.js +++ b/lib/chartmogul/customer.js @@ -2,6 +2,7 @@ const Resource = require('./resource'); const util = require('./util'); +const CustomerNote = require('./customer-note'); class Customer extends Resource { static get path () { @@ -27,6 +28,16 @@ class Customer extends Resource { const path = util.expandPath(this.path, [customerId, 'contacts']); return Resource.request(config, 'POST', path, params, callback); } + + static notes (config, customerId, params, callback) { + const path = util.expandPath(CustomerNote.path, []); + return Resource.request(config, 'GET', path, { ...params, customerId }, callback); + } + + static createrNote (config, customerId, params, callback) { + const path = util.expandPath(CustomerNote.path, []); + return Resource.request(config, 'POST', path, { ...params, customerId }, callback); + } } // @Override diff --git a/test/chartmogul/customer-note.js b/test/chartmogul/customer-note.js new file mode 100644 index 0000000..170ce3b --- /dev/null +++ b/test/chartmogul/customer-note.js @@ -0,0 +1,118 @@ +'use strict'; + +const ChartMogul = require('../../lib/chartmogul'); +const config = new ChartMogul.Config('token'); +const expect = require('chai').expect; +const nock = require('nock'); +const CustomerNote = ChartMogul.CustomerNote; + +describe('CustomerNote', () => { + it('creates a note from a customer', () => { + const uuid = 'cus_00000000-0000-0000-0000-000000000000'; + const postBody = { + customer_uuid: uuid, + type: 'note', + text: 'This is a note' + }; + + nock(config.API_BASE) + .post('/v1/customer_notes', postBody) + .reply(200, { + uuid: 'note_00000000-0000-0000-0000-000000000000', + customer_uuid: uuid, + type: 'note', + text: 'This is a note', + created_at: '2021-10-20T14:00:00.000Z', + updated_at: '2021-10-20T14:00:00.000Z', + author: 'John Doe (john@example.com)' + }); + + CustomerNote.create(config, postBody) + .then(res => { + expect(res.customer_uuid).to.be.equal(uuid); + expect(res.text).to.be.equal('This is a note'); + }); + }); + + it('lists all notes from a customer', () => { + const uuid = 'cus_00000000-0000-0000-0000-000000000000'; + + nock(config.API_BASE) + .get('/v1/customer_notes') + .reply(200, { + entries: [{ + uuid: 'note_00000000-0000-0000-0000-000000000000', + customer_uuid: 'cus_00000000-0000-0000-0000-000000000000', + author: 'John Doe (john@example.com)', + text: 'This is a note', + created_at: '2015-01-01T12:00:00-05:00', + updated_at: '2015-01-01T12:00:00-05:00', + type: 'note' + }] + }); + + CustomerNote.all(config, uuid) + .then(res => { + expect(res.entries).to.haveLength(1); + }); + }); + + it('retrieves a customer note', () => { + const uuid = 'note_00000000-0000-0000-0000-000000000000'; + + nock(config.API_BASE) + .get(`/v1/customer_notes/${uuid}`) + .reply(200, { + uuid, + customer_uuid: 'cus_00000000-0000-0000-0000-000000000000', + type: 'note', + text: 'This is a note', + created_at: '2021-10-20T14:00:00.000Z', + updated_at: '2021-10-20T14:00:00.000Z', + author: 'John Doe (john@example.com)' + }); + + CustomerNote.retrieve(config, uuid).then(res => { + expect(res).to.have.property('uuid'); + }); + }); + + it('updates a customer note', () => { + const uuid = 'note_00000000-0000-0000-0000-000000000000'; + const postBody = { + text: 'This is a note' + }; + + nock(config.API_BASE) + .patch(`/v1/customer_notes/${uuid}`, postBody) + .reply(200, { + /* eslint-disable camelcase */ + uuid, + customer_uuid: 'cus_00000000-0000-0000-0000-000000000000', + type: 'note', + text: 'This is a note', + created_at: '2021-10-20T14:00:00.000Z', + updated_at: '2021-10-20T14:00:00.000Z', + author: 'John Doe (john@example.com)' + /* eslint-enable camelcase */ + }); + + CustomerNote.patch(config, uuid, postBody) + .then(res => { + expect(res.text).to.be.equal('This is a note'); + }); + }); + + it('deletes a customer note', () => { + const uuid = 'con_00000000-0000-0000-0000-000000000000'; + + nock(config.API_BASE) + .delete(`/v1/customer_notes/${uuid}`) + .reply(204); + + CustomerNote.destroy(config, uuid) + .then(res => { + expect(res).to.be.equal({}); + }); + }); +}); diff --git a/test/chartmogul/customer.js b/test/chartmogul/customer.js index 476f28d..8a015da 100644 --- a/test/chartmogul/customer.js +++ b/test/chartmogul/customer.js @@ -187,6 +187,59 @@ describe('Customer', () => { expect(res.has_more).to.eql(false); }); }); + + it('creates a new note from a customer', () => { + const customerUuid = 'cus_00000000-0000-0000-0000-000000000000'; + const postBody = { + customer_uuid: customerUuid, + type: 'note', + author_email: 'john@example.com', + note: 'This is a note' + }; + + nock(config.API_BASE) + .post('/v1/customer_notes', postBody) + .reply(200, { + uuid: 'note_00000000-0000-0000-0000-000000000000', + customer_uuid: 'cus_00000000-0000-0000-0000-000000000000', + author: 'John Doe (john@example.com)', + text: 'This is a note', + created_at: '2015-01-01T12:00:00-05:00', + updated_at: '2015-01-01T12:00:00-05:00', + type: 'note' + }); + + Customer.createrNote(config, customerUuid, postBody) + .then(res => { + expect(res).to.have.property('uuid'); + }); + }); + + it('gets all notes from a customer', () => { + const customerUuid = 'cus_00000000-0000-0000-0000-000000000000'; + + nock(config.API_BASE) + .get(`/v1/customer_notes?customer_uuid=${customerUuid}`) + .reply(200, { + entries: [{ + uuid: 'note_00000000-0000-0000-0000-000000000000', + customer_uuid: 'cus_00000000-0000-0000-0000-000000000000', + author: 'John Doe (john@example.com)', + text: 'This is a note', + created_at: '2015-01-01T12:00:00-05:00', + updated_at: '2015-01-01T12:00:00-05:00', + type: 'note' + }] + }); + + Customer.notes(config, customerUuid) + .then(res => { + expect(res).to.have.property('entries'); + expect(res.entries).to.be.instanceof(Array); + expect(res.cursor).to.eql('MjAyMy0wMy0xM1QxMjowMTozMi44MD=='); + expect(res.has_more).to.eql(false); + }); + }); }); /** Suite that originally belonged in the Enrichment module.