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
85
86
87
88
89
90
91
92
93
|
import { zSignUpSchema } from "@/lib/types/api/users";
import { adminProcedure, publicProcedure, router } from "../trpc";
import { SqliteError } from "@hoarder/db";
import { z } from "zod";
import { hashPassword } from "@/server/auth";
import { TRPCError } from "@trpc/server";
import { users } from "@hoarder/db/schema";
import { count, eq } from "drizzle-orm";
export const usersAppRouter = router({
create: publicProcedure
.input(zSignUpSchema)
.output(
z.object({
id: z.string(),
name: z.string(),
email: z.string(),
role: z.enum(["user", "admin"]).nullable(),
}),
)
.mutation(async ({ input, ctx }) => {
// TODO: This is racy, but that's probably fine.
const [{ count: userCount }] = await ctx.db
.select({ count: count() })
.from(users);
try {
const result = await ctx.db
.insert(users)
.values({
name: input.name,
email: input.email,
password: await hashPassword(input.password),
role: userCount == 0 ? "admin" : "user",
})
.returning({
id: users.id,
name: users.name,
email: users.email,
role: users.role,
});
return result[0];
} catch (e) {
if (e instanceof SqliteError) {
if (e.code == "SQLITE_CONSTRAINT_UNIQUE") {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Email is already taken",
});
}
}
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Something went wrong",
});
}
}),
list: adminProcedure
.output(
z.object({
users: z.array(
z.object({
id: z.string(),
name: z.string(),
email: z.string(),
role: z.enum(["user", "admin"]).nullable(),
}),
),
}),
)
.query(async ({ ctx }) => {
const users = await ctx.db.query.users.findMany({
columns: {
id: true,
name: true,
email: true,
role: true,
},
});
return { users };
}),
delete: adminProcedure
.input(
z.object({
userId: z.string(),
}),
)
.mutation(async ({ input, ctx }) => {
const res = await ctx.db.delete(users).where(eq(users.id, input.userId));
if (res.changes == 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
}),
});
|