Fixed emails sending when scheduled post is published

no issue

- the schedules controller wraps the post creation in a transaction
- we need to pass that transaction through to all other queries, especially on sqlite where a non-transaction query inside a transaction will lock up because there's only 1 connection available
- updates our model method calls to pass through the transaction options
- switches the members service `list()` call to a direct model `findAll()` call to avoid going through our pagination plugin because the raw knex query does not respect the transacting option
This commit is contained in:
Kevin Ansfield 2019-11-26 17:39:50 +00:00
parent b9dd0d2b94
commit 90905b0212

View File

@ -1,3 +1,4 @@
const _ = require('lodash');
const url = require('url');
const moment = require('moment');
const common = require('../../lib/common');
@ -51,15 +52,24 @@ const sendTestEmail = async (postModel, toEmails) => {
* @param {object} postModel Post Model Object
*/
const addEmail = async (postModel, options) => {
const {members} = await membersService.api.members.list(Object.assign({filter: 'subscribed:true'}, {limit: 'all'}));
const {emailTmpl, emails} = await getEmailData(postModel, members);
const knexOptions = _.pick(options, ['transacting', 'forUpdate']);
// TODO: this is using the Member model directly rather than the members
// service because the service is hardcoded to Member.findPage and our
// pagination plugin does not currently work with transactions
const members = await models.Member
.findAll(Object.assign({filter: 'subscribed:true'}, knexOptions))
.map(member => member.toJSON(options));
const {emailTmpl, emails} = getEmailData(postModel, members);
// NOTE: don't create email object when there's nobody to send the email to
if (!emails.length) {
return null;
}
const postId = postModel.get('id');
const existing = await models.Email.findOne({post_id: postId});
const existing = await models.Email.findOne({post_id: postId}, knexOptions);
if (!existing) {
return models.Email.add({
@ -70,7 +80,7 @@ const addEmail = async (postModel, options) => {
html: emailTmpl.html,
plaintext: emailTmpl.plaintext,
submitted_at: moment().toDate()
}, {transacting: options.transacting});
}, knexOptions);
} else {
return existing;
}