diff --git a/packages/prisma-client/prisma/migrations/20260502000000_user_product_async_checkout/migration.sql b/packages/prisma-client/prisma/migrations/20260502000000_user_product_async_checkout/migration.sql new file mode 100644 index 000000000000..c41da708e44c --- /dev/null +++ b/packages/prisma-client/prisma/migrations/20260502000000_user_product_async_checkout/migration.sql @@ -0,0 +1,58 @@ +-- Treat checkout.session.completed as purchase-granting and revoke async +-- Checkout Sessions only if Stripe later reports async_payment_failed. +CREATE OR REPLACE VIEW "UserProduct" AS ( + SELECT + "userId", + "subscriptionId", + "productId", + "customerId", + "customerEmail" + FROM + "TransactionLog" AS tl + WHERE + "status" = 'complete' + AND "eventType" = 'checkout.session.completed' + AND NOT EXISTS ( + SELECT + 1 + FROM + "TransactionLog" AS failed + WHERE + failed."eventType" = 'checkout.session.async_payment_failed' + AND failed."eventData"#>>'{data,object,id}' = + tl."eventData"#>>'{data,object,id}' + AND failed."eventCreated" >= tl."eventCreated") + AND NOT EXISTS ( + SELECT + 1 + FROM + "TransactionLog" AS tlexsists + WHERE + tlexsists."subscriptionId" = tl."subscriptionId" + AND tlexsists."eventType" = 'customer.subscription.deleted' + AND tlexsists."status" = 'canceled' + AND tlexsists."eventCreated" > tl."eventCreated") + AND NOT EXISTS ( + SELECT + 1 + FROM + "TransactionLog" AS tlexsists + WHERE + tlexsists."paymentIntent" = tl."paymentIntent" + AND tlexsists."eventType" = 'charge.refunded' + AND tlexsists."status" = 'succeeded' + AND tlexsists."eventCreated" > tl."eventCreated") + ORDER BY + "userId", + "eventCreated" DESC) + UNION ALL ( + SELECT + "userId", + "subscriptionId", + "productId", + "customerId", + "customerEmail" + FROM + "TransactionLog" + WHERE + "eventType" = 'appsumo.activate');