Skip to content

Commit

Permalink
feat(ew): use payment options in inngest funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbranchaud committed Mar 27, 2024
1 parent 48ef1ab commit 299d3eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {inngest} from 'inngest/inngest.server'
import {v4} from 'uuid'
import {prisma} from '@skillrecordings/database'
import {defaultContext as defaultStripeContext} from '@skillrecordings/stripe-sdk'
import {loadSanityProduct} from './index'
import {sanityWriteClient} from 'utils/sanity-server'
import {SANITY_WEBHOOK_EVENT} from '../sanity-inngest-events'
import {paymentOptions} from 'pages/api/skill/[...skillRecordings]'

const {stripe} = defaultStripeContext
const stripe = paymentOptions.providers.stripe?.paymentClient

export const sanityProductCreated = inngest.createFunction(
{id: `product-create`, name: 'Create Product in Database'},
Expand All @@ -15,6 +15,10 @@ export const sanityProductCreated = inngest.createFunction(
if: 'event.data.event == "product.create"',
},
async ({event, step}) => {
if (!stripe) {
throw new Error('Payment provider (Stripe) is missing')
}

const sanityProduct = await step.run('get sanity product', async () => {
return loadSanityProduct(event.data._id)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {inngest} from 'inngest/inngest.server'
import {prisma} from '@skillrecordings/database'
import {SANITY_WEBHOOK_EVENT} from '../sanity-inngest-events'
import {defaultContext as defaultStripeContext} from '@skillrecordings/stripe-sdk'
import {paymentOptions} from 'pages/api/skill/[...skillRecordings]'

const {stripe} = defaultStripeContext
const stripe = paymentOptions.providers.stripe?.paymentClient

export const sanityProductDeleted = inngest.createFunction(
{id: `product-delete`, name: 'Deactivate Product in Database'},
Expand All @@ -12,6 +12,10 @@ export const sanityProductDeleted = inngest.createFunction(
if: 'event.data.event == "product.delete"',
},
async ({event, step}) => {
if (!stripe) {
throw new Error('Payment provider (Stripe) is missing')
}

const {productId} = event.data

if (!productId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {prisma} from '@skillrecordings/database'
import {v4} from 'uuid'
import {loadSanityProduct} from './index'
import {SANITY_WEBHOOK_EVENT} from '../sanity-inngest-events'
import {defaultContext as defaultStripeContext} from '@skillrecordings/stripe-sdk'
import {paymentOptions} from 'pages/api/skill/[...skillRecordings]'

const {stripe} = defaultStripeContext
const stripe = paymentOptions.providers.stripe?.paymentClient

export const sanityProductUpdated = inngest.createFunction(
{
Expand All @@ -22,6 +22,10 @@ export const sanityProductUpdated = inngest.createFunction(
if: 'event.data.event == "product.update"',
},
async ({event, step}) => {
if (!stripe) {
throw new Error('Payment provider (Stripe) is missing')
}

const sanityProduct = await step.run('get sanity product', async () => {
return loadSanityProduct(event.data._id)
})
Expand Down
23 changes: 11 additions & 12 deletions apps/epic-web/src/inngest/functions/stripe/webhook-received.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import {prisma} from '@skillrecordings/database'
import {NonRetriableError} from 'inngest'
import {postToSlack} from '@skillrecordings/skill-api'
import {WebClient} from '@slack/web-api'
import {
defaultContext as defaultStripeContext,
Stripe,
} from '@skillrecordings/stripe-sdk'
import {paymentOptions} from 'pages/api/skill/[...skillRecordings]'
import {z} from 'zod'

const {stripe} = defaultStripeContext
const stripe = paymentOptions.providers.stripe?.paymentClient

const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
})
const CustomerSchema = z.object({id: z.string(), email: z.string()})

export const stripeWebhookReceived = inngest.createFunction(
{id: `stripe-webhook-received`, name: 'Stripe Webhook Received'},
{event: STRIPE_WEBHOOK_RECEIVED_EVENT},
async ({event, step}) => {
if (!stripe) {
throw new Error('Payment provider (Stripe) is missing')
}

const stripeAccountId = await step.run(
'get stripe account id',
async () => {
Expand Down Expand Up @@ -54,9 +53,9 @@ export const stripeWebhookReceived = inngest.createFunction(
}

const customer = await step.run('get customer', async () => {
return (await stripe.customers.retrieve(
invoice.customer as string,
)) as Stripe.Customer
return CustomerSchema.parse(
await stripe.customers.retrieve(invoice.customer as string),
)
})

if (!customer) {
Expand Down

0 comments on commit 299d3eb

Please sign in to comment.