📈 Add telemetry webhook

Closes #357
This commit is contained in:
Baptiste Arnaud
2023-03-14 14:18:05 +01:00
parent e7132116f4
commit 9ca17e4e0b
22 changed files with 523 additions and 34 deletions

View File

@@ -2,7 +2,6 @@
import { PrismaClient, Prisma, WorkspaceRole, Session } from 'db'
import type { Adapter, AdapterUser } from 'next-auth/adapters'
import { createId } from '@paralleldrive/cuid2'
import { got } from 'got'
import { generateId } from 'utils'
import { parseWorkspaceDefaultPlan } from '@/features/workspace'
import {
@@ -10,6 +9,8 @@ import {
convertInvitationsToCollaborations,
joinWorkspaces,
} from '@/features/auth/api'
import { sendTelemetryEvents } from 'utils/telemetry/sendTelemetryEvent'
import { TelemetryEvent } from 'models/features/telemetry'
export function CustomAdapter(p: PrismaClient): Adapter {
return {
@@ -28,6 +29,11 @@ export function CustomAdapter(p: PrismaClient): Adapter {
workspaceInvitations.length === 0
)
throw Error('New users are forbidden')
const newWorkspaceData = {
name: data.name ? `${data.name}'s workspace` : `My workspace`,
plan: parseWorkspaceDefaultPlan(data.email),
}
const createdUser = await p.user.create({
data: {
...data,
@@ -42,25 +48,35 @@ export function CustomAdapter(p: PrismaClient): Adapter {
create: {
role: WorkspaceRole.ADMIN,
workspace: {
create: {
name: data.name
? `${data.name}'s workspace`
: `My workspace`,
plan: parseWorkspaceDefaultPlan(data.email),
},
create: newWorkspaceData,
},
},
},
onboardingCategories: [],
},
include: {
workspaces: { select: { workspaceId: true } },
},
})
if (process.env.USER_CREATED_WEBHOOK_URL)
await got.post(process.env.USER_CREATED_WEBHOOK_URL, {
json: {
email: data.email,
name: data.name ? (data.name as string).split(' ')[0] : undefined,
},
const newWorkspaceId = createdUser.workspaces.pop()?.workspaceId
const events: TelemetryEvent[] = []
if (newWorkspaceId) {
events.push({
name: 'Workspace created',
workspaceId: newWorkspaceId,
userId: createdUser.id,
data: newWorkspaceData,
})
}
events.push({
name: 'User created',
userId: createdUser.id,
data: {
email: data.email,
name: data.name ? (data.name as string).split(' ')[0] : undefined,
},
})
await sendTelemetryEvents(events)
if (invitations.length > 0)
await convertInvitationsToCollaborations(p, user, invitations)
if (workspaceInvitations.length > 0)

View File

@@ -4,6 +4,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { canPublishFileInput } from '@/utils/api/dbRules'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'
import { getAuthenticatedUser } from '@/features/auth/api'
import { sendTelemetryEvents } from 'utils/telemetry/sendTelemetryEvent'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
@@ -23,10 +24,25 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
!(await canPublishFileInput({ userId: user.id, workspaceId, res }))
)
return
const typebot = await prisma.publicTypebot.create({
const publicTypebot = await prisma.publicTypebot.create({
data: { ...data },
include: {
typebot: { select: { name: true } },
},
})
return res.send(typebot)
await sendTelemetryEvents([
{
name: 'Typebot published',
userId: user.id,
workspaceId,
typebotId: publicTypebot.typebotId,
data: {
isFirstPublish: true,
name: publicTypebot.typebot.name,
},
},
])
return res.send(publicTypebot)
}
return methodNotAllowed(res)
} catch (err) {

View File

@@ -4,6 +4,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { canPublishFileInput, canWriteTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'
import { sendTelemetryEvents } from 'utils/telemetry/sendTelemetryEvent'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
@@ -25,11 +26,25 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
!(await canPublishFileInput({ userId: user.id, workspaceId, res }))
)
return
const typebots = await prisma.publicTypebot.update({
const publicTypebot = await prisma.publicTypebot.update({
where: { id },
data,
include: {
typebot: { select: { name: true } },
},
})
return res.send({ typebots })
await sendTelemetryEvents([
{
name: 'Typebot published',
userId: user.id,
workspaceId,
typebotId: publicTypebot.typebotId,
data: {
name: publicTypebot.typebot.name,
},
},
])
return res.send({ typebot: publicTypebot })
}
if (req.method === 'DELETE') {
const publishedTypebotId = req.query.id as string

View File

@@ -36,6 +36,7 @@ const createCheckoutSession = async (userId: string) => {
mode: 'subscription',
metadata: {
claimableCustomPlanId: claimableCustomPlan.id,
userId,
},
currency: claimableCustomPlan.currency,
automatic_tax: { enabled: true },

View File

@@ -6,6 +6,7 @@ import { buffer } from 'micro'
import prisma from '@/lib/prisma'
import { Plan } from 'db'
import { RequestHandler } from 'next/dist/server/next'
import { sendTelemetryEvents } from 'utils/telemetry/sendTelemetryEvent'
if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET)
throw new Error('STRIPE_SECRET_KEY or STRIPE_WEBHOOK_SECRET missing')
@@ -46,11 +47,17 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
additionalChats: string
additionalStorage: string
workspaceId: string
userId: string
}
| { claimableCustomPlanId: string }
| { claimableCustomPlanId: string; userId: string }
if ('plan' in metadata) {
const { workspaceId, plan, additionalChats, additionalStorage } =
metadata
const {
workspaceId,
plan,
additionalChats,
additionalStorage,
userId,
} = metadata
if (!workspaceId || !plan || !additionalChats || !additionalStorage)
return res
.status(500)
@@ -58,7 +65,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
await prisma.workspace.update({
where: { id: workspaceId },
data: {
plan: plan,
plan,
stripeId: session.customer as string,
additionalChatsIndex: parseInt(additionalChats),
additionalStorageIndex: parseInt(additionalStorage),
@@ -68,8 +75,21 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
storageLimitSecondEmailSentAt: null,
},
})
await sendTelemetryEvents([
{
name: 'Subscription updated',
workspaceId,
userId,
data: {
plan,
additionalChatsIndex: parseInt(additionalChats),
additionalStorageIndex: parseInt(additionalStorage),
},
},
])
} else {
const { claimableCustomPlanId } = metadata
const { claimableCustomPlanId, userId } = metadata
if (!claimableCustomPlanId)
return res
.status(500)
@@ -90,6 +110,19 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
customSeatsLimit: seatsLimit,
},
})
await sendTelemetryEvents([
{
name: 'Subscription updated',
workspaceId,
userId,
data: {
plan: Plan.CUSTOM,
additionalChatsIndex: 0,
additionalStorageIndex: 0,
},
},
])
}
return res.status(200).send({ message: 'workspace upgraded in DB' })

View File

@@ -11,6 +11,7 @@ import { getAuthenticatedUser } from '@/features/auth/api'
import { parseNewTypebot } from '@/features/dashboard'
import { NewTypebotProps } from '@/features/dashboard/api/parseNewTypebot'
import { omit } from 'utils'
import { sendTelemetryEvents } from 'utils/telemetry/sendTelemetryEvent'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
@@ -65,6 +66,17 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
...data,
}),
})
await sendTelemetryEvents([
{
name: 'Typebot created',
userId: user.id,
workspaceId: typebot.workspaceId,
typebotId: typebot.id,
data: {
name: typebot.name,
},
},
])
return res.send(typebot)
}
return methodNotAllowed(res)