Commit Graph

49 Commits

Author SHA1 Message Date
Simon Backx
6ab29c14e4 Fixed OfferCreatedEvent being dispatched before the transaction has been committed (#391)
refs https://github.com/TryGhost/Ghost/pull/14488

**Bug:**
When creating a new offer, sometimes no discount object is created in Stripe (only in MySQL). This issue causes tests to fail in https://github.com/TryGhost/Ghost/pull/14488

**Cause:**
When creating an offer with the OffersAPI, we create a new transaction to save the offer and check for uniqueness. Inside that transaction, we dispatch an OfferCreatedEvent event before the transaction is committed to the database.

When listening for this event in the `members-payments` package, we create the discount object in Stripe and save the stripe_coupon_id to the newly created offer. But because (in most cases) the offer doesn't yet exist in the database, that listener fails without warning and exits early.

**Solution:**
Creating a discount in Stripe should only happen after we succesfully committed the offer to the database. If we don't wait for the commit, the offer often (by chance) won't exist in the database, so the coupon won't be created inside Stripe. The solution is to wait for the transaction to succeed/be committed before dispatching the event.
2022-04-19 12:47:03 +02:00
Simon Backx
41e0aa17ff Updated offer_id matching logic to use stripe_coupon_id instead of metadata (#390)
refs https://github.com/TryGhost/Team/issues/1519

- Removed offer metadata again from offer (added in one of the previous commits)
- Added `getByStripeCouponId` method in offer repository (required to find an offer based on the stripe_coupon_id)
- Match discounts from Stripe based on the stripe_coupon_id instead of metadata
2022-04-19 11:21:48 +02:00
Hannah Wolfe
3dcf85d5e4 Ensured correct usage of @tryghost/errors everywhere
refs: 23b383bedf

- @tryghost/error constructors take an object, not a string - the expectation is that message, context & help should all be set
- This does the bare minimum and just ensures message is set correctly
2022-02-15 12:30:36 +00:00
Fabien O'Carroll
3efb2f1f04 Required OfferName to be a maximum of 40 characters
refs https://github.com/TryGhost/Team/issues/1236

We want to be able to use the OfferName as the name property for a
Stripe Coupon - which has a maximum character length of 40.
2021-11-25 12:34:33 +02:00
Fabien O'Carroll
0b9c9968d0 Added initial tests for Offers
refs https://github.com/TryGhost/Team/issues/1198
2021-11-07 23:10:10 +02:00
Fabien O'Carroll
668a9d47ba Improved error messages for Offers API
no-issue
2021-10-22 16:48:52 +02:00
Fabien O'Carroll
a953b530c3 Returned null when updateOffer does not find Offer
no-issue

This is another improvement for the API so that we can correctly handle
404's
2021-10-22 14:43:52 +02:00
Fabien O'Carroll
49f325dde4 Handled missing Offer when reading
no-issue

When attempting to read a non-existent offer we were running into issues
with calling toJSON() on `null`. This updates the handling to explicitly
return null - so that the controller can correctly throw a NotFoundError
2021-10-22 14:13:04 +02:00
Fabien O'Carroll
b6234d6e96 Allowed OfferTitle to be empty
refs https://github.com/TryGhost/Team/issues/1163

This allows users to not provide a title for an Offer. We store the lack
of a title as `NULL` in the DB, but we will always provide a string to
the API so that the title can safely be used in HTML.
2021-10-22 13:41:09 +02:00
Fabien O'Carroll
ff2da8a417 Added and emitted events for Offer Created
refs https://github.com/TryGhost/Team/issues/1166

Since we removed the creation of coupons from the Offers module, we must
emit events so that the Payments module can handle creating Coupons when
Offers are created.

We also export the events from the module so that they can be listened
to by the Payments module.

We also export other internals of the module so that the types can be
used.
2021-10-21 15:40:55 +02:00
Fabien O'Carroll
947fa74b9e Removed stripe_coupon_id handling from Offers
refs https://github.com/TryGhost/Team/issues/1166

This will be handled by a payments module instead. In order to
disconnect Stripe we must delete all Stripe related data, which means an
Offer doesn't inherently have a stripe coupon id. Instead we can use a
payments service which will get/create the coupon for us when we need
it.
2021-10-21 15:40:55 +02:00
Fabien O'Carroll
e744c0f82e Fixed issue with updating offer to empty description
no-issue

Because we were checking for truthyness rather than existence when
updating properties on an Offer - it was impossible to set the
description to a blank string, as this is falsy.
2021-10-19 11:00:18 +02:00
Fabien O'Carroll
dec46383b7 Updated API to use snake_case for redemption_count
no-issue
2021-10-18 17:29:30 +02:00
Fabien O'Carroll
05619a193c Included redemptions in OfferDTO
refs https://github.com/TryGhost/Team/issues/1132

The DTO is the object which we expose externally so this adds the
Redemptions property for consumption by our external API's
2021-10-18 17:26:34 +02:00
Fabien O'Carroll
dbf549b664 Fixed count for redemptions
no-issue

You must use `where` rather than `forge` when generating counts.
2021-10-18 17:26:34 +02:00
Fabien O'Carroll
3cd26bc11b Updated OfferRepository to handle redemptions
refs https://github.com/TryGhost/Team/issues/1132

The OfferRepository needs to read from the OfferRedemptionModel to get
this data.
2021-10-18 17:26:34 +02:00
Fabien O'Carroll
34c0a7f6af Added redemptionCount to Offer
refs https://github.com/TryGhost/Team/issues/1132

This is a read-only property for tracking how many times an Offer has
been redeemed
2021-10-18 15:17:31 +02:00
Fabien O'Carroll
47ad10629e Used correct methods for reading/writing to db
no-issue

Using `save` was a placeholder and isn't the correct way to interact
with our model layer.
2021-10-15 11:08:30 +02:00
Fabien O'Carroll
c2f85d3742 Used isEqual to compare ValueObjects
no-issue

This ensures that ValueObjects can contain non-primitive types.
2021-10-14 14:56:37 +02:00
Fabien O'Carroll
53d24e501d Fixed Stripe Checkout using Offers
refs https://github.com/TryGhost/Members/commit/5172e40646

When we updated to use the OffersAPI instead of OfferRepository this was
missed, and we were passing blank coupon to Stripe Checkout. This should
eventually be replaced with a call like `getCoupon(offerId)` from a
payments service.
2021-10-14 12:02:39 +02:00
Fabien O'Carroll
96e87d6798 Ensured that Offers can be filtered on status
refs https://github.com/TryGhost/Team/issues/1131

This adds a mapping between the status property used in the domain & API
and the active column used in the database. As we only have the usecase
of filtering by `status` right now, we have not added support for all
the other columns. Instead of these potentially erroring where the
column name does not match the property name in the domain/api - we've
added a transformer which will ignore all filters for properties other
than `status`. This follows postels law, in that we can be liberal with
the filters we accept, but conservative in the ones we implement.
2021-10-12 18:36:04 +02:00
Fabien O'Carroll
1e4b5c792a Supported using NQL filter to get Offers
refs https://github.com/TryGhost/Team/issues/1131

This adds initial support for pass through a filter to the Model layer,
so that we can fetch Offers based on an NQL filter.
2021-10-12 18:32:48 +02:00
Fabien O'Carroll
facbfcfa8e Added support for Offer status to API
refs https://github.com/TryGhost/Team/issues/1131

- Includes `status` on OfferDTO so client can use it
- Allows editing `status` of Offers
- Allows setting initial `status` when creating Offers
2021-10-12 17:11:54 +02:00
Fabien O'Carroll
04b9944e67 Defaulted to 'active' status when creating Offers
refs https://github.com/TryGhost/Team/issues/1131

This ensures that Offers are active by default.
2021-10-12 17:10:51 +02:00
Fabien O'Carroll
a772f5b82f Wired up Offer status to the OfferRepository
refs https://github.com/TryGhost/Team/issues/1131

This allows us to persist and hydrate the Offer status from our
database.
2021-10-12 15:29:13 +02:00
Fabien O'Carroll
1f936357d9 Added concept of OfferStatus to domain model
refs https://github.com/TryGhost/Team/issues/1131

This allows us to model the behaviour of archived & active offers, as
well as allowing us to set their status on the model.
2021-10-12 15:27:27 +02:00
Fabien O'Carroll
1312943f5b Updated Offer to only change code once
no-issue

This simplifies the handling of updating redirects for a code, and
doesn't affect our application layer because we never have the need to
change a code twice.

In future this should be replaced with events at the domain level - so
that we do not have to track changed properties and instead a redirect
service can listen to events, which would be dispatched on a successful
save by the repository.
2021-10-08 13:02:22 +02:00
Fabien O'Carroll
08d3e6e99c Allowed for OfferDescription to be null/empty
refs https://github.com/TryGhost/Team/issues/1083

OfferDescription is not a required field, so we must not throw when it
is falsy or not present.
2021-10-08 12:40:57 +02:00
Fabien O'Carroll
35f150bcf4 Moved errors to domain/errors
no-issue

More cleanup to pull files into their appropriate responsibility
2021-10-08 12:31:11 +02:00
Fabien O'Carroll
9bcd25fe5e Moved application concerns to an application dir
no-issue

Shuffling files to give a better idea of what the files concern, as well
as to add some structure.
2021-10-08 12:27:17 +02:00
Fabien O'Carroll
78be4b55c9 Moved ValueObject to domain/models/shared
no-issue

This is only ever used in this directory so it makes sense to be
colocated.
2021-10-08 12:23:40 +02:00
Fabien O'Carroll
4a27ef68df Moved events into domain directory
no-issue

Events are a domain concern and as such should live in this directory
2021-10-08 12:21:27 +02:00
Fabien O'Carroll
9f7a922415 Added support for "repeating" duration Offers
refs https://github.com/TryGhost/Team/issues/1083

We combine the duration and duration_in_months into a single value
object which can be validated together, meaning we will never have
properties which are out of sync (e.g. forever durations with 2 months).
2021-10-08 12:10:36 +02:00
Fabien O'Carroll
bd3f1e6456 Fixed sending back currency for Fixed Offers
no-issue

Since we changed the type from "amount" to "fixed" the logic to send
back the currency was not working. This updates it to use the correct
values.
2021-10-08 11:38:47 +02:00
Fabien O'Carroll
c8933c9abd Fixed handling of null currency & used duration
no-issue

Currency is not always present on an offer so we need to handle it.
Duration was incorrectly not passed to Stripe when creating the coupon.
2021-10-07 18:18:53 +02:00
Fabien O'Carroll
170591a113 Improved types for Offer
no-issue

This splits out the types of the Offers properties from the types of the
expected values to be passed to create.
2021-10-07 17:37:48 +02:00
Fabien O'Carroll
2b58ecd82e Added support for creating fixed offers with currency
refs https://github.com/TryGhost/Team/issues/1083

We now allow creating offers for a fixed amount, rather than a
percentage. These require a currency to be passed as a fixed amount is
meaningless without one.
2021-10-07 17:37:48 +02:00
Fabien O'Carroll
2c04afe810 Updated types of Offers to "fixed" and "percent"
no-issue

This is inline with the language used elsewhere to describe these types.
2021-10-07 17:35:34 +02:00
Fabien O'Carroll
6d383c2d0e Added support for "once" and "forever" Offer duration
refs https://github.com/TryGhost/Team/issues/1083

Instead of Offers being hardcoded to the "once" duration this will allow
Admins to start creating offers of variable durations.
2021-10-07 17:13:23 +02:00
Fabien O'Carroll
504fb1bfa1 Used Value Objects to validate outside of Offer factory
no-issue

This adds the concept of "Value Objects" to an Offers properties,
allowing us to move validation out and ensure that an Offer will only
ever have valid properties, without having to duplicate checks - or
leave them to the persistent layer. This means we can fail early, as
well as write unit tests for all of our validation.
2021-10-07 16:46:08 +02:00
Fabien O'Carroll
d9e40c6070 Ensured Offer code is slugified
refs https://github.com/TryGhost/Team/issues/1083

This ensures that the code will be URL safe for redirects
2021-10-07 12:45:51 +02:00
Fabien O'Carroll
6ae8b7eb0c Added stripeCouponId to Offer
refs https://github.com/TryGhost/Team/issues/1090

When creating a Stripe Checkout Session for an Offer - we need access to
the underlying Stripe Coupon. Exposing it here allows consumers of the
OfferRepository access.
2021-10-06 16:12:53 +02:00
Fabien O'Carroll
d57d082c39 Exposed OfferRepository from OffersModule
refs https://github.com/TryGhost/Team/issues/1090

When creating checkout session we will need to be able to look up Offers
from the OfferRepository. This exposes the repository so that it can be
passed as a dependency elsewhere.
2021-10-06 15:15:03 +02:00
Fabien O'Carroll
dbc7d73be7 Fixed updateOffer API method
no-issue

- Property names were out of sync with API
- Async methods were not awaited
- Transaction was erroneously commited
2021-10-06 12:07:03 +02:00
Fabien O'Carroll
33d042ccf7 Added getOffer method to OffersAPI
refs https://github.com/TryGhost/Team/issues/1083
2021-10-05 16:25:56 +02:00
Fabien O'Carroll
b3ed676e53 Moved redirect handling outside of repository
no-issue

The redirect handling is more of an application concern that can happen
at the top level, rather than the lower level of the repository.
2021-10-05 16:24:47 +02:00
Fabien O'Carroll
5dd5b71718 Fixed redirects for new offers
refs https://github.com/TryGhost/Team/issues/1091
2021-10-05 13:14:33 +02:00
Fabien O'Carroll
ef73121ae4 Fixed UniqueChecker methods
no-issue

These were returning the opposite of what they should have.
2021-10-05 11:43:38 +02:00
Fabien O'Carroll
5674036902 Added initial Offers module
refs https://github.com/TryGhost/Team/issues/1083

This is the initial scaffolding for setting up Offers in Ghost
2021-10-05 11:19:15 +02:00