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

View File

@ -1,11 +1,5 @@
import {
BackgroundType,
Settings,
StartBlock,
StepType,
Theme,
} from 'bot-engine'
import { Typebot, User } from 'db'
import { parseNewTypebot } from 'bot-engine'
import { User } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
@ -30,35 +24,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ typebots })
}
if (req.method === 'POST') {
const data = JSON.parse(req.body) as Typebot
const startBlock: StartBlock = {
id: 'start-block',
title: 'Start',
graphCoordinates: { x: 0, y: 0 },
steps: [
{
id: 'start-step',
blockId: 'start-block',
label: 'Form starts here',
type: StepType.START,
},
],
}
const theme: Theme = {
general: {
font: 'Open Sans',
background: { type: BackgroundType.NONE, content: '#ffffff' },
},
}
const settings: Settings = {
typingEmulation: {
enabled: true,
speed: 300,
maxDelay: 1.5,
},
}
const data = JSON.parse(req.body)
const typebot = await prisma.typebot.create({
data: { ...data, ownerId: user.id, startBlock, theme, settings },
data: parseNewTypebot({ ownerId: user.id, ...data }),
})
return res.send(typebot)
}