mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
🐛 Fixed immediately sent email when scheduling email-only post
closes https://linear.app/tryghost/issue/CORE-78/email-only-scheduling-should-work-the-same-way-as-regular-posts - The email was going out at the moment of scheduling an email-only post instead of respecting the scheduled time.
This commit is contained in:
parent
e2eef2a6d3
commit
e3b56dd99f
2 changed files with 41 additions and 3 deletions
|
@ -60,10 +60,9 @@ class PostsService {
|
|||
}
|
||||
}
|
||||
|
||||
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
||||
const emailOnlyEnabled = model.related('posts_meta').get('email_only') && this.isSet('emailOnlyPosts');
|
||||
const sendEmail = model.wasChanged() && this.shouldSendEmail(model.get('status'), model.previous('status'));
|
||||
|
||||
if (postPublished || emailOnlyEnabled) {
|
||||
if (sendEmail) {
|
||||
let postEmail = model.relations.email;
|
||||
|
||||
if (!postEmail) {
|
||||
|
@ -79,6 +78,18 @@ class PostsService {
|
|||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates if the email should be tried to be sent out
|
||||
* @private
|
||||
* @param {String} currentStatus current status from the post model
|
||||
* @param {String} previousStatus previous status from the post model
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
shouldSendEmail(currentStatus, previousStatus) {
|
||||
return (['published', 'sent'].includes(currentStatus))
|
||||
&& (!['published', 'sent'].includes(previousStatus));
|
||||
}
|
||||
|
||||
handleCacheInvalidation(model) {
|
||||
let cacheInvalidate;
|
||||
|
||||
|
@ -124,3 +135,5 @@ const getPostServiceInstance = (apiVersion) => {
|
|||
};
|
||||
|
||||
module.exports = getPostServiceInstance;
|
||||
// exposed for testing purposes only
|
||||
module.exports.PostsService = PostsService;
|
||||
|
|
25
test/unit/services/posts/posts-service.test.js
Normal file
25
test/unit/services/posts/posts-service.test.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
const should = require('should');
|
||||
|
||||
const {PostsService} = require('../../../../core/server/services/posts/posts-service');
|
||||
|
||||
describe('PostsService', function () {
|
||||
describe('shouldSendEmail', function () {
|
||||
it('calculates if an email should be sent', async function () {
|
||||
const postsService = new PostsService({});
|
||||
|
||||
postsService.shouldSendEmail('published', 'draft').should.be.true();
|
||||
postsService.shouldSendEmail('published', 'scheduled').should.be.true();
|
||||
postsService.shouldSendEmail('sent', 'draft').should.be.true();
|
||||
postsService.shouldSendEmail('sent', 'scheduled').should.be.true();
|
||||
|
||||
postsService.shouldSendEmail('published', 'published').should.be.false();
|
||||
postsService.shouldSendEmail('published', 'sent').should.be.false();
|
||||
postsService.shouldSendEmail('published', 'published').should.be.false();
|
||||
postsService.shouldSendEmail('published', 'sent').should.be.false();
|
||||
postsService.shouldSendEmail('sent', 'published').should.be.false();
|
||||
postsService.shouldSendEmail('sent', 'sent').should.be.false();
|
||||
|
||||
postsService.shouldSendEmail().should.be.false();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue