From beab9d1b31cc97891572e4d4736c80e7efeb91ff Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 21 Nov 2014 18:04:20 -0800 Subject: [PATCH] CRM-15578 - crmMailing2 - Implement "Send test" buttons --- js/Common.js | 11 +++++++++++ js/angular-crmMailing2-services.js | 21 +++++++++++++++++++++ js/angular-crmMailing2.js | 16 ++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/js/Common.js b/js/Common.js index 26ae4cfaf1..a1af2f459c 100644 --- a/js/Common.js +++ b/js/Common.js @@ -721,6 +721,17 @@ CRM.strings = CRM.strings || {}; }); }; + // Convert an Angular promise to a jQuery promise + CRM.toJqPromise = function(aPromise) { + var jqDeferred = $.Deferred(); + aPromise.then( + function(data) { jqDeferred.resolve(data); }, + function(data) { jqDeferred.reject(data); } + // should we also handle progress events? + ); + return jqDeferred.promise(); + }; + /** * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference */ diff --git a/js/angular-crmMailing2-services.js b/js/angular-crmMailing2-services.js index 760307716e..f8307a75e6 100644 --- a/js/angular-crmMailing2-services.js +++ b/js/angular-crmMailing2-services.js @@ -73,6 +73,7 @@ } }); return crmApi('Mailing', 'create', params).then(function(result){ + // changes rolled back, so we don't care about updating mailing return result.values[result.id]['api.Mailing.preview'].values; }); }, @@ -93,8 +94,28 @@ } }); return crmApi('Mailing', 'create', params).then(function(recipResult){ + // changes rolled back, so we don't care about updating mailing return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values; }); + }, + + // @param mailing Object (per APIv3) + // @param testEmail string + // @param testGroup int (id#) + // @return Promise for a list of delivery reports + sendTest: function(mailing, testEmail, testGroup) { + var params = _.extend({}, mailing, { + // options: {force_rollback: 1}, + 'api.Mailing.send_test': { + mailing_id: '$value.id', + test_email: testEmail, + test_group: testGroup + } + }); + return crmApi('Mailing', 'create', params).then(function(result){ + if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id + return result.values[result.id]['api.Mailing.send_test'].values; + }); } }; }); diff --git a/js/angular-crmMailing2.js b/js/angular-crmMailing2.js index a3e7f27f5c..1d3e2c7f7b 100644 --- a/js/angular-crmMailing2.js +++ b/js/angular-crmMailing2.js @@ -171,10 +171,22 @@ }); }; $scope.sendTestToContact = function sendTestToContact() { - CRM.alert('Send test to contact, ' + $scope.testContact.email); + $scope.sendTest($scope.mailing, $scope.testContact.email, null); }; $scope.sendTestToGroup = function sendTestToGroup() { - CRM.alert('Send test to group, ' + $scope.testGroup.gid); + $scope.sendTest($scope.mailing, null, $scope.testGroup.gid); + }; + $scope.sendTest = function sendTest(mailing, testEmail, testGroup) { + var promise = crmMailingMgr.sendTest(mailing, testEmail, testGroup).then(function(deliveryInfos){ + var count = Object.keys(deliveryInfos).length; + if (count === 0) { + CRM.alert(ts('Could not identify any recipients. Perhaps the group is empty?')); + } + }); + CRM.status({ + start: ts('Sending...'), + success: ts('Sent') + }, CRM.toJqPromise(promise)); }; }); -- 2.25.1