Skip to content

Commit

Permalink
feat: moving product queries to adapter (#284)
Browse files Browse the repository at this point in the history
* feat: product query to adapter

* fix: types

* fix: tables for tests were stale

* fix: don't load from /dist

* fix: send in a mock stripe provider if one isn't supplied

* fix: payment provider is optional

* feat(cb): move utils and basic pricing types etc out of commerce-next and into core

these things aren't next specific and should be in core

* fix: sort a package.json file

* remove the `react-rsc` package

* add sql to shiki list

* don't log pinecone as error

* don't log missing article as error

* try to add role better

* hotfix: handle refunds properly

* hotfix: handle refunds properly

* fix: redirect to discord after connecting

* delete stripe adapter

* fix: types

* fix: tables for tests were stale

* fix: don't load from /dist

* fix: send in a mock stripe provider if one isn't supplied

* fix: payment provider is optional

* feat(cb): move utils and basic pricing types etc out of commerce-next and into core

these things aren't next specific and should be in core

* fix: sort a package.json file

* remove the `react-rsc` package

* package lock
  • Loading branch information
joelhooks authored Sep 3, 2024
1 parent da99aa0 commit 9ff95c1
Show file tree
Hide file tree
Showing 137 changed files with 1,103 additions and 3,415 deletions.
1 change: 0 additions & 1 deletion apps/astro-party/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"@coursebuilder/core": "0.2.2",
"@coursebuilder/email-templates": "^1.0.2",
"@coursebuilder/next": "workspace:^",
"@coursebuilder/react-rsc": "workspace:^",
"@coursebuilder/ui": "^1.0.15",
"@fontsource/inter": "^5.0.16",
"@heroicons/react": "^2.0.18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import { env } from '@/env.mjs'
import { pricingClassNames } from '@/styles/commerce'

import { PriceCheckProvider } from '@coursebuilder/commerce-next/pricing/pricing-check-context'
import {
PricingData,
PricingWidget,
} from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { PricingWidget } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { CommerceProps, PricingData } from '@coursebuilder/core/types'

export function ProductPricing({
product,
Expand Down
6 changes: 5 additions & 1 deletion apps/astro-party/src/app/(commerce)/products/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ParsedUrlQuery } from 'querystring'
import * as React from 'react'
import { Suspense } from 'react'
import type { Metadata, ResolvingMetadata } from 'next'
import { headers } from 'next/headers'
import Link from 'next/link'
import { ProductPricing } from '@/app/(commerce)/products/[slug]/_components/product-pricing'
import { Layout } from '@/components/layout'
Expand All @@ -13,7 +14,7 @@ import { getServerAuthSession } from '@/server/auth'
import { getOGImageUrlForResource } from '@/utils/get-og-image-url-for-resource'
import { count, eq } from 'drizzle-orm'

import { propsForCommerce } from '@coursebuilder/commerce-next/pricing/props-for-commerce'
import { propsForCommerce } from '@coursebuilder/core/pricing/props-for-commerce'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import { Button } from '@coursebuilder/ui'

Expand Down Expand Up @@ -118,6 +119,8 @@ async function ProductCommerce({
const pricingDataLoader = getPricingData({ productId: product?.id })
let productProps: any

const countryCode =
headers().get('x-vercel-ip-country') || process.env.DEFAULT_COUNTRY || 'US'
let commerceProps = await propsForCommerce(
{
query: {
Expand All @@ -126,6 +129,7 @@ async function ProductCommerce({
},
userId: user?.id,
products: [product],
countryCode,
},
courseBuilderAdapter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import pluralize from 'pluralize'
import { useFormStatus } from 'react-dom'

import InviteTeam from '@coursebuilder/commerce-next/team/invite-team'
import { buildStripeCheckoutPath } from '@coursebuilder/commerce-next/utils/build-stripe-checkout-path'
import { formatUsd } from '@coursebuilder/commerce-next/utils/format-usd'
import { buildStripeCheckoutPath } from '@coursebuilder/core/pricing/build-stripe-checkout-path'
import type {
ContentResource,
Product,
Purchase,
} from '@coursebuilder/core/schemas'
import type { FormattedPrice } from '@coursebuilder/core/types'
import { formatUsd } from '@coursebuilder/core/utils/format-usd'
import { Button, Progress, useToast } from '@coursebuilder/ui'
import { useVideoPlayerOverlay } from '@coursebuilder/ui/hooks/use-video-player-overlay'
import type { CompletedAction } from '@coursebuilder/ui/hooks/use-video-player-overlay'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Event } from '@/lib/events'
import { MDXRemoteSerializeResult } from 'next-mdx-remote'

import { PricingData } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import { CommerceProps, PricingData } from '@coursebuilder/core/types'

export type EventPageProps = {
event: Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as React from 'react'

import { useCoupon } from '@coursebuilder/commerce-next/coupons/use-coupon'
import * as Pricing from '@coursebuilder/commerce-next/pricing/pricing'
import type { PricingOptions } from '@coursebuilder/commerce-next/pricing/pricing-props'
import type { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import type { FormattedPrice } from '@coursebuilder/core/types'
import type {
CommerceProps,
FormattedPrice,
PricingOptions,
} from '@coursebuilder/core/types'

export type PricingData = {
formattedPrice?: FormattedPrice | null
Expand Down
8 changes: 7 additions & 1 deletion apps/astro-party/src/app/(content)/events/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'
import type { Metadata, ResolvingMetadata } from 'next'
import { headers } from 'next/headers'
import Link from 'next/link'
import { notFound } from 'next/navigation'
import { CldImage } from '@/app/_components/cld-image'
Expand All @@ -20,7 +21,7 @@ import { first } from 'lodash'
import ReactMarkdown from 'react-markdown'
import { Event as EventMetaSchema, Ticket } from 'schema-dts'

import { propsForCommerce } from '@coursebuilder/commerce-next/pricing/props-for-commerce'
import { propsForCommerce } from '@coursebuilder/core/pricing/props-for-commerce'
import { Product, productSchema, Purchase } from '@coursebuilder/core/schemas'
import { Button } from '@coursebuilder/ui'

Expand Down Expand Up @@ -82,6 +83,10 @@ export default async function EventPage({
productId: product.id,
})

const countryCode =
headers().get('x-vercel-ip-country') ||
process.env.DEFAULT_COUNTRY ||
'US'
const commerceProps = await propsForCommerce(
{
query: {
Expand All @@ -90,6 +95,7 @@ export default async function EventPage({
},
userId: user?.id,
products: [productParsed.data],
countryCode,
},
courseBuilderAdapter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Check } from 'lucide-react'

import { useCoupon } from '@coursebuilder/commerce-next/coupons/use-coupon'
import * as Pricing from '@coursebuilder/commerce-next/pricing/pricing'
import type { PricingOptions } from '@coursebuilder/commerce-next/pricing/pricing-props'
import type { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import type { FormattedPrice } from '@coursebuilder/core/types'
import type {
CommerceProps,
FormattedPrice,
PricingOptions,
} from '@coursebuilder/core/types'

export type PricingData = {
formattedPrice?: FormattedPrice | null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type Module } from '@/lib/module'
import { MDXRemoteSerializeResult } from 'next-mdx-remote'

import { PricingData } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import { CommerceProps, PricingData } from '@coursebuilder/core/types'

export type WorkshopPageProps = {
quantityAvailable: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as React from 'react'
import { headers } from 'next/headers'
import { courseBuilderAdapter } from '@/db'
import { Module } from '@/lib/module'
import { getPricingData } from '@/lib/pricing-query'
import { getWorkshopProduct } from '@/lib/workshops-query'
import { getServerAuthSession } from '@/server/auth'
import first from 'lodash/first'

import { propsForCommerce } from '@coursebuilder/commerce-next/pricing/props-for-commerce'
import { propsForCommerce } from '@coursebuilder/core/pricing/props-for-commerce'
import {
productSchema,
type Product,
Expand Down Expand Up @@ -38,6 +39,10 @@ export async function WorkshopPricing({
productId: product.id,
})

const countryCode =
headers().get('x-vercel-ip-country') ||
process.env.DEFAULT_COUNTRY ||
'US'
const commerceProps = await propsForCommerce(
{
query: {
Expand All @@ -46,6 +51,7 @@ export async function WorkshopPricing({
},
userId: user?.id,
products: [product],
countryCode,
},
courseBuilderAdapter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { usePathname } from 'next/navigation'
import { env } from '@/env.mjs'

import { PriceCheckProvider } from '@coursebuilder/commerce-next/pricing/pricing-check-context'
import { PricingData } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import type { Product } from '@coursebuilder/core/schemas'

import { PricingWidget } from './pricing-widget'
import type { WorkshopPageProps } from './workshop-page-props'
Expand Down
2 changes: 1 addition & 1 deletion apps/astro-party/src/app/(user)/team/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { getPricingData } from '@/lib/pricing-query'
import { getServerAuthSession } from '@/server/auth'
import { eq } from 'drizzle-orm'

import type { PricingData } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import {
type Coupon,
type Purchase,
type UpgradableProduct,
type User,
} from '@coursebuilder/core/schemas'
import type { PricingData } from '@coursebuilder/core/types'

export type TeamPageData = {
bulkPurchases: {
Expand Down
8 changes: 5 additions & 3 deletions apps/astro-party/src/app/_components/home-pricing-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import pluralize from 'pluralize'

import { useCoupon } from '@coursebuilder/commerce-next/coupons/use-coupon'
import * as Pricing from '@coursebuilder/commerce-next/pricing/pricing'
import type { PricingOptions } from '@coursebuilder/commerce-next/pricing/pricing-props'
import type { CommerceProps } from '@coursebuilder/commerce-next/utils/commerce-props'
import { Product, Purchase } from '@coursebuilder/core/schemas'
import type { FormattedPrice } from '@coursebuilder/core/types'
import type {
CommerceProps,
FormattedPrice,
PricingOptions,
} from '@coursebuilder/core/types'

export type PricingData = {
formattedPrice?: FormattedPrice | null
Expand Down
2 changes: 1 addition & 1 deletion apps/astro-party/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { AxiomWebVitals } from 'next-axiom'
import { extractRouterConfig } from 'uploadthing/server'

import { CouponProvider } from '@coursebuilder/commerce-next/coupons/coupon-context'
import { getCouponForCode } from '@coursebuilder/commerce-next/pricing/props-for-commerce'
import { getCouponForCode } from '@coursebuilder/core/pricing/props-for-commerce'

const fredokaFont = Fredoka({
subsets: ['latin'],
Expand Down
2 changes: 1 addition & 1 deletion apps/astro-party/src/components/module-resources-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { getVideoResource } from '@/lib/video-resource-query'
import { Plus } from 'lucide-react'

import type { ContentResource } from '@coursebuilder/core/schemas'
import { NewResourceWithVideoForm } from '@coursebuilder/react-rsc/client'
import {
Button,
Dialog,
Expand All @@ -28,6 +27,7 @@ import {
DialogTrigger,
} from '@coursebuilder/ui'
import { CreateResourceForm } from '@coursebuilder/ui/resources-crud/create-resource-form'
import { NewResourceWithVideoForm } from '@coursebuilder/ui/resources-crud/new-resource-with-video-form'

import { LessonUploader } from './resources-crud/lesson-uploader'

Expand Down
88 changes: 0 additions & 88 deletions apps/astro-party/src/coursebuilder/stripe-payment-adapter.ts

This file was deleted.

10 changes: 7 additions & 3 deletions apps/astro-party/src/coursebuilder/stripe-provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { StripePaymentAdapter } from '@/coursebuilder/stripe-payment-adapter'
import { env } from '@/env.mjs'

import StripeProvider from '@coursebuilder/core/providers/stripe'
import StripeProvider, {
StripePaymentAdapter,
} from '@coursebuilder/core/providers/stripe'

export const stripeProvider = StripeProvider({
errorRedirectUrl: `${env.COURSEBUILDER_URL}`,
baseSuccessUrl: `${env.COURSEBUILDER_URL}`,
cancelUrl: `${env.COURSEBUILDER_URL}`,
paymentsAdapter: new StripePaymentAdapter(),
paymentsAdapter: new StripePaymentAdapter({
stripeToken: env.STRIPE_SECRET_TOKEN,
stripeWebhookSecret: env.STRIPE_WEBHOOK_SECRET,
}),
})
7 changes: 6 additions & 1 deletion apps/astro-party/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stripeProvider } from '@/coursebuilder/stripe-provider'
import { mysqlTable } from '@/db/mysql-table'
import { env } from '@/env.mjs'
import { Client } from '@planetscale/database'
Expand All @@ -14,4 +15,8 @@ export const db = drizzle(
{ schema },
)

export const courseBuilderAdapter = DrizzleAdapter(db, mysqlTable)
export const courseBuilderAdapter = DrizzleAdapter(
db,
mysqlTable,
stripeProvider,
)
6 changes: 4 additions & 2 deletions apps/astro-party/src/lib/pricing-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { courseBuilderAdapter, db } from '@/db'
import { purchases } from '@/db/schema'
import { eq } from 'drizzle-orm'

import { PricingData } from '@coursebuilder/commerce-next/pricing/pricing-widget'
import { formatPricesForProduct } from '@coursebuilder/core'
import { type FormatPricesForProductOptions } from '@coursebuilder/core/types'
import {
PricingData,
type FormatPricesForProductOptions,
} from '@coursebuilder/core/types'

export async function getPricingData(
options: Partial<FormatPricesForProductOptions>,
Expand Down
Loading

0 comments on commit 9ff95c1

Please sign in to comment.