This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
signup.ts
84 lines (73 loc) · 2.28 KB
/
signup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { resolver, SecurePassword } from "blitz"
import db from "db"
import { Signup } from "app/auth/validations"
import { Role } from "types"
import { Knock } from "@knocklabs/node"
import { NEW_ASSET, NEW_COMMENT, WELCOME } from "app/lib/workflows"
const knockClient = new Knock(process.env.KNOCK_API_KEY)
export default resolver.pipe(resolver.zod(Signup), async ({ email, password, name }, ctx) => {
const hashedPassword = await SecurePassword.hash(password.trim())
// assume all users are signing up for the same workspace for now
const workspace = await db.workspace.findFirst()
const user = await db.user.create({
data: {
name,
email: email.toLowerCase().trim(),
hashedPassword,
role: "USER",
},
select: { id: true, name: true, email: true, role: true },
})
if (user) {
if (workspace) {
await db.workspace_seat.create({
data: {
userId: user.id,
workspaceId: workspace.id,
},
})
}
// Identify user on Knock so we the data is accessible when triggering workflows
await knockClient.users.identify(`${user.id}`, { email: user.email, name: user.name })
const notify = { success: false }
// Trigger the welcome email workflow
try {
await knockClient.notify(WELCOME, {
data: {
workspace: workspace?.name || "Default",
},
recipients: [`${user.id}`],
})
notify.success = true
} catch (error) {
console.error("Welcome notification error:", error)
}
try {
await knockClient.users.setWorkflowsPreferences(`${user.id}`, {
[NEW_COMMENT]: {
conditions: [
{
variable: "recipient.muted_projects",
operator: "not_contains",
argument: "data.projectId",
},
],
},
[NEW_ASSET]: {
conditions: [
{
variable: "recipient.muted_projects",
operator: "not_contains",
argument: "data.projectId",
},
],
},
})
} catch (error) {
notify.success = false
console.error("Error setting preferences:", error)
}
await ctx.session.$create({ userId: user.id, role: user.role as Role })
return { user, notify }
}
})