From ceb5a01a6d6d8856a941db311a17bca076d4b9df Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 14 Aug 2022 12:35:11 -0400 Subject: [PATCH] SearchKit - Add contacts to mailing group in batches For the "Email - schedule/send via CiviMail" action, this switches to using the batch runner for adding contacts to the mailing group instead of doing it all at once via chaining. This prevents timeouts when creating very large mailings > 500 contacts. --- .../crmSearchBatchRunner.component.js | 2 +- .../crmSearchTaskMailing.ctrl.js | 18 ++++++++++-------- .../crmSearchTasks/crmSearchTaskMailing.html | 8 ++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js b/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js index 88c90f7f4b32..6ccffdacaf9b 100644 --- a/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchBatchRunner.component.js @@ -48,7 +48,7 @@ if (ctrl.action === 'save') { // For the save action, take each record from params and copy it with each supplied id params.records = _.transform(ctrl.ids.slice(ctrl.first, ctrl.last), function(records, id) { - _.each(_.cloneDeep(ctrl.params.records), function(record) { + _.each(_.cloneDeep(ctrl.params.records || [{}]), function(record) { record[ctrl.idField || 'id'] = id; records.push(record); }); diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js index a5f14bb30373..280bd70c86c2 100644 --- a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.ctrl.js @@ -5,6 +5,7 @@ var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), // Combine this controller with model properties (ids, entity, entityInfo) and searchTaskBaseTrait ctrl = angular.extend(this, $scope.model, searchTaskBaseTrait), + mailingId, templateTypes; this.entityTitle = this.getEntityTitle(); @@ -27,9 +28,6 @@ }); this.submit = function() { - var contacts = _.transform(ctrl.ids, function(records, cid) { - records.push({contact_id: cid}); - }); ctrl.start({ values: { title: 'Hidden Group ' + Date.now(), @@ -37,10 +35,6 @@ 'group_type:name': ['Mailing List'], }, chain: { - contacts: ['GroupContact', 'save', { - defaults: {group_id: '$id'}, - records: contacts - }], mailing: ['Mailing', 'create', { values: { name: ctrl.name, @@ -59,9 +53,17 @@ }); }; + // After running first api call to create group & mailing, + // This runs a batch of api calls to add contacts to the mailing group + this.afterGroupCreate = function(result) { + mailingId = result[0].mailing.id; + ctrl.addContacts = { + defaults: {group_id: result[0].id} + }; + }; this.onSuccess = function(result) { - window.location = CRM.url('civicrm/a#/mailing/' + result[0].mailing.id); + window.location = CRM.url('civicrm/a#/mailing/' + mailingId); }; this.onError = function() { diff --git a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html index f03ae5d90965..f9fd705449b0 100644 --- a/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html +++ b/ext/search_kit/ang/crmSearchTasks/crmSearchTaskMailing.html @@ -24,9 +24,13 @@ {{:: ts('All of the selected contacts have active email addresses.') }} -
+
{{:: ts('Creating mailing...') }}
- + +
+
+
{{:: ts('Adding contacts...') }}
+