From 43102e472bee3903e00aa6d278d81206fead9546 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 2 Dec 2014 18:32:49 -0800 Subject: [PATCH] CRM-15578 - crmMailingMgr - Implement submit(). Add comments. --- js/angular-crmMailing2-services.js | 62 +++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/js/angular-crmMailing2-services.js b/js/angular-crmMailing2-services.js index 7cdb56f5cc..95ba835b58 100644 --- a/js/angular-crmMailing2-services.js +++ b/js/angular-crmMailing2-services.js @@ -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, @@ -209,14 +230,22 @@ }); }, + // 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]; }); }, @@ -224,15 +253,22 @@ // 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 @@ -242,13 +278,19 @@ // @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; -- 2.25.1