2
0

Add e2e tests for account

This commit is contained in:
Baptiste Arnaud
2021-12-28 11:13:09 +01:00
parent e10fe1a186
commit 8c826fcf70
13 changed files with 321 additions and 302 deletions

View File

@ -0,0 +1,32 @@
import { User } from 'db'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'services/api/utils'
import Stripe from 'stripe'
const createCheckoutSession = async (
req: NextApiRequest,
res: NextApiResponse
) => {
const session = await getSession({ req })
if (!session?.user)
return res.status(401).json({ message: 'Not authenticated' })
const user = session.user as User
if (!user.stripeId)
return res.status(401).json({ message: 'Not authenticated' })
if (req.method === 'GET') {
if (!process.env.STRIPE_SECRET_KEY)
throw Error('STRIPE_SECRET_KEY var is missing')
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
})
const session = await stripe.billingPortal.sessions.create({
customer: user.stripeId,
return_url: `${req.headers.origin}/account`,
})
res.status(201).redirect(session.url)
}
return methodNotAllowed(res)
}
export default createCheckoutSession