});
// The crmMailingMgr service provides business logic for loading, saving, previewing, etc
- angular.module('crmMailing').factory('crmMailingMgr', function ($q, crmApi, crmFromAddresses) {
+ angular.module('crmMailing').factory('crmMailingMgr', function ($q, crmApi, crmFromAddresses, crmQueue) {
+ var qApi = crmQueue(crmApi);
var pickDefaultMailComponent = function pickDefaultMailComponent(type) {
var mcs = _.where(CRM.crmMailing.headerfooterList, {
component_type: type,
get: function get(id) {
var crmMailingMgr = this;
var mailing;
- return crmApi('Mailing', 'getsingle', {id: id})
+ return qApi('Mailing', 'getsingle', {id: id})
.then(function (getResult) {
mailing = getResult;
return $q.all([
// @return Promise
'delete': function (mailing) {
if (mailing.id) {
- return crmApi('Mailing', 'delete', {id: mailing.id});
+ return qApi('Mailing', 'delete', {id: mailing.id});
}
else {
var d = $q.defer();
// @return Promise an object with "subject", "body_text", "body_html"
preview: function preview(mailing) {
if (CRM.crmMailing.workflowEnabled && !CRM.checkPerm('create mailings') && !CRM.checkPerm('access CiviMail')) {
- return crmApi('Mailing', 'preview', {id: mailing.id}).then(function(result) {
+ return qApi('Mailing', 'preview', {id: mailing.id}).then(function(result) {
return result.values;
});
}
}
});
delete params.recipients; // the content was merged in
- return crmApi('Mailing', 'create', params).then(function(result) {
+ return qApi('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;
});
}
});
delete params.recipients; // the content was merged in
- return crmApi('Mailing', 'create', params).then(function (recipResult) {
+ return qApi('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;
});
},
+ previewRecipientCount: function previewRecipientCount(mailing) {
+ // To get list of recipients, we tentatively save the mailing and
+ // get the resulting recipients -- then rollback any changes.
+ var params = angular.extend({}, mailing, mailing.recipients, {
+ name: 'placeholder', // for previewing recipients on new, incomplete mailing
+ subject: 'placeholder', // for previewing recipients on new, incomplete mailing
+ options: {force_rollback: 1},
+ 'api.mailing_job.create': 1, // note: exact match to API default
+ 'api.MailingRecipients.getcount': {
+ mailing_id: '$value.id'
+ }
+ });
+ delete params.recipients; // the content was merged in
+ return qApi('Mailing', 'create', params).then(function (recipResult) {
+ // changes rolled back, so we don't care about updating mailing
+ return recipResult.values[recipResult.id]['api.MailingRecipients.getcount'];
+ });
+ },
+
// Save a (draft) mailing
// @param mailing Object (per APIv3)
// @return Promise
delete params.recipients; // the content was merged in
- return crmApi('Mailing', 'create', params).then(function(result) {
+ return qApi('Mailing', 'create', params).then(function(result) {
if (result.id && !mailing.id) {
mailing.id = result.id;
} // no rollback, so update mailing.id
approval_date: 'now',
scheduled_date: mailing.scheduled_date ? mailing.scheduled_date : 'now'
};
- return crmApi('Mailing', 'submit', params)
+ return qApi('Mailing', 'submit', params)
.then(function (result) {
angular.extend(mailing, result.values[result.id]); // Perhaps we should reload mailing based on result?
return crmMailingMgr._loadJobs(mailing);
delete params.recipients; // the content was merged in
- return crmApi('Mailing', 'create', params).then(function (result) {
+ return qApi('Mailing', 'create', params).then(function (result) {
if (result.id && !mailing.id) {
mailing.id = result.id;
} // no rollback, so update mailing.id
});
result = dialogService.open('previewDialog', templates[mode], content, options);
});
- crmStatus({start: ts('Previewing'), success: ''}, p);
+ crmStatus({start: ts('Previewing...'), success: ''}, p);
return result;
},