📈 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)