Commit Graph

1243 Commits

Author SHA1 Message Date
Rishabh Garg
05b5059918 Updated portal to manage single newsletter with existing UI (#14573)
refs https://github.com/TryGhost/Team/issues/1554

- bumps portal to manage single site newsletter with old subscribe UI
- adds unit test for portal site endpoint
2022-04-26 12:31:34 +01:00
Hannah Wolfe
2d36e5881b Simplified newsletter e2e tests
- Many of these tests were using API calls to get IDs or check side effects
- This makes snap files much harder to read, where keeping tests more minimal gives us the same test coverage
- Also updated the tests to include members, so we have some real live counts
2022-04-26 12:31:34 +01:00
Thibaut Patel
ed29c7addf Filtered member email recipients based on the newsletter subscriptions (#14489)
refs https://github.com/TryGhost/Team/issues/1524

- We need to fetch the post newsletter to grab the slug as it's needed for the member NQL filter.
- We can then use the newsletter slug and append it in the existing member NQL filter.
- Removed `subscribed:true` when an email is sent to a newsletter and replaced it with the newsletter id
- Added `status:-free` when an email is sent to a newsletter with `visibility` set to `paid`
- Added tests what happens when you publish without newsletter_id
- Added tests what happens when you publish with newsletter_id

Co-authored-by: Simon Backx <simon@ghost.org>
2022-04-26 12:31:34 +01:00
Hannah Wolfe
9da57fa6bb Updated test fixtures for multiple newsletters
- Updated the member fixtures to have 6 subscribed and 2 unsubscribed members
- Added an inactive newsletter
- Updated newsletter-member relations to reflect the 6 subscribed and 2 unsubscribed, but with different cases:
   - 3 subscribed to default
   - 1 subscribed to secondary only
   - 1 subscribed to default + secondary
   - 1 subscribed to secondary + inactive
   - 1 subscribed to inactive only (i.e. not subscribed)
   - 1 not subscribed at all

- With these changes, I needed to update the members snap as 2 members appear as subscribed:false in many tests
- I also needed to update some posts regression tests, as 2 less members get emailed in 2 tests
2022-04-26 12:31:34 +01:00
Thibaut Patel
48799e1c68 Added the newsletter API ?include=count.posts and ?include=count.members query options (#14525)
refs https://github.com/TryGhost/Team/issues/1524

- This enables admins in the ghost admin to have an overview of the total posts/members associated with a newsletter.
- Follows the `?include=count.x` convention used by other resources
2022-04-26 12:31:34 +01:00
Rishabh Garg
a330165e4f Handled email unsubscribe with multiple newsletters (#14560)
refs https://github.com/TryGhost/Team/issues/1495

- removes subscription from all newsletters for a member on click of unsubscribe link in email
- allows the new multiple newsletter system to work with existing unsubscribe flow
2022-04-26 12:31:34 +01:00
Simon Backx
50ab1e0d73 Added created_at/updated_at to newsletters (#14559)
no issue

Added the missing timestamps to the newsletters table
2022-04-26 12:31:34 +01:00
Thibaut Patel
c49d2d0b7e Fixed the newsletter_id assignement in the post API (#14485)
refs https://github.com/TryGhost/Team/issues/1502

- Assigned posts to the default newsletter when sent as email
2022-04-26 12:31:34 +01:00
Simon Backx
c15cb1b5be Added newsletter_id column to subscribe events (#14553)
refs https://github.com/TryGhost/Team/issues/1478

The `newsletter_id` is nullable for now to remain compatible until we have a proper data migration + updated code to set it on inserts
2022-04-26 12:31:34 +01:00
Kevin Ansfield
a8687b35b9 Added newsletter from address verification (#14491)
refs https://github.com/TryGhost/Team/issues/1498
refs https://github.com/TryGhost/Team/issues/584

- Added newsletter `from` address verification

Co-authored-by: Hannah Wolfe <github.erisds@gmail.com>
2022-04-26 12:31:34 +01:00
Matt Hanley
298599ce91 Added show_header_name column to newsletters table
refs https://github.com/TryGhost/Team/issues/1532

- Added before the migration in https://github.com/TryGhost/Ghost/pull/14468 to populate the default newsletter
- The fixture for the default newsletter has a different value than the model and schema default
- This is because by default the newsletter name is the same as the site title, and the site title is already shown
2022-04-26 12:31:34 +01:00
Naz
2a592a9bf3 Added test coverage to settings bread service
no issue

- This serivce was missing any unit test coverage. Covering few cases gives a good groundwork to expand on
2022-04-22 16:31:56 +08:00
Naz
cc71bbfd61 Hooked up api version compatibility middleware
refs https://github.com/TryGhost/Toolbox/issues/280

- ctd of putting pieces together to allow Ghost notifying owner and admin users about version mismatch errors
-  The `@tryghost/mw-api-version-mismatch` in a combination with api version compatibility service make the whole notification process play nicely :)
- The flow of the logic from the request to a sent notification email is following:
1. Request comes is with an Accept-Version header that's behind current Ghost version and is not supported
2. mw-error-handler middleware's 'resourceNotFound' detects such request and returns a 406 with a special 'code' identifying if the version of the client is ahead or behind
3. mw-api-version-mismatch intercepts the 406 request with "code === 'UPDATE_CLIENT'` and calls up APIVersionCompatibilityService
4. emails are sent out to active owner and admin users

- The above flow is also illustratd in the e2e tests that come with the changeset
2022-04-22 16:31:56 +08:00
Rishabh
2ab78bcdc9 Updated settings test snapshot for new flag 2022-04-22 12:50:33 +05:30
Fabien 'egg' O'Carroll
19a08cd11d
Replaced stats service with @tryghost/stats-service (#14527)
- Pulls logic out of Ghost core and into the Analytics repository.
- Test coverage has increased for the stats service
- Interface simplified
2022-04-21 14:57:07 +01:00
Naz
1ecb837981 Added version_notifications key to settings table
refs https://github.com/TryGhost/Toolbox/issues/292

- When version missmatch handling is done in Ghost we need to store the 'Accept-Version' header values that have been already processed in the past (to avoid sending notifications about the same mismatch multiple times)
- The `version_notifications` will be storing an array with handled versions like so: `['v3.44', 'v4.23', 'v4.39']`.
- The emailing logic and processing is slightly similar to how "notification" key is handled, that's why I've placed the definition of this new key close by.
2022-04-21 20:34:19 +08:00
Simon Backx
b9646cceb1
Updated members API to return subscriptions' offers from the offer_id (#14515)
refs https://github.com/TryGhost/Team/issues/1520

**Changes in members repo**

Bumped to `5.12.0`, with the following changes:

- Compare differences via https://github.com/TryGhost/Members/compare/%40tryghost/members-api%405.11.1...%40tryghost/members-api%405.12.0
- Instead of doing the matching of the offers and subscriptions by looking at the offer redemptions, we can now look at the offer_id from subscriptions.
- This also fixes an issue where we don't attach the offer object to subscriptions in the members' browse method
- Updated browse behaviour to match the read behaviour of members (product relation needs to get loaded because it is missing in member.products if the subscription is expired).

**Tests**
- Includes test to see if the API correctly returns the offer object when fetching one or multiple members
- Check if the return format is the same for the read, edit and browse members admin API endpoints (offer was missing in subscriptions)
- Snapshot files have been updated because now the offer is returned in subscriptions (content length increased)
2022-04-21 12:06:06 +02:00
Thibaut Patel
52a855f314
Added the newsletter read endpoint (#14520)
refs https://github.com/TryGhost/Team/issues/1533

- Retrieves one newsletter
- Makes the newsletter resource consistent with the other resources
- Solves an issue with the admin expecting the route to exist
2022-04-21 09:59:44 +02:00
Naz
f28e65b4e7 Added staus code assertions to version test suite
refs https://github.com/TryGhost/Toolbox/issues/280

- All e2e tests should be checking returned status code! This was missed when writing tests
2022-04-21 12:45:21 +08:00
Kevin Ansfield
fbaf49b882 Updated settings API snapshot for new labs flag
refs 05da593516

- test was failing because the `labs` property returned by the settings endpoint changed after the new labs flag was added
2022-04-20 16:29:51 +01:00
Thibaut Patel
ae664e9cad
Added newsletter read permission (#14519)
refs https://github.com/TryGhost/Team/issues/1533

- Needed to create the read newsletter endpoint to make the newsletter resource more consistent with the other resources
- Read is available to admins like other newsletter actions
2022-04-20 15:25:41 +02:00
Simon Backx
a3215a6edf
Updated calculation of MRR history to use mrr column (#14481)
refs https://github.com/TryGhost/Team/issues/1518

- Uses mrr column now
- Doesn't account for `cancel_at_period_end` until a migration fixes the MRR values: https://github.com/TryGhost/Ghost/pull/14480
2022-04-19 17:07:20 +02:00
Simon Backx
0c72e78e6a
Stored offer_id in subscriptions (#14488)
refs https://github.com/TryGhost/Team/issues/1519

**Tests:**
- Tests whether the metadata from an offer is read correctly and stored in the database
- Test that invalid offer ids are ignored

**Changes in members repository:** 
- Compare changes here: https://github.com/TryGhost/Members/compare/%40tryghost/members-api%405.9.2...%40tryghost/members-api%405.11.1
- The `offer_id` column of subscriptions is set based on the coupon id from Stripe
- `getByStripeCouponId` method added in the offers repository (required to look up an offer from a stripe_coupon_id)
- the `members-payments` package was bumped twice (once for changes, once for undoing those changes, my bad). Nothing else has changed in that package.
2022-04-19 17:06:53 +02:00
Hannah Wolfe
1d121c52f4
Replaced white/black list terminology
refs 92986b77e3

- I thought we did this a while ago, but uses in comments and elsewhere in the codebase were missed
2022-04-19 11:19:59 +01:00
Rishabh Garg
88ed60d079
Updated default newsletter subscription logic for members (#14482)
refs https://github.com/TryGhost/Team/issues/1469

Previously, members were subscribed to all available newsletters by default when added. This change updates the default newsletters subscription for member to take into account newsletter preferences for auto opt-in(`subscribe_on_signup`) as well as `visibility`.
2022-04-18 13:48:44 +05:30
Naz
d9ac05f97a Fixed snapshot tests using version headers
refs 3381dae1e6
refs 4a4b0cc8a6
refs f432ee9aa6

- The initial tests didn't take into account the veersion of the Ghost instance would be changing contantly. The "context" part of the returned version missmatch errors should be a regex with only partially hardcoded message.
2022-04-18 10:35:29 +08:00
Daniel Lockyer
f432ee9aa6
Fixed linting re: skipped tests
refs 4a4b0cc8a6

- we're temporarily skipping the tests until they're fixed but then
  linting started failing........
2022-04-15 16:24:07 +01:00
Daniel Lockyer
4a4b0cc8a6
Skipped tests containing faulty snapshot
refs 3381dae1e6

- these tests contain snapshots that hardcode the Ghost version into them
- this is incorrect but we need to unblock `main` so I'm commenting them
  out until someone can look at them
2022-04-15 16:18:26 +01:00
Simon Backx
9b23424e48
Added offer_id column to members_stripe_customers_subscriptions (#14487)
refs https://github.com/TryGhost/Team/issues/1519

Migration that adds the (nullable) offer_id column to members_stripe_customers_subscriptions.
- New reliable way to know which offer is active for a given subscription (currently we compare the tier and cadence in offer redemptions)
- We'll create a separate migration to backfill all the offer_ids, but only after we updated the code to also store them correctly for new or updated subscriptions (https://github.com/TryGhost/Team/issues/1520)
- Allows us to backfill the MRR of all subscriptions to account for forever offers
2022-04-15 13:47:15 +02:00
Simon Backx
a696d99f20
Added tests to check MRR and MRR_delta for subscriptions with offers (#14470)
refs https://github.com/TryGhost/Team/issues/1451
refs https://github.com/TryGhost/Team/issues/1456

Tests for updating `members-api` package to `5.9.0` (happened in earlier commit), which includes the following changes since `5.8.0`:
* Simplifies the calculation of MRR deltas, which will make it easier to update MRR to include offers and cancellations in the future.
* Adjusted MRR and MRR delta calculation to consider "forever" duration offers (only if dashboardv5 flag is enabled)
* Uses the discount information from Stripe to calculate the MRR (this was the easiest way to include it + also supports manually created discounts from users)
* Full difference in https://github.com/TryGhost/Members/pull/387

New tests:
- Checks calculation of MRR when using forever vs repeating discounts
- Checks calculation of MRR with dashboard v5 flag enabled/disabled
- Checks calculation of MRR for yearly and monthly subscriptions with forever offers
- Checks updates of MRR and MRR_delta when adding a forever discount to an existing subscription
- Checks updates of MRR and MRR_delta when canceling a subscription with a discount
2022-04-15 11:16:50 +02:00
Rishabh Garg
1362750640
Updated newsletter fixtures to remove nullable sender name (#14479)
refs https://github.com/TryGhost/Team/issues/1513

Since `sender_name` for newsletter is now nullable field, this change removes hardcoded `sender_name` from fixtures.
2022-04-14 23:45:59 +05:30
Rishabh Garg
5218cf194d
Updated newsletter sender name column as nullable (#14476)
refs TryGhost/Team#1513

- nullable `sender_name` allows us to use auto fallback of site title for sender name without setting any explicit value for it.
2022-04-14 17:42:20 +05:30
Naz
4dcb229b35 Removed models dependency from Webhook Service test
no issue

- There's no need to init whole models module in this test. Simplified mocks are easier to understand without any "magic" that's going on behind model's module "init()" call
2022-04-14 16:12:05 +08:00
Naz
0139c9c3ad Fixed failing test
refs fa82722b03
2022-04-14 15:33:24 +08:00
Rishabh Garg
c6da6afc9e
Handled members newsletter preference on signup (#14473)
refs https://github.com/TryGhost/Team/issues/1490

With multiple newsletters, members can choose their newsletter subscription preference in Portal while signing up. This change handles newsletter preference data for both free and paid members via magic link/stripe checkout and saves it for the newly created member.
2022-04-13 20:02:45 +05:30
Thibaut Patel
7a55e1a60a
Added the newsletter_id query string to the post edit API route (#14449)
refs https://github.com/TryGhost/Team/issues/1502

- Support the `newsletter_id` only when sending a newsletter
- Default to the default newsletter when `newsletter_id` isn't specified
- Ignore the `newsletter_id` parameter when passed in the post body
2022-04-13 15:27:43 +02:00
Thibaut Patel
ffc57301f6
Fixed the newsletters api permissions (#14458)
refs https://github.com/TryGhost/Team/issues/1504

- The permissions were missing in the fixture file
- This caused some Ghost installs to not have the right permissions
- This is fixed by adding the missing permissions to the fixture file and creating a migration to resolve the missing permissions
2022-04-13 12:02:06 +02:00
Naz
4c2bcba5f0 Added example test case for a 404 vs 406
closes https://github.com/TryGhost/Toolbox/issues/291

- When a legit Resource 404 is returned by the API we want to make sure it's not shadowed by a 406. That would be confusing, eh?
2022-04-13 12:53:49 +08:00
Rishabh
9f7bb5b65b Updated snapshot members test
refs e6f96d4711
2022-04-12 21:07:47 +05:30
Rishabh Garg
e6f96d4711
Added filtering for members api on newsletter subscriptions (#14460)
refs https://github.com/TryGhost/Team/issues/1484

While sending a post to a specific newsletter, we'll need to get list/count of members eligible to receive the post. This change enables members admin API to filter list of members on specific newsletter by their slug.
2022-04-12 21:01:49 +05:30
Rishabh Garg
95670eed52
Handled default newsletter fixtures setup (#14459)
closes https://github.com/TryGhost/Team/issues/1491

With multiple newsletters feature, a site should always have at-least one newsletter by default. Also, as with the default product, the default newsletter also needs to be renamed to the site title during the setup flow.

- adds default newsletter to main and test fixtures
- updates setup flow to rename newsletter name and sender name to site title
- updates model to extend default value for fields
- updates test
2022-04-12 19:44:21 +05:30
Fabien 'egg' O'Carroll
baf9c1b61b
Updated MRR events & stored current subscription MRR
refs https://github.com/TryGhost/Team/issues/1456
refs https://github.com/TryGhost/Team/issues/1454
refs https://github.com/TryGhost/Team/issues/1302
refs https://github.com/TryGhost/Team/issues/1453

This includes changes to store MRR on subscriptions, as well as to store
events for both cancellation and expiration of subscriptions. Cancellation
events will cause a change in MRR when the `dashboardV5` flag is enabled.

Co-authored-by: Simon Backx <simon@ghost.org>
2022-04-12 12:22:26 +01:00
Naz
3381dae1e6 Added missing snapshots
refs eee8f364de

- Part of the snapshot file was not commited with a referenced commit
- Also while at it, had another look at naming used and have cleared up couple places
2022-04-12 17:03:23 +08:00
Simon Backx
2db746e559
Added mrr column to subscriptions (#14448)
refs https://github.com/TryGhost/Team/issues/1457

We want to save the MRR with a subscription to simplify the calculation of the total MRR once, in 5.0, we also take 'forever' offers into account into the MRR (so we can just SUM the MRR of all subscriptions).

- Sets the MRR to 0 for now.
- Separate commit will fill in all the values in a data migration, but this needs to get merged first because we need this new column in order to update the members-api package (so we already save the MRR before doing the data migration).
- Updated `test/e2e-api/admin/legacy-members.test.js` with improved body assertions.
2022-04-12 11:02:18 +02:00
Rishabh Garg
cb76b738ce
Fixed newsletter schema column validation (#14456)
refs https://github.com/TryGhost/Team/issues/1500

- newsletter schema columns were using incorrect syntax for `isIn` validations
- adds schema test to make sure schema `isIn` validations are in correct format
2022-04-11 15:05:02 +05:30
Hannah Wolfe
95faf54e73
Moved route-settings tests to their own folder
- the route settings code was factored out into its own service, but the tests were not moved
- this moves them so it's clearer what tests are for what service
2022-04-08 16:53:11 +01:00
Thibaut Patel
1ba72edce6
Added the emails-newsletters relation (#14434)
refs https://github.com/TryGhost/Team/issues/1497

- This enables us to keep track of which newsletter an email was sent to even if the related post is deleted.
- Adds the `newsletter_id` property to the email API
2022-04-08 16:16:23 +02:00
Rishabh Garg
b0df387deb
Cleaned up newsletter schema migrations (#14441)
refs https://github.com/TryGhost/Team/issues/1500

The newsletter table schema has bunch of changes to go through for new and existing columns, this consolidates the schema changes into a single re-create table migration that drops and adds the newsletter table with correct schema. The table re-create migration needs to run before any of the tables using newsletter as foreign key. The changes include -

- new columns for design related fields
- new slug column for filtering
- unique constraint to `name` column
- remove `default` column (noops the existing default column migration)
- `sender_reply_to` has a default of newsletter and a validation of ['newsletter', 'support']
- updated default values for `subscribe_on_signup` and  `recipient_filter`
2022-04-08 19:06:30 +05:30
Fabien 'egg' O'Carroll
7ff0247e8d
Added subscription_id to MRR events (#14445)
* Added subscription_id to MRR events

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

As part of 5.0 we want to have cancelled evetns affect MRR. We are going
to backfill and begin populating cancelled events _before_ 5.0 however,
adding a migration to set the MRR deltas as part of 5.0

This migration will need to associate the cancelled events with a
subscription, so we are adding the subscription_id now. This will allow
us to guarantee that all cancelled events will have a subscription_id.

The column is missing a NOT NULL and REFERENCES constraint because
we have not populated the missing values. These will be added in the future
once we have populated the column for all rows
2022-04-08 14:04:40 +01:00
Fabien 'egg' O'Carroll
022c8c8e69
Added support for 'FOR UPDATE' lock (#14433)
refs https://github.com/TryGhost/Team/issues/1248

This is the underlying cause of the problems we've seen whilst handling
Stripe webhooks. A transaction ensures that the operations are atomic,
but not that they can run concurrently.

If you have some code which does this, concurrently:
1. Starts a transaction
2. Reads a value
3. Changes the values
4. Ends the transaction

Without applying the `FOR UPDATE` lock - you will have both sequenes
read the same value at step 2.

With the `FOR UPDATE` lock - one of the sequences will hang at step 2,
waiting for the other transaction to end, at which point it will resume
and read the _changed_ value.

Because the `edit` method explicitly does a read followed by a write, we
have also add the `FOR UPDATE` lock to this by default, to avoid any race
conditions. This does however require that `edit` is called within a
transaction. An issue here https://github.com/TryGhost/Team/issues/1503
considers running in a transaction by default.
2022-04-08 12:52:33 +01:00