Merge pull request #5047 from colemanw/CRM-15898
[civicrm-core.git] / js / angular-crmMailingAB / services.js
index fd656bf346c1bf9bde7fda3cde9711fbd626b767..e23c905b1feab15f1aedd091497d84679e1bdc1e 100644 (file)
@@ -1,22 +1,18 @@
 (function (angular, $, _) {
 
-  // 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;
-  };
+  function OptionGroup(values) {
+    this.get = function get(value) {
+      var r = _.where(values, {value: '' + value});
+      return r.length > 0 ? r[0] : null;
+    };
+    this.getByName = function get(name) {
+      var r = _.where(values, {name: '' + name});
+      return r.length > 0 ? r[0] : null;
+    };
+    this.getAll = function getAll() {
+      return values;
+    };
+  }
 
   angular.module('crmMailingAB').factory('crmMailingABCriteria', function () {
     // TODO Get data from server
       '2': {value: '2', name: 'From names', label: ts('Test different "From" lines')},
       '3': {value: '3', name: 'Two different emails', label: ts('Test entirely different emails')}
     };
-    return {
-      get: function get(value) {
-        var r = _.where(values, {value: '' + value});
-        return r.length > 0 ? r[0] : null;
-      },
-      getAll: function getAll() {
-        return values;
-      }
+    return new OptionGroup(values);
+  });
+
+  angular.module('crmMailingAB').factory('crmMailingABStatus', function () {
+    // TODO Get data from server
+    var values = {
+      '1': {value: '1', name: 'Draft', label: ts('Draft')},
+      '2': {value: '2', name: 'Testing', label: ts('Testing')},
+      '3': {value: '3', name: 'Final', label: ts('Final')}
     };
+    return new OptionGroup(values);
   });
 
   // CrmMailingAB is a data-model which combines an AB test (APIv3 "MailingAB"), three mailings (APIv3 "Mailing"),
@@ -44,7 +42,7 @@
   //   abtest.load().then(function(){
   //     alert("Mailing A is named "+abtest.mailings.a.name);
   //   });
-  angular.module('crmMailingAB').factory('CrmMailingAB', function (crmApi, crmMailingMgr, $q, CrmAttachments) {
+  angular.module('crmMailingAB').factory('CrmMailingAB', function (crmApi, crmMailingMgr, $q, CrmAttachments, crmNow) {
     function CrmMailingAB(id) {
       this.id = id;
       this.mailings = {};
           crmMailingAB.mailings.b = crmMailingMgr.create();
           crmMailingAB.mailings.c = crmMailingMgr.create();
           crmMailingAB.attachments.a = new CrmAttachments(function () {
-            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_a']};
+            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_a};
           });
           crmMailingAB.attachments.b = new CrmAttachments(function () {
-            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_b']};
+            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_b};
           });
           crmMailingAB.attachments.c = new CrmAttachments(function () {
-            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_c']};
+            return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_c};
           });
 
           var dfr = $q.defer();
           .then(function () {
             return crmApi('MailingAB', 'create', crmMailingAB.ab)
               .then(function (abResult) {
-                crmMailingAB.ab.id = abResult.id;
+                crmMailingAB.id = crmMailingAB.ab.id = abResult.id;
               });
           })
           .then(function () {
       },
       // Schedule the test
       // @return Promise CrmMailingAB
-      // Note: Submission may cause the server state to change. Consider abtest.submit().then(abtest.load)
-      submit: function submit(newStatus) {
+      // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
+      submitTest: function submitTest() {
+        var crmMailingAB = this;
+        var params = {
+          id: this.ab.id,
+          status: 'Testing',
+          approval_date: crmNow(),
+          scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : crmNow()
+        };
+        return crmApi('MailingAB', 'submit', params)
+          .then(function () {
+            return crmMailingAB;
+          });
+      },
+      // Schedule the final mailing
+      // @return Promise CrmMailingAB
+      // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...)
+      submitFinal: function submitFinal() {
         var crmMailingAB = this;
         var params = {
-          id: this.id,
-          status: newStatus,
-          approval_date: createNow(),
-          scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : createNow()
+          id: this.ab.id,
+          status: 'Final',
+          approval_date: crmNow(),
+          scheduled_date: this.mailings.c.scheduled_date ? this.mailings.c.scheduled_date : crmNow()
         };
         return crmApi('MailingAB', 'submit', params)
           .then(function () {