Skip to content

Commit

Permalink
Merge pull request #180 from emartech/add-contact-field-delete
Browse files Browse the repository at this point in the history
feat(field): add contact field DELETE CDP-1737
  • Loading branch information
BlasiusVonSzerencsi committed Oct 7, 2022
2 parents e035b37 + 09a0929 commit ebe8ea5
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ Statistics about the purchases of customer.

suiteAPI.field.getMultipleChoices(payload, options);

##### [Delete Field](https://dev.emarsys.com/docs/emarsys-api/9b5653c65052b-delete-a-field)

suiteAPI.field.delete(payload, options);

#### Export

##### [Downloading export data](https://dev.emarsys.com/v2/contact-and-email-data/download-export-data)
Expand Down
51 changes: 51 additions & 0 deletions api/endpoints/_test/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var _ = require('lodash');

module.exports = {
shouldDeleteResourceOnEndpoint: function(expectedUrl, expectedData) {
var apiEndpoint;
var request;


expectedData = expectedData || this._requestRespondWith;

describe('towards ' + expectedUrl + ' endpoint', function() {

var self = this;

beforeEach(function() {
request = self._getRequestStub();
apiEndpoint = self.ApiEndpoint.create(request, { customerId: 123 });
});

it('should properly call a DELETE request', async function() {
await apiEndpoint[self.method](self.payload, self.options);
expect(request.delete).to.have.been.callCount(1);
expect(request.delete).to.have.been.calledWith(123, expectedUrl, {});
});


it('should return the result', async function() {
var result = await apiEndpoint[self.method](self.payload, self.options);
expect(request.delete).to.have.been.callCount(1);
expect(result).to.eql(expectedData);
});


it('should pass the options to the suite request', async function() {
var options = _.merge({ customerId: 999, environment: 'otherEnv' }, self.options);
await apiEndpoint[self.method](self.payload, options);
expect(request.delete).to.have.been.callCount(1);
expect(request.delete).to.have.been.calledWithExactly(sinon.match.any, sinon.match.any, options);
});


it('should override customerId from the options', async function() {
await apiEndpoint[self.method](self.payload, { customerId: 999 });
expect(request.delete).to.have.been.callCount(1);
expect(request.delete).to.have.been.calledWithExactly(999, sinon.match.any, { customerId: 999 });
});
}.bind(this));
}
};
6 changes: 4 additions & 2 deletions api/endpoints/_test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _ = require('lodash');
var testGet = require('./get');
var testPost = require('./post');
var testPut = require('./put');
var testDelete = require('./delete');
var testMissingParameter = require('./missing-parameter');
var testRequestError = require('./request-error');

Expand All @@ -24,7 +25,7 @@ var ApiMethodTest = function(ApiEndpoint, method) {
}
};

_.extend(this, testGet, testPost, testPut, testMissingParameter, testRequestError);
_.extend(this, testGet, testPost, testPut, testDelete, testMissingParameter, testRequestError);
};

ApiMethodTest.prototype = {
Expand All @@ -46,7 +47,8 @@ ApiMethodTest.prototype = {
return {
get: sinon.stub().returns(Promise.resolve(this._requestRespondWith)),
post: sinon.stub().returns(Promise.resolve(this._requestRespondWith)),
put: sinon.stub().returns(Promise.resolve(this._requestRespondWith))
put: sinon.stub().returns(Promise.resolve(this._requestRespondWith)),
delete: sinon.stub().returns(Promise.resolve(this._requestRespondWith))
};
}

Expand Down
9 changes: 9 additions & 0 deletions api/endpoints/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ _.extend(Field.prototype, {
options
);
}.bind(this));
},

delete: function(payload, options) {
return this._requireParameters(payload, ['field_id']).then(function() {
var url = util.format('/field/%s', payload.field_id);
logger.log('field_delete');

return this._request.delete(this._getCustomerId(options), url, options);
}.bind(this));
}

});
Expand Down
6 changes: 6 additions & 0 deletions api/endpoints/field/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ describe('SuiteAPI Field endpoint', function() {
testApiMethod(FieldAPI, 'getMultipleChoices').withArgs({ field_ids: [123, 456], translate_id: 'fr' }).shouldGetResultFromEndpoint('/field/choices?fields=123,456&language=fr');
});

describe('#delete', function() {
testApiMethod(FieldAPI, 'delete').withArgs({ field_id: 123 }).shouldDeleteResourceOnEndpoint('/field/123');

testApiMethod(FieldAPI, 'delete').withArgs({}).shouldThrowMissingParameterError('field_id');
});

});
9 changes: 9 additions & 0 deletions lib/api-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ _.extend(ApiRequest.prototype, {
return this._suiteRequest.put(completeUrl, data, options);
},

delete: function(customerId, url, options) {
var opts = this._getOptions(options);

this._suiteRequest = this._createRequest(opts);

var completeUrl = this._assembleUrl(customerId, url);
return this._suiteRequest.delete(completeUrl, options);
},

setCache: function(cacheId) {
this._cacheId = cacheId;
},
Expand Down
59 changes: 59 additions & 0 deletions lib/api-request/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,65 @@ describe('ApiRequest', function() {

});

describe('#delete', function() {

it('should urlencode customer name if specified', function(done) {
assertRequestMethodCalledWithEncodedCustomerName('put', done);
});

it('should call suite request\'s delete', function(done) {
var promiseRespond = { dummyData: 12 };

var putApiRequest = sinon.stub(SuiteRequest.prototype, 'delete').callsFake(function() {
return getPromiseResolvesWith(promiseRespond);
});

var request = new Request({});

request.delete(2, '/administrator').then(function(returnValue) {
expect(putApiRequest).to.have.been.calledWith('/2/administrator');
expect(returnValue).to.eql(promiseRespond);
}).then(done);
});


it('should throw the sdk error if something went wrong', function(done) {
var promiseError = new Error('yoError');
sinon.stub(SuiteRequest.prototype, 'delete').callsFake(function() {
return getPromiseRejectsWith(promiseError);
});
var request = new Request({});

request.delete(2, '/administrator', {}).catch(function(error) {
expect(error).to.equal(promiseError);
done();
});
});


it('should use the appropriate keys from the given options object', function(done) {
var options = getDefaultOptions();
var expectedOptions = _.omit(options, 'some_option');

sinon.stub(SuiteRequest.prototype, 'delete').callsFake(function() {
return getPromiseResolvesWith({});
});
sinon.stub(SuiteRequest.Options, 'createForInternalApi').returns({});

var request = new Request({});

request.delete(2, '/administrator', options).then(function() {
expect(SuiteRequest.Options.createForInternalApi).to.have.been.calledWith(expectedOptions);
}).then(done).catch(done);
});


it('should use the initial config', function(done) {
testRequestOptionSetupConstructorParameter('delete', done);
});

});

beforeEach(function() {
getPromiseResolvesWith = getPromiseResolvesWith.bind(this);
getPromiseRejectsWith = getPromiseRejectsWith.bind(this);
Expand Down

0 comments on commit ebe8ea5

Please sign in to comment.