From 79368f565f5f16afa08b556a252c1bc2d5533b32 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 5 Sep 2022 16:14:12 +0100 Subject: [PATCH] Fixed Tier events being created when Posts are edited refs https://github.com/TryGhost/Team/issues/1875 - due to an misbehavior in our model layer, when `tiers` is set on a Post, it'll trigger a save of the Tier, and this produces an extra event in the `actions` table - mapping the Tier(s) to just the ID prevents bookshelf-relations from editing the Tier and thus prevents the extra event - also fixed tests which were implicitly assuming supplying a slug to a post would create the product --- ghost/core/core/server/models/post.js | 6 ++++++ ghost/core/test/e2e-frontend/helpers/get.test.js | 4 +++- ghost/core/test/e2e-frontend/helpers/next_post.test.js | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index faebf6b372..18118599a2 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -794,6 +794,12 @@ Post = ghostBookshelf.Model.extend({ }); } + if (this.get('tiers')) { + this.set('tiers', this.get('tiers').map(t => ({ + id: t.id + }))); + } + return sequence(ops); }, diff --git a/ghost/core/test/e2e-frontend/helpers/get.test.js b/ghost/core/test/e2e-frontend/helpers/get.test.js index a0c8380f00..b45541746f 100644 --- a/ghost/core/test/e2e-frontend/helpers/get.test.js +++ b/ghost/core/test/e2e-frontend/helpers/get.test.js @@ -72,11 +72,13 @@ describe('e2e {{#get}} helper', function () { published_at: new Date() // here to ensure sorting is not modified }); + const defaultTier = await models.Product.findOne({slug: 'default-product'}); + basicTierPost = await createPost({ slug: 'tiers-post', visibility: 'tiers', tiers: [{ - slug: 'default-product' + id: defaultTier.get('id') }], published_at: new Date() // here to ensure sorting is not modified }); diff --git a/ghost/core/test/e2e-frontend/helpers/next_post.test.js b/ghost/core/test/e2e-frontend/helpers/next_post.test.js index 44f40a4ded..0a81b6d9e2 100644 --- a/ghost/core/test/e2e-frontend/helpers/next_post.test.js +++ b/ghost/core/test/e2e-frontend/helpers/next_post.test.js @@ -54,11 +54,13 @@ describe('e2e {{#next_post}} helper', function () { published_at: new Date(2020, 0, 3) // here to ensure sorting is not modified }); + const defaultTier = await models.Product.findOne({slug: 'default-product'}); + basicTierPost = await createPost({ slug: 'tiers-post', visibility: 'tiers', tiers: [{ - slug: 'default-product' + id: defaultTier.get('id') }], published_at: new Date(2020, 0, 4) // here to ensure sorting is not modified });