CRM-15578 - crmMailingMgr - Implement submit(). Add comments.
authorTim Otten <totten@civicrm.org>
Wed, 3 Dec 2014 02:32:49 +0000 (18:32 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 3 Dec 2014 02:34:37 +0000 (18:34 -0800)
js/angular-crmMailing2-services.js

index 7cdb56f5cc3017f8951525b1eea6f155bac0f2fa..95ba835b5820fe1568cc1ea2422610cff50b15e3 100644 (file)
@@ -3,6 +3,27 @@
     return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath;
   };
 
+  // FIXME: surely there's already some helper which can do this in one line?
+  // @return string "YYYY-MM-DD hh:mm:ss"
+  var createNow = function () {
+    var currentdate = new Date();
+    var yyyy = currentdate.getFullYear();
+    var mm = currentdate.getMonth() + 1;
+    mm = mm < 10 ? '0' + mm : mm;
+    var dd = currentdate.getDate();
+    dd = dd < 10 ? '0' + dd : dd;
+    var hh = currentdate.getHours();
+    hh = hh < 10 ? '0' + hh : hh;
+    var min = currentdate.getMinutes();
+    min = min < 10 ? '0' + min : min;
+    var sec = currentdate.getSeconds();
+    sec = sec < 10 ? '0' + sec : sec;
+    return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + sec;
+  };
+
+  // FIXME: Load status ids from DB
+  var APPROVAL_STATUSES = { Approved: "1", Rejected: "2", None: "3" };
+
   var crmMailing2 = angular.module('crmMailing2');
 
   // The representation of from/reply-to addresses is inconsistent in the mailing data-model,
         });
       },
 
+      // Save a (draft) mailing
       // @param mailing Object (per APIv3)
       // @return Promise
       save: function(mailing) {
         var params = _.extend({}, mailing, {
           'api.mailing_job.create': 0 // note: exact match to API default
         });
+
+        // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date
+        // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date
+        // is therefore not allowed. Remove this after fixing Mailing.create's contract.
+        delete params.scheduled_date;
+
         return crmApi('Mailing', 'create', params).then(function(result){
           if (result.id && !mailing.id) mailing.id = result.id;  // no rollback, so update mailing.id
+          // Perhaps we should reload mailing based on result?
           return result.values[result.id];
         });
       },
       // Schedule/send the mailing
       // @param mailing Object (per APIv3)
       // @return Promise
-      submit: function(mailing) {
-        throw 'Not implemented: crmMailingMgr.submit';
-//        var params = _.extend({}, mailing, {
-//          'api.mailing_job.create': 1 // note: exact match to API default
-//        });
-//        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];
-//        });
+      submit: function (mailing) {
+        var changes = {
+          approval_date: createNow(),
+          approver_id: CRM.crmMailing.contactid,
+          approval_status_id: APPROVAL_STATUSES.Approved,
+          scheduled_date: mailing.scheduled_date ? mailing.scheduled_date : createNow(),
+          scheduled_id: CRM.crmMailing.contactid
+        };
+        var params = _.extend({}, mailing, changes, {
+          'api.mailing_job.create': 0 // note: exact match to API default
+        });
+        return crmApi('Mailing', 'create', params).then(function (result) {
+          if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id
+          _.extend(mailing, changes); // Perhaps we should reload mailing based on result?
+          return result.values[result.id];
+        });
       },
 
       // Immediately send a test message
       // @return Promise for a list of delivery reports
       sendTest: function(mailing, testEmail, testGroup) {
         var params = _.extend({}, mailing, {
-          // options:  {force_rollback: 1},
+          // options:  {force_rollback: 1}, // Test mailings include tracking features, so the mailing must be persistent
           'api.Mailing.send_test': {
             mailing_id: '$value.id',
             test_email: testEmail,
             test_group: testGroup
           }
         });
+
+        // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date
+        // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date
+        // is therefore not allowed. Remove this after fixing Mailing.create's contract.
+        delete params.scheduled_date;
+
         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;