Replies: 2 comments
-
you want to use Also in case you want to make something unique but still want to not provide this, use |
Beta Was this translation helpful? Give feedback.
-
Hi,
Regarding |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
dg-publish: true
Problem 1. Passing Product Id on Creation
Using afterOperation hook
My initial thought was to create the Stripe Product so I can retrieve the
id
of the newly created object through theitem
propertythe
stripeProductCreate()
is a wrapper function for Node Stripe API. Only thing to note isitem.id
is set to themetadata
Update Via Context
Ok... so instead I'll use a
context.db.Product.updateOne
right? Except that will create an infinite loop of the appI could just run a check and see if all data is the same? like price, name, status, url. But that seems very cumbersome.
I could use a combination of beforeOperation and afterOperation, creating the product and immediately updating only the metadata. But I don't like this idea of multiple round trips to solve a seemingly trivial problem.
Using beforeOperation hook
After running into the
afterOperation
brick wall, It seemed more logical to create a Stripe Product in thebeforeOperation
The code is very similar with one key exception
For linking the
stripeProductId
viaresolvedData
this works great... except now I can't pass the Product's Id tometadata
because the object hasn't been created yet. I know it doesn't make sense, but if there was a way to "pass Id to 3rd party integration in beforeOperation hook" that would theoretically solve all of this.Using Stripe Ids to search
No matter, I'll use Stripe's provided Id's to identify my database items. This leads right into my second problem "Finding Product during Webhook"
Custom Unique or Blank Validation
Brief pitstop here. I'll need custom validation to allow these Stripe Id fields to be unique OR be blank (
null
) to prevent any overlap between data.isIndexed 'Unique'
How about I set
stripeProductId
field toisIndexed: 'Unique'
? That way I canwhere: ProductWhereUniqueInput!
and use{ connect: stripeProductId }
or.findOne()
easily during search or creation of data.Except the
stripeProductId
is possibly a blank field (if the dev decides to forgo using Stripe). I realize I can make this app depend on Stripe and always set thestripeProductId
, but I will re-emphasize the introductory note aboveInstead of the built in
isIndexed: 'unique'
I'll create a custom validate hook inside the fieldProduct.ts
schema fieldsI'm calling this a "custom unique validation" that also allows the field to be empty if dev is omitting the use of Stripe. This is a necessary step to prevent end users from the foot gun of linking the 2 or more products to the same Stripe Product
Problem 2. Finding Product during Webhook
With correct validation set, only using stripe's Ids as keys to search the app's database still comes with a few undesired side effects.
Data Search Workaround
Ok so I can't use
where: ProductWhereUniqueInput!
, but I can still usedb.Product.findMany()
towhile the code works, the integrity still feels fragile.
Stripe Webhook
In this scenario, I've successfully completed a Stripe checkout session and Stripe returns back to my
webhook
with the response.I'm able to use this helper function to find and connect a product to this theoretical Order Item data object for my app's database. Using
await Promise.all()
Conclusion
As to refrain from rambling on, I'm left with a deep "There must be a better way" feeling to all of this.
I apologies If there is a simple solution I'm not seeing. Perhaps I've fiddled for too long in this code base and am reaching out to fresh eyes and experienced devs who have a better idea of how to handles this 1 to 1 setup between stand alone NextJS app and Stripe API integration.
Beta Was this translation helpful? Give feedback.
All reactions