From df1e4e9e80f44687bcf1741bd4bda56250d8768e Mon Sep 17 00:00:00 2001 From: DougMidgley Date: Tue, 5 Nov 2019 13:29:38 +0100 Subject: [PATCH] =?UTF-8?q?closes=20salesforce-marketingcloud/FuelSDK-Node?= =?UTF-8?q?-SOAP#122=20Added=20Schedule=E2=80=A6=20(#123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * closes salesforce-marketingcloud/FuelSDK-Node-SOAP#122 Added Schedule Support * Added Test for Schedule action * Updated Test case * Missing semicolon * Corrected Tests and validated * Increment Version * Bumped version to 2.2.4 Changed version as per PR reviewer request --- lib/fuel-soap.js | 59 ++++++++++++++++- package.json | 2 +- test/specs/general-tests.js | 1 - test/specs/soap-action-schedule.js | 103 +++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 test/specs/soap-action-schedule.js diff --git a/lib/fuel-soap.js b/lib/fuel-soap.js index 71bf6c7..c5e9cb6 100644 --- a/lib/fuel-soap.js +++ b/lib/fuel-soap.js @@ -617,13 +617,70 @@ FuelSoap.prototype._parseResponse = function(key, body, callback) { callback(null, parsedRes); }.bind(this)); }; +/** + * This method handles the Schedule SOAP Action + * @memberof FuelSoap + * @param {String} type - xsi:type + * @param {Object} props - Value set in body as `ScheduleRequest.Objects` + * @param {Object} [options] - Configuration options passed in body as `ScheduleRequest.Options` + * @param {Boolean} [options.queryAllAccounts=false] - Sets `QueryAllAccounts = true` to body under `ScheduleRequest.Options`. **Note:** This value will be delete from body if used + * @param {Object} [options.reqOptions] - Request options passed to soapRequest fn. **Note:** These will be delete from body if passed + * @param {FuelSoap~StandardCallback} callback - Callback that handles response + */ +FuelSoap.prototype.schedule = function(type,schedule,interactions, action, options, callback) { + var body; + var optionsAndCallback; + var reqOptions; + var updateQueryAllAccounts; + + optionsAndCallback = determineCallbackAndOptions(arguments, callback, options); + callback = optionsAndCallback.callback; + options = optionsAndCallback.options; + + updateQueryAllAccounts = configureQueryAllAccounts(options); + if(isEmpty(options)) { + options = null; + } + + reqOptions = helpers.parseReqOptions(options); + body = { + ScheduleRequestMsg: { + $: { + xmlns: 'http://exacttarget.com/wsdl/partnerAPI' + } + , Action: action + , Options: options + , Schedule: schedule + , Interactions: interactions + } + }; + + if(Array.isArray(body.ScheduleRequestMsg.Interactions)){ + for(let i = 0; i < body.ScheduleRequestMsg.Interactions.length; i++){ + body.ScheduleRequestMsg.Interactions[0].Interaction.$ = {'xsi:type': type}; + } + } else if (typeof body.ScheduleRequestMsg.Interactions === "object") { + body.ScheduleRequestMsg.Interactions.Interaction.$ = {'xsi:type': type}; + } else { + throw new TypeError('Interactions must be of Array or Object Type'); + } + + updateQueryAllAccounts(body.ScheduleRequestMsg, 'Options'); + + this.soapRequest({ + action: 'Schedule' + , req: body + , key: 'ScheduleResponseMsg' + , retry: true + , reqOptions: reqOptions + }, callback); +}; // Methods that need implementations FuelSoap.prototype.configure = function() {}; FuelSoap.prototype.extract = function() {}; FuelSoap.prototype.getSystemStatus = function() {}; FuelSoap.prototype.query = function() {}; -FuelSoap.prototype.schedule = function() {}; FuelSoap.prototype.versionInfo = function() {}; function determineCallbackAndOptions(args, callback, options) { diff --git a/package.json b/package.json index 0a589f3..2418f62 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fuel-soap", - "version": "2.2.3", + "version": "2.2.4", "description": "Node library for performing SOAP API calls to Salesforce Marketing Cloud (formerly ExactTarget).", "main": "./lib/fuel-soap.js", "scripts": { diff --git a/test/specs/general-tests.js b/test/specs/general-tests.js index 4dc2088..5cd1bb8 100755 --- a/test/specs/general-tests.js +++ b/test/specs/general-tests.js @@ -129,7 +129,6 @@ describe('General Tests', function() { , 'extract' , 'getSystemStatus' , 'query' - , 'schedule' , 'versionInfo' ]; diff --git a/test/specs/soap-action-schedule.js b/test/specs/soap-action-schedule.js new file mode 100644 index 0000000..34fa0d0 --- /dev/null +++ b/test/specs/soap-action-schedule.js @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2018, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +'use strict'; + +var assert = require('assert'); +var proxyquire = require('proxyquire'); +var sinon = require('sinon'); + +describe('SOAP Action - schedule', function() { + var FuelSoap; + var soapRequestSpy; + var parsedOptions = { parsedOptions: true }; + var simpleVerifyTestCases = [ + { property: 'action', expected: 'Schedule' } + , { property: 'key', expected: 'ScheduleResponseMsg' } + , { property: 'retry', expected: true } + , { property: 'reqOptions', expected: parsedOptions } + ]; + + var exampleSchedule = { + Recurrence: { + $: {'xsi:type': 'MinutelyRecurrence'}, + MinutelyRecurrencePatternType: 'Interval', + MinuteInterval: '10' + }, + RecurrenceType: 'Minutely', + RecurrenceRangeType: 'EndAfter', + StartDateTime: '2019-04-10T10:10:00.046+01:00', + Occurrences: '999999' + }; + var exampleInteractions =[ + { + Interaction: { + ObjectID: '1234' + } + } + ]; + + beforeEach(function() { + FuelSoap = proxyquire('../../lib/fuel-soap', { + './helpers': { + parseReqOptions: function() { return parsedOptions; } + } + }); + soapRequestSpy = sinon.stub(FuelSoap.prototype, 'soapRequest'); + }); + + afterEach(function() { + FuelSoap.prototype.soapRequest.restore(); + }); + simpleVerifyTestCases.forEach(function(testCase) { + it('should call soapRequest with correct ' + testCase.property, function() { + // Act + FuelSoap.prototype.schedule( + 'Test', + exampleSchedule, + exampleInteractions, + 'start', + { options: true }, + function() {}); + // Assert + assert.equal(soapRequestSpy.args[0][0][testCase.property], testCase.expected); + }); + }); + it('should pass correct body to soapRequest', function() { + + // Act + FuelSoap.prototype.schedule('Test', exampleSchedule, exampleInteractions,'start',{ options: true } , function() {}); + // Assert + var actualObj = soapRequestSpy.args[0][0].req; + assert.equal(actualObj.ScheduleRequestMsg.Schedule, exampleSchedule); + assert.equal(actualObj.ScheduleRequestMsg.Interactions, exampleInteractions); + assert.ok(!actualObj.ScheduleRequestMsg.Options.QueryAllAccounts); + }); + + it('should pass callback to soapRequest when options', function() { + // Arrange + var sampleCallback = sinon.spy(); + + // Act + FuelSoap.prototype.schedule('Test', exampleSchedule, exampleInteractions,'start', { options: true }, sampleCallback); + + // Assert + assert.ok(soapRequestSpy.calledWith(sinon.match.object, sampleCallback)); + }); + + it('should add QueryAllAccounts to body when option passed', function() { + // Arrange + var sampleOptions = { queryAllAccounts: true }; + + // Act + FuelSoap.prototype.schedule('Test', exampleSchedule, exampleInteractions,'start',sampleOptions , function() {}); + + // Assert + var actualObj = soapRequestSpy.args[0][0].req; + assert.ok(actualObj.ScheduleRequestMsg.Options.QueryAllAccounts); + }); +});