@mmailaender/convex-creem 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +1176 -0
- package/dist/client/helpers.d.ts +17 -0
- package/dist/client/helpers.d.ts.map +1 -0
- package/dist/client/helpers.js +43 -0
- package/dist/client/helpers.js.map +1 -0
- package/dist/client/index.d.ts +1041 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +1068 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/parsers.d.ts +45 -0
- package/dist/client/parsers.d.ts.map +1 -0
- package/dist/client/parsers.js +138 -0
- package/dist/client/parsers.js.map +1 -0
- package/dist/client/polyfill.d.ts +2 -0
- package/dist/client/polyfill.d.ts.map +1 -0
- package/dist/client/polyfill.js +3 -0
- package/dist/client/polyfill.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +542 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +1005 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +647 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +191 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +104 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/util.d.ts +61 -0
- package/dist/component/util.d.ts.map +1 -0
- package/dist/component/util.js +142 -0
- package/dist/component/util.js.map +1 -0
- package/dist/core/catalog.d.ts +18 -0
- package/dist/core/catalog.d.ts.map +1 -0
- package/dist/core/catalog.js +82 -0
- package/dist/core/catalog.js.map +1 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +9 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/markdown.d.ts +12 -0
- package/dist/core/markdown.d.ts.map +1 -0
- package/dist/core/markdown.js +26 -0
- package/dist/core/markdown.js.map +1 -0
- package/dist/core/payments.d.ts +11 -0
- package/dist/core/payments.d.ts.map +1 -0
- package/dist/core/payments.js +27 -0
- package/dist/core/payments.js.map +1 -0
- package/dist/core/pendingCheckout.d.ts +15 -0
- package/dist/core/pendingCheckout.d.ts.map +1 -0
- package/dist/core/pendingCheckout.js +40 -0
- package/dist/core/pendingCheckout.js.map +1 -0
- package/dist/core/resolver.d.ts +11 -0
- package/dist/core/resolver.d.ts.map +1 -0
- package/dist/core/resolver.js +106 -0
- package/dist/core/resolver.js.map +1 -0
- package/dist/core/selectors.d.ts +12 -0
- package/dist/core/selectors.d.ts.map +1 -0
- package/dist/core/selectors.js +18 -0
- package/dist/core/selectors.js.map +1 -0
- package/dist/core/subscriptionUpdate.d.ts +20 -0
- package/dist/core/subscriptionUpdate.d.ts.map +1 -0
- package/dist/core/subscriptionUpdate.js +64 -0
- package/dist/core/subscriptionUpdate.js.map +1 -0
- package/dist/core/types.d.ts +170 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +15 -0
- package/dist/core/types.js.map +1 -0
- package/dist/design-system/colors/color-utils.d.ts +10 -0
- package/dist/design-system/colors/color-utils.d.ts.map +1 -0
- package/dist/design-system/colors/color-utils.js +91 -0
- package/dist/design-system/colors/color-utils.js.map +1 -0
- package/dist/design-system/colors/config.d.ts +33 -0
- package/dist/design-system/colors/config.d.ts.map +1 -0
- package/dist/design-system/colors/config.js +224 -0
- package/dist/design-system/colors/config.js.map +1 -0
- package/dist/design-system/colors/index.d.ts +3 -0
- package/dist/design-system/colors/index.d.ts.map +1 -0
- package/dist/design-system/colors/index.js +3 -0
- package/dist/design-system/colors/index.js.map +1 -0
- package/dist/design-system/rounded/config.d.ts +31 -0
- package/dist/design-system/rounded/config.d.ts.map +1 -0
- package/dist/design-system/rounded/config.js +76 -0
- package/dist/design-system/rounded/config.js.map +1 -0
- package/dist/design-system/rounded/index.d.ts +2 -0
- package/dist/design-system/rounded/index.d.ts.map +1 -0
- package/dist/design-system/rounded/index.js +2 -0
- package/dist/design-system/rounded/index.js.map +1 -0
- package/dist/design-system/typography/config.d.ts +55 -0
- package/dist/design-system/typography/config.d.ts.map +1 -0
- package/dist/design-system/typography/config.js +308 -0
- package/dist/design-system/typography/config.js.map +1 -0
- package/dist/design-system/typography/index.d.ts +3 -0
- package/dist/design-system/typography/index.d.ts.map +1 -0
- package/dist/design-system/typography/index.js +3 -0
- package/dist/design-system/typography/index.js.map +1 -0
- package/dist/design-system/typography/tokens.d.ts +23 -0
- package/dist/design-system/typography/tokens.d.ts.map +1 -0
- package/dist/design-system/typography/tokens.js +99 -0
- package/dist/design-system/typography/tokens.js.map +1 -0
- package/dist/react/hooks/useCheckoutSuccessParams.d.ts +2 -0
- package/dist/react/hooks/useCheckoutSuccessParams.d.ts.map +1 -0
- package/dist/react/hooks/useCheckoutSuccessParams.js +5 -0
- package/dist/react/hooks/useCheckoutSuccessParams.js.map +1 -0
- package/dist/react/index.d.ts +25 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +22 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/primitives/BillingGate.d.ts +8 -0
- package/dist/react/primitives/BillingGate.d.ts.map +1 -0
- package/dist/react/primitives/BillingGate.js +13 -0
- package/dist/react/primitives/BillingGate.js.map +1 -0
- package/dist/react/primitives/BillingToggle.d.ts +8 -0
- package/dist/react/primitives/BillingToggle.d.ts.map +1 -0
- package/dist/react/primitives/BillingToggle.js +12 -0
- package/dist/react/primitives/BillingToggle.js.map +1 -0
- package/dist/react/primitives/CheckoutButton.d.ts +11 -0
- package/dist/react/primitives/CheckoutButton.d.ts.map +1 -0
- package/dist/react/primitives/CheckoutButton.js +21 -0
- package/dist/react/primitives/CheckoutButton.js.map +1 -0
- package/dist/react/primitives/CheckoutSuccessSummary.d.ts +7 -0
- package/dist/react/primitives/CheckoutSuccessSummary.d.ts.map +1 -0
- package/dist/react/primitives/CheckoutSuccessSummary.js +11 -0
- package/dist/react/primitives/CheckoutSuccessSummary.js.map +1 -0
- package/dist/react/primitives/CustomerPortalButton.d.ts +8 -0
- package/dist/react/primitives/CustomerPortalButton.d.ts.map +1 -0
- package/dist/react/primitives/CustomerPortalButton.js +21 -0
- package/dist/react/primitives/CustomerPortalButton.js.map +1 -0
- package/dist/react/primitives/NumberInput.d.ts +11 -0
- package/dist/react/primitives/NumberInput.d.ts.map +1 -0
- package/dist/react/primitives/NumberInput.js +18 -0
- package/dist/react/primitives/NumberInput.js.map +1 -0
- package/dist/react/primitives/OneTimeCheckoutButton.d.ts +11 -0
- package/dist/react/primitives/OneTimeCheckoutButton.d.ts.map +1 -0
- package/dist/react/primitives/OneTimeCheckoutButton.js +4 -0
- package/dist/react/primitives/OneTimeCheckoutButton.js.map +1 -0
- package/dist/react/primitives/OneTimePaymentStatusBadge.d.ts +6 -0
- package/dist/react/primitives/OneTimePaymentStatusBadge.d.ts.map +1 -0
- package/dist/react/primitives/OneTimePaymentStatusBadge.js +11 -0
- package/dist/react/primitives/OneTimePaymentStatusBadge.js.map +1 -0
- package/dist/react/primitives/PaymentWarningBanner.d.ts +7 -0
- package/dist/react/primitives/PaymentWarningBanner.d.ts.map +1 -0
- package/dist/react/primitives/PaymentWarningBanner.js +18 -0
- package/dist/react/primitives/PaymentWarningBanner.js.map +1 -0
- package/dist/react/primitives/PricingCard.d.ts +37 -0
- package/dist/react/primitives/PricingCard.d.ts.map +1 -0
- package/dist/react/primitives/PricingCard.js +125 -0
- package/dist/react/primitives/PricingCard.js.map +1 -0
- package/dist/react/primitives/PricingSection.d.ts +39 -0
- package/dist/react/primitives/PricingSection.d.ts.map +1 -0
- package/dist/react/primitives/PricingSection.js +24 -0
- package/dist/react/primitives/PricingSection.js.map +1 -0
- package/dist/react/primitives/ScheduledChangeBanner.d.ts +8 -0
- package/dist/react/primitives/ScheduledChangeBanner.d.ts.map +1 -0
- package/dist/react/primitives/ScheduledChangeBanner.js +13 -0
- package/dist/react/primitives/ScheduledChangeBanner.js.map +1 -0
- package/dist/react/primitives/SegmentControl.d.ts +11 -0
- package/dist/react/primitives/SegmentControl.d.ts.map +1 -0
- package/dist/react/primitives/SegmentControl.js +8 -0
- package/dist/react/primitives/SegmentControl.js.map +1 -0
- package/dist/react/primitives/SegmentGroup.d.ts +14 -0
- package/dist/react/primitives/SegmentGroup.d.ts.map +1 -0
- package/dist/react/primitives/SegmentGroup.js +11 -0
- package/dist/react/primitives/SegmentGroup.js.map +1 -0
- package/dist/react/primitives/TrialLimitBanner.d.ts +7 -0
- package/dist/react/primitives/TrialLimitBanner.d.ts.map +1 -0
- package/dist/react/primitives/TrialLimitBanner.js +14 -0
- package/dist/react/primitives/TrialLimitBanner.js.map +1 -0
- package/dist/react/shared.d.ts +28 -0
- package/dist/react/shared.d.ts.map +1 -0
- package/dist/react/shared.js +109 -0
- package/dist/react/shared.js.map +1 -0
- package/dist/react/widgets/BillingPortal.d.ts +9 -0
- package/dist/react/widgets/BillingPortal.d.ts.map +1 -0
- package/dist/react/widgets/BillingPortal.js +30 -0
- package/dist/react/widgets/BillingPortal.js.map +1 -0
- package/dist/react/widgets/ProductItem.d.ts +8 -0
- package/dist/react/widgets/ProductItem.d.ts.map +1 -0
- package/dist/react/widgets/ProductItem.js +14 -0
- package/dist/react/widgets/ProductItem.js.map +1 -0
- package/dist/react/widgets/ProductRoot.d.ts +16 -0
- package/dist/react/widgets/ProductRoot.d.ts.map +1 -0
- package/dist/react/widgets/ProductRoot.js +171 -0
- package/dist/react/widgets/ProductRoot.js.map +1 -0
- package/dist/react/widgets/SubscriptionItem.d.ts +27 -0
- package/dist/react/widgets/SubscriptionItem.d.ts.map +1 -0
- package/dist/react/widgets/SubscriptionItem.js +32 -0
- package/dist/react/widgets/SubscriptionItem.js.map +1 -0
- package/dist/react/widgets/SubscriptionRoot.d.ts +16 -0
- package/dist/react/widgets/SubscriptionRoot.d.ts.map +1 -0
- package/dist/react/widgets/SubscriptionRoot.js +405 -0
- package/dist/react/widgets/SubscriptionRoot.js.map +1 -0
- package/dist/react/widgets/index.d.ts +19 -0
- package/dist/react/widgets/index.d.ts.map +1 -0
- package/dist/react/widgets/index.js +16 -0
- package/dist/react/widgets/index.js.map +1 -0
- package/dist/react/widgets/productGroupContext.d.ts +6 -0
- package/dist/react/widgets/productGroupContext.d.ts.map +1 -0
- package/dist/react/widgets/productGroupContext.js +3 -0
- package/dist/react/widgets/productGroupContext.js.map +1 -0
- package/dist/react/widgets/subscriptionContext.d.ts +6 -0
- package/dist/react/widgets/subscriptionContext.d.ts.map +1 -0
- package/dist/react/widgets/subscriptionContext.js +3 -0
- package/dist/react/widgets/subscriptionContext.js.map +1 -0
- package/dist/react/widgets/types.d.ts +171 -0
- package/dist/react/widgets/types.d.ts.map +1 -0
- package/dist/react/widgets/types.js +2 -0
- package/dist/react/widgets/types.js.map +1 -0
- package/dist/svelte/index.d.ts +22 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +20 -0
- package/dist/svelte/index.js.map +1 -0
- package/dist/svelte/primitives/BillingGate.svelte +28 -0
- package/dist/svelte/primitives/BillingToggle.svelte +27 -0
- package/dist/svelte/primitives/CheckoutButton.svelte +60 -0
- package/dist/svelte/primitives/CheckoutSuccessSummary.svelte +34 -0
- package/dist/svelte/primitives/CustomerPortalButton.svelte +60 -0
- package/dist/svelte/primitives/NumberInput.svelte +71 -0
- package/dist/svelte/primitives/OneTimeCheckoutButton.svelte +37 -0
- package/dist/svelte/primitives/OneTimePaymentStatusBadge.svelte +20 -0
- package/dist/svelte/primitives/PaymentWarningBanner.svelte +30 -0
- package/dist/svelte/primitives/PricingCard.svelte +356 -0
- package/dist/svelte/primitives/PricingSection.svelte +121 -0
- package/dist/svelte/primitives/ScheduledChangeBanner.svelte +46 -0
- package/dist/svelte/primitives/SegmentControl.svelte +38 -0
- package/dist/svelte/primitives/SegmentGroup.svelte +52 -0
- package/dist/svelte/primitives/TrialLimitBanner.svelte +32 -0
- package/dist/svelte/primitives/shared.d.ts +13 -0
- package/dist/svelte/primitives/shared.d.ts.map +1 -0
- package/dist/svelte/primitives/shared.js +87 -0
- package/dist/svelte/primitives/shared.js.map +1 -0
- package/dist/svelte/widgets/BillingPortal.svelte +55 -0
- package/dist/svelte/widgets/Product.svelte +35 -0
- package/dist/svelte/widgets/ProductRoot.svelte +428 -0
- package/dist/svelte/widgets/Subscription.svelte +52 -0
- package/dist/svelte/widgets/SubscriptionRoot.svelte +690 -0
- package/dist/svelte/widgets/index.d.ts +19 -0
- package/dist/svelte/widgets/index.d.ts.map +1 -0
- package/dist/svelte/widgets/index.js +16 -0
- package/dist/svelte/widgets/index.js.map +1 -0
- package/dist/svelte/widgets/productGroupContext.d.ts +6 -0
- package/dist/svelte/widgets/productGroupContext.d.ts.map +1 -0
- package/dist/svelte/widgets/productGroupContext.js +2 -0
- package/dist/svelte/widgets/productGroupContext.js.map +1 -0
- package/dist/svelte/widgets/subscriptionContext.d.ts +6 -0
- package/dist/svelte/widgets/subscriptionContext.d.ts.map +1 -0
- package/dist/svelte/widgets/subscriptionContext.js +2 -0
- package/dist/svelte/widgets/subscriptionContext.js.map +1 -0
- package/dist/svelte/widgets/types.d.ts +171 -0
- package/dist/svelte/widgets/types.d.ts.map +1 -0
- package/dist/svelte/widgets/types.js +2 -0
- package/dist/svelte/widgets/types.js.map +1 -0
- package/package.json +182 -0
- package/src/client/helpers.test.ts +139 -0
- package/src/client/helpers.ts +51 -0
- package/src/client/index.test.ts +1554 -0
- package/src/client/index.ts +1504 -0
- package/src/client/parsers.test.ts +1017 -0
- package/src/client/parsers.ts +182 -0
- package/src/client/polyfill.ts +2 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +619 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/lib.test.ts +1359 -0
- package/src/component/lib.ts +726 -0
- package/src/component/schema.ts +112 -0
- package/src/component/util.test.ts +281 -0
- package/src/component/util.ts +228 -0
- package/src/core/catalog.test.ts +212 -0
- package/src/core/catalog.ts +119 -0
- package/src/core/index.ts +8 -0
- package/src/core/markdown.test.ts +43 -0
- package/src/core/markdown.ts +26 -0
- package/src/core/payments.test.ts +69 -0
- package/src/core/payments.ts +33 -0
- package/src/core/pendingCheckout.test.ts +44 -0
- package/src/core/pendingCheckout.ts +40 -0
- package/src/core/resolver.test.ts +283 -0
- package/src/core/resolver.ts +160 -0
- package/src/core/selectors.test.ts +119 -0
- package/src/core/selectors.ts +35 -0
- package/src/core/subscriptionUpdate.test.ts +164 -0
- package/src/core/subscriptionUpdate.ts +102 -0
- package/src/core/types.ts +220 -0
- package/src/design-system/README.md +40 -0
- package/src/design-system/base.css +27 -0
- package/src/design-system/colors/color-utils.ts +110 -0
- package/src/design-system/colors/config.ts +282 -0
- package/src/design-system/colors/index.ts +2 -0
- package/src/design-system/colors/utilities.css +2328 -0
- package/src/design-system/components/badges.css +65 -0
- package/src/design-system/components/buttons.css +256 -0
- package/src/design-system/components/dialog.css +218 -0
- package/src/design-system/components/icon-buttons.css +115 -0
- package/src/design-system/components/inputs.css +94 -0
- package/src/design-system/components/links.css +53 -0
- package/src/design-system/components/prose.css +67 -0
- package/src/design-system/components/segment-control.css +303 -0
- package/src/design-system/index.css +21 -0
- package/src/design-system/rounded/config.ts +91 -0
- package/src/design-system/rounded/index.ts +1 -0
- package/src/design-system/rounded/utilities.css +37 -0
- package/src/design-system/typography/config.ts +340 -0
- package/src/design-system/typography/index.ts +2 -0
- package/src/design-system/typography/tokens.ts +148 -0
- package/src/design-system/typography/utilities.css +728 -0
- package/src/library.css +20 -0
- package/src/react/hooks/useCheckoutSuccessParams.ts +7 -0
- package/src/react/index.tsx +47 -0
- package/src/react/primitives/BillingGate.tsx +26 -0
- package/src/react/primitives/BillingToggle.tsx +29 -0
- package/src/react/primitives/CheckoutButton.tsx +47 -0
- package/src/react/primitives/CheckoutSuccessSummary.tsx +36 -0
- package/src/react/primitives/CustomerPortalButton.tsx +50 -0
- package/src/react/primitives/NumberInput.tsx +83 -0
- package/src/react/primitives/OneTimeCheckoutButton.tsx +27 -0
- package/src/react/primitives/OneTimePaymentStatusBadge.tsx +18 -0
- package/src/react/primitives/PaymentWarningBanner.tsx +33 -0
- package/src/react/primitives/PricingCard.tsx +421 -0
- package/src/react/primitives/PricingSection.tsx +129 -0
- package/src/react/primitives/ScheduledChangeBanner.tsx +52 -0
- package/src/react/primitives/SegmentControl.tsx +32 -0
- package/src/react/primitives/SegmentGroup.tsx +53 -0
- package/src/react/primitives/TrialLimitBanner.tsx +32 -0
- package/src/react/shared.ts +138 -0
- package/src/react/widgets/BillingPortal.tsx +56 -0
- package/src/react/widgets/ProductItem.tsx +26 -0
- package/src/react/widgets/ProductRoot.tsx +441 -0
- package/src/react/widgets/SubscriptionItem.tsx +71 -0
- package/src/react/widgets/SubscriptionRoot.tsx +759 -0
- package/src/react/widgets/index.ts +36 -0
- package/src/react/widgets/productGroupContext.ts +10 -0
- package/src/react/widgets/subscriptionContext.ts +10 -0
- package/src/react/widgets/types.ts +179 -0
- package/src/svelte/index.ts +43 -0
- package/src/svelte/primitives/BillingGate.svelte +28 -0
- package/src/svelte/primitives/BillingToggle.svelte +27 -0
- package/src/svelte/primitives/CheckoutButton.svelte +60 -0
- package/src/svelte/primitives/CheckoutSuccessSummary.svelte +34 -0
- package/src/svelte/primitives/CustomerPortalButton.svelte +60 -0
- package/src/svelte/primitives/NumberInput.svelte +71 -0
- package/src/svelte/primitives/OneTimeCheckoutButton.svelte +37 -0
- package/src/svelte/primitives/OneTimePaymentStatusBadge.svelte +20 -0
- package/src/svelte/primitives/PaymentWarningBanner.svelte +30 -0
- package/src/svelte/primitives/PricingCard.svelte +356 -0
- package/src/svelte/primitives/PricingSection.svelte +121 -0
- package/src/svelte/primitives/ScheduledChangeBanner.svelte +46 -0
- package/src/svelte/primitives/SegmentControl.svelte +38 -0
- package/src/svelte/primitives/SegmentGroup.svelte +52 -0
- package/src/svelte/primitives/TrialLimitBanner.svelte +32 -0
- package/src/svelte/primitives/shared.ts +113 -0
- package/src/svelte/svelte.d.ts +6 -0
- package/src/svelte/widgets/BillingPortal.svelte +55 -0
- package/src/svelte/widgets/Product.svelte +35 -0
- package/src/svelte/widgets/ProductRoot.svelte +428 -0
- package/src/svelte/widgets/Subscription.svelte +52 -0
- package/src/svelte/widgets/SubscriptionRoot.svelte +690 -0
- package/src/svelte/widgets/index.ts +36 -0
- package/src/svelte/widgets/productGroupContext.ts +7 -0
- package/src/svelte/widgets/subscriptionContext.ts +7 -0
- package/src/svelte/widgets/types.ts +179 -0
- package/src/tailwind.css +6 -0
- package/src/test.ts +18 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
const vNullableString = v.union(v.string(), v.null());
|
|
5
|
+
|
|
6
|
+
export default defineSchema(
|
|
7
|
+
{
|
|
8
|
+
customers: defineTable({
|
|
9
|
+
id: v.string(),
|
|
10
|
+
entityId: v.string(),
|
|
11
|
+
email: v.optional(v.string()),
|
|
12
|
+
name: v.optional(vNullableString),
|
|
13
|
+
country: v.optional(v.string()),
|
|
14
|
+
mode: v.optional(v.string()),
|
|
15
|
+
metadata: v.optional(v.record(v.string(), v.any())),
|
|
16
|
+
createdAt: v.optional(v.string()),
|
|
17
|
+
updatedAt: v.optional(v.string()),
|
|
18
|
+
})
|
|
19
|
+
.index("entityId", ["entityId"])
|
|
20
|
+
.index("id", ["id"]),
|
|
21
|
+
products: defineTable({
|
|
22
|
+
id: v.string(),
|
|
23
|
+
name: v.string(),
|
|
24
|
+
description: v.union(v.string(), v.null()),
|
|
25
|
+
price: v.number(),
|
|
26
|
+
currency: v.string(),
|
|
27
|
+
billingType: v.string(),
|
|
28
|
+
billingPeriod: v.optional(v.string()),
|
|
29
|
+
status: v.string(),
|
|
30
|
+
taxMode: v.optional(v.string()),
|
|
31
|
+
taxCategory: v.optional(v.string()),
|
|
32
|
+
imageUrl: v.optional(v.string()),
|
|
33
|
+
productUrl: v.optional(v.string()),
|
|
34
|
+
defaultSuccessUrl: v.optional(vNullableString),
|
|
35
|
+
mode: v.optional(v.string()),
|
|
36
|
+
features: v.optional(
|
|
37
|
+
v.array(
|
|
38
|
+
v.object({
|
|
39
|
+
id: v.string(),
|
|
40
|
+
description: v.string(),
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
),
|
|
44
|
+
metadata: v.optional(v.record(v.string(), v.any())),
|
|
45
|
+
createdAt: v.string(),
|
|
46
|
+
modifiedAt: v.union(v.string(), v.null()),
|
|
47
|
+
})
|
|
48
|
+
.index("id", ["id"])
|
|
49
|
+
.index("status", ["status"]),
|
|
50
|
+
subscriptions: defineTable({
|
|
51
|
+
id: v.string(),
|
|
52
|
+
customerId: v.string(),
|
|
53
|
+
productId: v.string(),
|
|
54
|
+
status: v.string(),
|
|
55
|
+
amount: v.union(v.number(), v.null()),
|
|
56
|
+
currency: v.union(v.string(), v.null()),
|
|
57
|
+
recurringInterval: vNullableString,
|
|
58
|
+
currentPeriodStart: v.string(),
|
|
59
|
+
currentPeriodEnd: v.union(v.string(), v.null()),
|
|
60
|
+
cancelAtPeriodEnd: v.boolean(),
|
|
61
|
+
startedAt: v.union(v.string(), v.null()),
|
|
62
|
+
endedAt: v.union(v.string(), v.null()),
|
|
63
|
+
priceId: v.optional(v.string()),
|
|
64
|
+
checkoutId: v.union(v.string(), v.null()),
|
|
65
|
+
metadata: v.record(v.string(), v.any()),
|
|
66
|
+
collectionMethod: v.optional(v.string()),
|
|
67
|
+
discountId: v.optional(v.union(v.string(), v.null())),
|
|
68
|
+
canceledAt: v.optional(v.union(v.string(), v.null())),
|
|
69
|
+
endsAt: v.optional(v.union(v.string(), v.null())),
|
|
70
|
+
trialStart: v.optional(v.union(v.string(), v.null())),
|
|
71
|
+
trialEnd: v.optional(v.union(v.string(), v.null())),
|
|
72
|
+
seats: v.optional(v.union(v.number(), v.null())),
|
|
73
|
+
lastTransactionId: v.optional(v.union(v.string(), v.null())),
|
|
74
|
+
nextTransactionDate: v.optional(v.union(v.string(), v.null())),
|
|
75
|
+
mode: v.optional(v.string()),
|
|
76
|
+
createdAt: v.string(),
|
|
77
|
+
modifiedAt: v.union(v.string(), v.null()),
|
|
78
|
+
})
|
|
79
|
+
.index("id", ["id"])
|
|
80
|
+
.index("customerId", ["customerId"])
|
|
81
|
+
.index("customerId_status", ["customerId", "status"])
|
|
82
|
+
.index("customerId_endedAt", ["customerId", "endedAt"]),
|
|
83
|
+
orders: defineTable({
|
|
84
|
+
id: v.string(),
|
|
85
|
+
customerId: v.string(),
|
|
86
|
+
productId: v.string(),
|
|
87
|
+
amount: v.number(),
|
|
88
|
+
currency: v.string(),
|
|
89
|
+
status: v.string(),
|
|
90
|
+
type: v.string(),
|
|
91
|
+
subTotal: v.optional(v.number()),
|
|
92
|
+
taxAmount: v.optional(v.number()),
|
|
93
|
+
discountAmount: v.optional(v.number()),
|
|
94
|
+
amountDue: v.optional(v.number()),
|
|
95
|
+
amountPaid: v.optional(v.number()),
|
|
96
|
+
transactionId: v.optional(v.union(v.string(), v.null())),
|
|
97
|
+
checkoutId: v.optional(v.union(v.string(), v.null())),
|
|
98
|
+
discountId: v.optional(v.union(v.string(), v.null())),
|
|
99
|
+
affiliate: v.optional(v.union(v.string(), v.null())),
|
|
100
|
+
mode: v.optional(v.string()),
|
|
101
|
+
metadata: v.optional(v.record(v.string(), v.any())),
|
|
102
|
+
createdAt: v.string(),
|
|
103
|
+
updatedAt: v.string(),
|
|
104
|
+
})
|
|
105
|
+
.index("id", ["id"])
|
|
106
|
+
.index("customerId", ["customerId"])
|
|
107
|
+
.index("customerId_productId", ["customerId", "productId"]),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
schemaValidation: true,
|
|
111
|
+
},
|
|
112
|
+
);
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
convertToDatabaseSubscription,
|
|
4
|
+
convertToOrder,
|
|
5
|
+
convertToDatabaseProduct,
|
|
6
|
+
} from "./util.js";
|
|
7
|
+
|
|
8
|
+
describe("convertToDatabaseSubscription", () => {
|
|
9
|
+
const baseSubscription = {
|
|
10
|
+
id: "sub_1",
|
|
11
|
+
customer: "cus_1",
|
|
12
|
+
product: "prod_1",
|
|
13
|
+
status: "active" as const,
|
|
14
|
+
items: [{ productId: "prod_1", priceId: "price_1", units: 1 }],
|
|
15
|
+
currentPeriodStartDate: "2026-01-01T00:00:00.000Z",
|
|
16
|
+
currentPeriodEndDate: "2026-02-01T00:00:00.000Z",
|
|
17
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
18
|
+
updatedAt: "2026-01-15T00:00:00.000Z",
|
|
19
|
+
canceledAt: null,
|
|
20
|
+
lastTransactionId: "txn_1",
|
|
21
|
+
nextTransactionDate: "2026-02-01T00:00:00.000Z",
|
|
22
|
+
mode: "live",
|
|
23
|
+
discount: null,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
it("converts an active subscription", () => {
|
|
27
|
+
const result = convertToDatabaseSubscription(baseSubscription as any);
|
|
28
|
+
expect(result.id).toBe("sub_1");
|
|
29
|
+
expect(result.customerId).toBe("cus_1");
|
|
30
|
+
expect(result.productId).toBe("prod_1");
|
|
31
|
+
expect(result.status).toBe("active");
|
|
32
|
+
expect(result.cancelAtPeriodEnd).toBe(false);
|
|
33
|
+
expect(result.endedAt).toBeNull();
|
|
34
|
+
expect(result.trialStart).toBeNull();
|
|
35
|
+
expect(result.trialEnd).toBeNull();
|
|
36
|
+
expect(result.seats).toBe(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("converts a trialing subscription with trial dates", () => {
|
|
40
|
+
const sub = {
|
|
41
|
+
...baseSubscription,
|
|
42
|
+
status: "trialing" as const,
|
|
43
|
+
};
|
|
44
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
45
|
+
expect(result.trialStart).toBe("2026-01-01T00:00:00.000Z");
|
|
46
|
+
expect(result.trialEnd).toBe("2026-02-01T00:00:00.000Z");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("converts a scheduled_cancel subscription with cancelAtPeriodEnd", () => {
|
|
50
|
+
const sub = {
|
|
51
|
+
...baseSubscription,
|
|
52
|
+
status: "scheduled_cancel" as const,
|
|
53
|
+
canceledAt: "2026-01-20T00:00:00.000Z",
|
|
54
|
+
};
|
|
55
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
56
|
+
expect(result.cancelAtPeriodEnd).toBe(true);
|
|
57
|
+
expect(result.endsAt).toBe("2026-02-01T00:00:00.000Z");
|
|
58
|
+
expect(result.canceledAt).toBe("2026-01-20T00:00:00.000Z");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("converts a canceled subscription with endedAt", () => {
|
|
62
|
+
const sub = {
|
|
63
|
+
...baseSubscription,
|
|
64
|
+
status: "canceled" as const,
|
|
65
|
+
canceledAt: "2026-01-20T00:00:00.000Z",
|
|
66
|
+
};
|
|
67
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
68
|
+
expect(result.cancelAtPeriodEnd).toBe(false);
|
|
69
|
+
expect(result.endedAt).toBe("2026-01-20T00:00:00.000Z");
|
|
70
|
+
expect(result.endsAt).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("throws ConvexError when customer id is missing", () => {
|
|
74
|
+
const sub = { ...baseSubscription, customer: null };
|
|
75
|
+
expect(() => convertToDatabaseSubscription(sub as any)).toThrow(
|
|
76
|
+
"Creem subscription is missing customer id",
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("throws ConvexError when product id is missing", () => {
|
|
81
|
+
const sub = { ...baseSubscription, product: null, items: [] };
|
|
82
|
+
expect(() => convertToDatabaseSubscription(sub as any)).toThrow(
|
|
83
|
+
"Creem subscription is missing product id",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("extracts product id from items when product entity id is null", () => {
|
|
88
|
+
const sub = {
|
|
89
|
+
...baseSubscription,
|
|
90
|
+
product: null as any,
|
|
91
|
+
customer: "cus_1",
|
|
92
|
+
items: [{ productId: "prod_from_items", priceId: "p1", units: 2 }],
|
|
93
|
+
};
|
|
94
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
95
|
+
expect(result.productId).toBe("prod_from_items");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("passes rawMetadata option through", () => {
|
|
99
|
+
const meta = { convexUserId: "user_123" };
|
|
100
|
+
const result = convertToDatabaseSubscription(baseSubscription as any, {
|
|
101
|
+
rawMetadata: meta,
|
|
102
|
+
});
|
|
103
|
+
expect(result.metadata).toEqual(meta);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("handles customer as object with id", () => {
|
|
107
|
+
const sub = {
|
|
108
|
+
...baseSubscription,
|
|
109
|
+
customer: { id: "cus_obj" },
|
|
110
|
+
};
|
|
111
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
112
|
+
expect(result.customerId).toBe("cus_obj");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("handles product as object with id and extracts price/currency", () => {
|
|
116
|
+
const sub = {
|
|
117
|
+
...baseSubscription,
|
|
118
|
+
product: {
|
|
119
|
+
id: "prod_obj",
|
|
120
|
+
price: 1999,
|
|
121
|
+
currency: "USD",
|
|
122
|
+
billingPeriod: "every-month",
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
126
|
+
expect(result.productId).toBe("prod_obj");
|
|
127
|
+
expect(result.amount).toBe(1999);
|
|
128
|
+
expect(result.currency).toBe("USD");
|
|
129
|
+
expect(result.recurringInterval).toBe("every-month");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("uses discount id when discount is an object", () => {
|
|
133
|
+
const sub = {
|
|
134
|
+
...baseSubscription,
|
|
135
|
+
discount: { id: "disc_1" },
|
|
136
|
+
};
|
|
137
|
+
const result = convertToDatabaseSubscription(sub as any);
|
|
138
|
+
expect(result.discountId).toBe("disc_1");
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("convertToOrder", () => {
|
|
143
|
+
const baseOrder = {
|
|
144
|
+
id: "ord_1",
|
|
145
|
+
customer: "cus_1",
|
|
146
|
+
product: "prod_1",
|
|
147
|
+
amount: 2999,
|
|
148
|
+
currency: "USD",
|
|
149
|
+
status: "completed",
|
|
150
|
+
type: "one_time",
|
|
151
|
+
transaction: "txn_1",
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
it("converts a basic order with camelCase fields", () => {
|
|
155
|
+
const order = {
|
|
156
|
+
...baseOrder,
|
|
157
|
+
subTotal: 2500,
|
|
158
|
+
taxAmount: 499,
|
|
159
|
+
discountAmount: 0,
|
|
160
|
+
amountDue: 2999,
|
|
161
|
+
amountPaid: 2999,
|
|
162
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
163
|
+
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
164
|
+
};
|
|
165
|
+
const result = convertToOrder(order);
|
|
166
|
+
expect(result.id).toBe("ord_1");
|
|
167
|
+
expect(result.customerId).toBe("cus_1");
|
|
168
|
+
expect(result.productId).toBe("prod_1");
|
|
169
|
+
expect(result.subTotal).toBe(2500);
|
|
170
|
+
expect(result.taxAmount).toBe(499);
|
|
171
|
+
expect(result.transactionId).toBe("txn_1");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("falls back to snake_case fields when camelCase is undefined", () => {
|
|
175
|
+
const order = {
|
|
176
|
+
...baseOrder,
|
|
177
|
+
sub_total: 2500,
|
|
178
|
+
tax_amount: 499,
|
|
179
|
+
discount_amount: 0,
|
|
180
|
+
amount_due: 2999,
|
|
181
|
+
amount_paid: 2999,
|
|
182
|
+
created_at: "2026-01-01T00:00:00.000Z",
|
|
183
|
+
updated_at: "2026-01-01T00:00:00.000Z",
|
|
184
|
+
};
|
|
185
|
+
const result = convertToOrder(order);
|
|
186
|
+
expect(result.subTotal).toBe(2500);
|
|
187
|
+
expect(result.taxAmount).toBe(499);
|
|
188
|
+
expect(result.createdAt).toBe("2026-01-01T00:00:00.000Z");
|
|
189
|
+
expect(result.updatedAt).toBe("2026-01-01T00:00:00.000Z");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("includes checkoutId and metadata from options", () => {
|
|
193
|
+
const result = convertToOrder(baseOrder, {
|
|
194
|
+
checkoutId: "chk_1",
|
|
195
|
+
metadata: { source: "test" },
|
|
196
|
+
});
|
|
197
|
+
expect(result.checkoutId).toBe("chk_1");
|
|
198
|
+
expect(result.metadata).toEqual({ source: "test" });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("defaults customerId to empty string when customer is null", () => {
|
|
202
|
+
const order = { ...baseOrder, customer: null };
|
|
203
|
+
const result = convertToOrder(order);
|
|
204
|
+
expect(result.customerId).toBe("");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("handles optional fields as null/undefined", () => {
|
|
208
|
+
const result = convertToOrder(baseOrder);
|
|
209
|
+
expect(result.discountId).toBeNull();
|
|
210
|
+
expect(result.affiliate).toBeNull();
|
|
211
|
+
expect(result.checkoutId).toBeNull();
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe("convertToDatabaseProduct", () => {
|
|
216
|
+
const baseProduct = {
|
|
217
|
+
id: "prod_1",
|
|
218
|
+
name: "Pro Plan",
|
|
219
|
+
description: "Full access",
|
|
220
|
+
price: 1999,
|
|
221
|
+
currency: "USD",
|
|
222
|
+
billingType: "recurring",
|
|
223
|
+
billingPeriod: "every-month",
|
|
224
|
+
status: "active",
|
|
225
|
+
taxMode: "inclusive",
|
|
226
|
+
taxCategory: "saas",
|
|
227
|
+
defaultSuccessUrl: "https://example.com/success",
|
|
228
|
+
mode: "live",
|
|
229
|
+
features: [
|
|
230
|
+
{ id: "feat_1", description: "Unlimited access" },
|
|
231
|
+
{ id: "feat_2", description: "Priority support" },
|
|
232
|
+
],
|
|
233
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
234
|
+
updatedAt: "2026-01-15T00:00:00.000Z",
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
it("converts a full product entity", () => {
|
|
238
|
+
const result = convertToDatabaseProduct(baseProduct as any);
|
|
239
|
+
expect(result.id).toBe("prod_1");
|
|
240
|
+
expect(result.name).toBe("Pro Plan");
|
|
241
|
+
expect(result.price).toBe(1999);
|
|
242
|
+
expect(result.currency).toBe("USD");
|
|
243
|
+
expect(result.billingType).toBe("recurring");
|
|
244
|
+
expect(result.billingPeriod).toBe("every-month");
|
|
245
|
+
expect(result.status).toBe("active");
|
|
246
|
+
expect(result.features).toHaveLength(2);
|
|
247
|
+
expect(result.features?.[0]?.description).toBe("Unlimited access");
|
|
248
|
+
expect(result.createdAt).toBe("2026-01-01T00:00:00.000Z");
|
|
249
|
+
expect(result.modifiedAt).toBe("2026-01-15T00:00:00.000Z");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("handles missing optional fields", () => {
|
|
253
|
+
const product = {
|
|
254
|
+
id: "prod_2",
|
|
255
|
+
name: "Basic",
|
|
256
|
+
price: 0,
|
|
257
|
+
currency: "EUR",
|
|
258
|
+
billingType: "onetime",
|
|
259
|
+
status: "active",
|
|
260
|
+
mode: "test",
|
|
261
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
262
|
+
};
|
|
263
|
+
const result = convertToDatabaseProduct(product as any);
|
|
264
|
+
expect(result.id).toBe("prod_2");
|
|
265
|
+
expect(result.description).toBeUndefined();
|
|
266
|
+
expect(result.billingPeriod).toBeUndefined();
|
|
267
|
+
expect(result.features).toBeUndefined();
|
|
268
|
+
expect(result.modifiedAt).toBeNull();
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("extracts imageUrl and productUrl via cast", () => {
|
|
272
|
+
const product = {
|
|
273
|
+
...baseProduct,
|
|
274
|
+
imageUrl: "https://img.example.com/pro.png",
|
|
275
|
+
productUrl: "https://example.com/pro",
|
|
276
|
+
};
|
|
277
|
+
const result = convertToDatabaseProduct(product as any);
|
|
278
|
+
expect(result.imageUrl).toBe("https://img.example.com/pro.png");
|
|
279
|
+
expect(result.productUrl).toBe("https://example.com/pro");
|
|
280
|
+
});
|
|
281
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GenericMutationCtx,
|
|
3
|
+
GenericActionCtx,
|
|
4
|
+
GenericQueryCtx,
|
|
5
|
+
GenericDataModel,
|
|
6
|
+
} from "convex/server";
|
|
7
|
+
import type {
|
|
8
|
+
ProductEntity as CreemProduct,
|
|
9
|
+
SubscriptionEntity as CreemSubscription,
|
|
10
|
+
} from "creem/models/components";
|
|
11
|
+
import { ConvexError, type Infer } from "convex/values";
|
|
12
|
+
import type schema from "./schema.js";
|
|
13
|
+
|
|
14
|
+
/** Minimal context type for Convex functions that only need `runQuery`. */
|
|
15
|
+
export type RunQueryCtx = {
|
|
16
|
+
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
|
|
17
|
+
};
|
|
18
|
+
/** Minimal context type for Convex functions that need `runQuery` + `runMutation`. */
|
|
19
|
+
export type RunMutationCtx = {
|
|
20
|
+
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
|
|
21
|
+
runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
|
|
22
|
+
};
|
|
23
|
+
/** Mutation context that also includes `scheduler` for scheduling async follow-up actions. */
|
|
24
|
+
export type RunSchedulerMutationCtx = RunMutationCtx & {
|
|
25
|
+
scheduler: GenericMutationCtx<GenericDataModel>["scheduler"];
|
|
26
|
+
};
|
|
27
|
+
/** Minimal context type for Convex actions that need `runQuery` + `runMutation` + `runAction`. */
|
|
28
|
+
export type RunActionCtx = {
|
|
29
|
+
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
|
|
30
|
+
runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
|
|
31
|
+
runAction: GenericActionCtx<GenericDataModel>["runAction"];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const toIsoString = (value: unknown): string | null => {
|
|
35
|
+
if (!value) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (value instanceof Date) {
|
|
39
|
+
return value.toISOString();
|
|
40
|
+
}
|
|
41
|
+
if (typeof value === "string") {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const toIsoStringOrNow = (value: unknown): string => {
|
|
48
|
+
return toIsoString(value) ?? new Date().toISOString();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const entityId = (value: unknown): string | null => {
|
|
52
|
+
if (typeof value === "string") {
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
if (value && typeof value === "object" && "id" in value) {
|
|
56
|
+
const id = (value as { id?: unknown }).id;
|
|
57
|
+
if (typeof id === "string") {
|
|
58
|
+
return id;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Convert a Creem SDK `SubscriptionEntity` to the Convex DB subscription schema shape. Handles snake_case/camelCase, metadata recovery, and trial/cancel state. */
|
|
65
|
+
export const convertToDatabaseSubscription = (
|
|
66
|
+
subscription: CreemSubscription,
|
|
67
|
+
options?: { rawMetadata?: Record<string, unknown> },
|
|
68
|
+
): Infer<typeof schema.tables.subscriptions.validator> => {
|
|
69
|
+
const customerId = entityId(subscription.customer);
|
|
70
|
+
if (!customerId) {
|
|
71
|
+
throw new ConvexError("Creem subscription is missing customer id");
|
|
72
|
+
}
|
|
73
|
+
const productId =
|
|
74
|
+
entityId(subscription.product) ??
|
|
75
|
+
subscription.items?.[0]?.productId ??
|
|
76
|
+
null;
|
|
77
|
+
if (!productId) {
|
|
78
|
+
throw new ConvexError("Creem subscription is missing product id");
|
|
79
|
+
}
|
|
80
|
+
const product =
|
|
81
|
+
typeof subscription.product === "object" && subscription.product
|
|
82
|
+
? subscription.product
|
|
83
|
+
: null;
|
|
84
|
+
const now = new Date().toISOString();
|
|
85
|
+
|
|
86
|
+
const periodStartStr = toIsoString(subscription.currentPeriodStartDate);
|
|
87
|
+
const periodEndStr = toIsoString(subscription.currentPeriodEndDate);
|
|
88
|
+
|
|
89
|
+
// Only `scheduled_cancel` is the resumable cancel-at-period-end state.
|
|
90
|
+
// `canceled` means truly ended (even if currentPeriodEnd is in the future).
|
|
91
|
+
const isScheduledCancel = subscription.status === "scheduled_cancel";
|
|
92
|
+
const isTrialing = subscription.status === "trialing";
|
|
93
|
+
|
|
94
|
+
// SDK's SubscriptionEntity type does not include `metadata` — zod strips it.
|
|
95
|
+
// Accept rawMetadata from the caller (extracted from the raw webhook object).
|
|
96
|
+
const metadata =
|
|
97
|
+
options?.rawMetadata ??
|
|
98
|
+
(subscription as { metadata?: Record<string, unknown> }).metadata ??
|
|
99
|
+
{};
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
id: subscription.id,
|
|
103
|
+
customerId,
|
|
104
|
+
productId,
|
|
105
|
+
status: subscription.status,
|
|
106
|
+
amount: product?.price ?? null,
|
|
107
|
+
currency: product?.currency ?? null,
|
|
108
|
+
recurringInterval: product?.billingPeriod ?? null,
|
|
109
|
+
currentPeriodStart:
|
|
110
|
+
periodStartStr ?? toIsoStringOrNow(subscription.createdAt),
|
|
111
|
+
currentPeriodEnd: periodEndStr,
|
|
112
|
+
cancelAtPeriodEnd: isScheduledCancel,
|
|
113
|
+
startedAt: periodStartStr ?? toIsoString(subscription.createdAt),
|
|
114
|
+
endedAt:
|
|
115
|
+
subscription.status === "canceled"
|
|
116
|
+
? (toIsoString(subscription.canceledAt) ?? now)
|
|
117
|
+
: null,
|
|
118
|
+
checkoutId: null,
|
|
119
|
+
metadata,
|
|
120
|
+
collectionMethod:
|
|
121
|
+
(subscription as { collectionMethod?: string }).collectionMethod ??
|
|
122
|
+
"charge_automatically",
|
|
123
|
+
discountId:
|
|
124
|
+
(subscription.discount as { id?: string } | undefined)?.id ?? null,
|
|
125
|
+
canceledAt: toIsoString(subscription.canceledAt),
|
|
126
|
+
endsAt: isScheduledCancel ? periodEndStr : null,
|
|
127
|
+
// Fix: populate trial dates from period dates when status is trialing
|
|
128
|
+
trialStart: isTrialing ? periodStartStr : null,
|
|
129
|
+
trialEnd: isTrialing ? periodEndStr : null,
|
|
130
|
+
seats: subscription.items?.[0]?.units ?? null,
|
|
131
|
+
lastTransactionId: subscription.lastTransactionId ?? null,
|
|
132
|
+
nextTransactionDate: toIsoString(subscription.nextTransactionDate),
|
|
133
|
+
mode: subscription.mode,
|
|
134
|
+
priceId: subscription.items?.[0]?.priceId,
|
|
135
|
+
createdAt: toIsoStringOrNow(subscription.createdAt),
|
|
136
|
+
modifiedAt: toIsoString(subscription.updatedAt),
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** Convert a raw order object (from checkout webhook) to the Convex DB order schema shape. Handles both snake_case and camelCase field names. */
|
|
141
|
+
export const convertToOrder = (
|
|
142
|
+
order: {
|
|
143
|
+
id: string;
|
|
144
|
+
customer?: string | null;
|
|
145
|
+
product: string;
|
|
146
|
+
amount: number;
|
|
147
|
+
currency: string;
|
|
148
|
+
status: string;
|
|
149
|
+
type: string;
|
|
150
|
+
transaction?: string | null;
|
|
151
|
+
sub_total?: number;
|
|
152
|
+
subTotal?: number;
|
|
153
|
+
tax_amount?: number;
|
|
154
|
+
taxAmount?: number;
|
|
155
|
+
discount_amount?: number;
|
|
156
|
+
discountAmount?: number;
|
|
157
|
+
amount_due?: number;
|
|
158
|
+
amountDue?: number;
|
|
159
|
+
amount_paid?: number;
|
|
160
|
+
amountPaid?: number;
|
|
161
|
+
discount?: string | null;
|
|
162
|
+
affiliate?: string | null;
|
|
163
|
+
mode?: string;
|
|
164
|
+
createdAt?: Date | string | null;
|
|
165
|
+
updatedAt?: Date | string | null;
|
|
166
|
+
created_at?: string | null;
|
|
167
|
+
updated_at?: string | null;
|
|
168
|
+
},
|
|
169
|
+
options?: {
|
|
170
|
+
checkoutId?: string | null;
|
|
171
|
+
metadata?: Record<string, unknown>;
|
|
172
|
+
},
|
|
173
|
+
): Infer<typeof schema.tables.orders.validator> => {
|
|
174
|
+
const now = new Date().toISOString();
|
|
175
|
+
return {
|
|
176
|
+
id: order.id,
|
|
177
|
+
customerId: order.customer ?? "",
|
|
178
|
+
productId: order.product,
|
|
179
|
+
amount: order.amount,
|
|
180
|
+
currency: order.currency,
|
|
181
|
+
status: order.status,
|
|
182
|
+
type: order.type,
|
|
183
|
+
subTotal: order.subTotal ?? order.sub_total,
|
|
184
|
+
taxAmount: order.taxAmount ?? order.tax_amount,
|
|
185
|
+
discountAmount: order.discountAmount ?? order.discount_amount,
|
|
186
|
+
amountDue: order.amountDue ?? order.amount_due,
|
|
187
|
+
amountPaid: order.amountPaid ?? order.amount_paid,
|
|
188
|
+
transactionId: order.transaction ?? null,
|
|
189
|
+
checkoutId: options?.checkoutId ?? null,
|
|
190
|
+
discountId: order.discount ?? null,
|
|
191
|
+
affiliate: order.affiliate ?? null,
|
|
192
|
+
mode: order.mode,
|
|
193
|
+
metadata: (options?.metadata as Record<string, string>) ?? undefined,
|
|
194
|
+
createdAt:
|
|
195
|
+
toIsoString(order.createdAt) ?? toIsoString(order.created_at) ?? now,
|
|
196
|
+
updatedAt:
|
|
197
|
+
toIsoString(order.updatedAt) ?? toIsoString(order.updated_at) ?? now,
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** Convert a Creem SDK `ProductEntity` to the Convex DB product schema shape. */
|
|
202
|
+
export const convertToDatabaseProduct = (
|
|
203
|
+
product: CreemProduct,
|
|
204
|
+
): Infer<typeof schema.tables.products.validator> => {
|
|
205
|
+
return {
|
|
206
|
+
id: product.id,
|
|
207
|
+
name: product.name,
|
|
208
|
+
description: product.description,
|
|
209
|
+
price: product.price,
|
|
210
|
+
currency: product.currency,
|
|
211
|
+
billingType: product.billingType,
|
|
212
|
+
billingPeriod: product.billingPeriod,
|
|
213
|
+
status: product.status,
|
|
214
|
+
taxMode: product.taxMode,
|
|
215
|
+
taxCategory: product.taxCategory,
|
|
216
|
+
imageUrl: (product as { imageUrl?: string }).imageUrl,
|
|
217
|
+
productUrl: (product as { productUrl?: string }).productUrl,
|
|
218
|
+
defaultSuccessUrl: product.defaultSuccessUrl,
|
|
219
|
+
mode: product.mode,
|
|
220
|
+
features: product.features?.map((f) => ({
|
|
221
|
+
id: f.id,
|
|
222
|
+
description: f.description,
|
|
223
|
+
})),
|
|
224
|
+
metadata: {},
|
|
225
|
+
createdAt: toIsoStringOrNow(product.createdAt),
|
|
226
|
+
modifiedAt: toIsoString(product.updatedAt),
|
|
227
|
+
};
|
|
228
|
+
};
|