Fixed 'sent' status setting when publishing a post

refs https://github.com/TryGhost/Team/issues/947

- During the work of the UI and moving `email_only` flag to publish menu it created the situation where the publishing of the post was at the same time as adding `email_only` flag, resulted in not picking up teh `sent` status as the `posts_meta` model and record were's available during save.
- Adding the incoming attribute check for email_only flag covers this situation
This commit is contained in:
Naz 2021-08-26 22:25:34 +04:00
parent b895b87add
commit 35e23636ae
2 changed files with 46 additions and 2 deletions

View File

@ -654,7 +654,8 @@ Post = ghostBookshelf.Model.extend({
// NOTE: this is a stopgap solution for email-only posts where their status is unchanged after publish
// but the usual publis/send newsletter flow continues
if (model.related('posts_meta').get('email_only') && (newStatus === 'published') && this.hasChanged('status')) {
const hasEmailOnlyFlag = _.get(attrs, 'posts_meta.email_only') || model.related('posts_meta').get('email_only');
if (hasEmailOnlyFlag && (newStatus === 'published') && this.hasChanged('status')) {
this.set('status', 'sent');
}

View File

@ -365,7 +365,7 @@ describe('Posts API (canary)', function () {
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('posts/')}${res.body.posts[0].id}/`);
const publishedRes = await request
.put(localUtils.API.getApiQuery(`posts/${res.body.posts[0].id}/?email_recipient_filter=all&send_email_when_published=true`))
.put(localUtils.API.getApiQuery(`posts/${res.body.posts[0].id}/?email_recipient_filter=all`))
.set('Origin', config.get('url'))
.send({
posts: [{
@ -385,6 +385,49 @@ describe('Posts API (canary)', function () {
publishedRes.body.posts[0].email.email_count.should.equal(8);
});
it('publishes a post while setting email_only flag sends an email', async function () {
const res = await request
.post(localUtils.API.getApiQuery('posts/'))
.set('Origin', config.get('url'))
.send({
posts: [{
title: 'Email me'
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(201);
should.exist(res.body.posts);
should.exist(res.body.posts[0].title);
res.body.posts[0].title.should.equal('Email me');
res.body.posts[0].email_only.should.be.false();
res.body.posts[0].status.should.equal('draft');
should.exist(res.headers.location);
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('posts/')}${res.body.posts[0].id}/`);
const publishedRes = await request
.put(localUtils.API.getApiQuery(`posts/${res.body.posts[0].id}/?email_recipient_filter=paid`))
.set('Origin', config.get('url'))
.send({
posts: [{
status: 'published',
email_only: true,
updated_at: res.body.posts[0].updated_at
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
should.exist(publishedRes.body.posts);
publishedRes.body.posts[0].status.should.equal('sent');
should.exist(publishedRes.body.posts[0].email);
publishedRes.body.posts[0].email.email_count.should.equal(5);
});
it('read-only value do not cause errors when edited', function () {
return request
.get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[0].id}/`))