forked from webdevcody/file-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.ts
123 lines (102 loc) · 2.73 KB
/
users.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { ConvexError, v } from "convex/values";
import {
MutationCtx,
QueryCtx,
internalMutation,
query,
} from "./_generated/server";
import { roles } from "./schema";
import { hasAccessToOrg } from "./files";
export async function getUser(
ctx: QueryCtx | MutationCtx,
tokenIdentifier: string
) {
const user = await ctx.db
.query("users")
.withIndex("by_tokenIdentifier", (q) =>
q.eq("tokenIdentifier", tokenIdentifier)
)
.first();
if (!user) {
throw new ConvexError("expected user to be defined");
}
return user;
}
export const createUser = internalMutation({
args: { tokenIdentifier: v.string(), name: v.string(), image: v.string() },
async handler(ctx, args) {
await ctx.db.insert("users", {
tokenIdentifier: args.tokenIdentifier,
orgIds: [],
name: args.name,
image: args.image,
});
},
});
export const updateUser = internalMutation({
args: { tokenIdentifier: v.string(), name: v.string(), image: v.string() },
async handler(ctx, args) {
const user = await ctx.db
.query("users")
.withIndex("by_tokenIdentifier", (q) =>
q.eq("tokenIdentifier", args.tokenIdentifier)
)
.first();
if (!user) {
throw new ConvexError("no user with this token found");
}
await ctx.db.patch(user._id, {
name: args.name,
image: args.image,
});
},
});
export const addOrgIdToUser = internalMutation({
args: { tokenIdentifier: v.string(), orgId: v.string(), role: roles },
async handler(ctx, args) {
const user = await getUser(ctx, args.tokenIdentifier);
await ctx.db.patch(user._id, {
orgIds: [...user.orgIds, { orgId: args.orgId, role: args.role }],
});
},
});
export const updateRoleInOrgForUser = internalMutation({
args: { tokenIdentifier: v.string(), orgId: v.string(), role: roles },
async handler(ctx, args) {
const user = await getUser(ctx, args.tokenIdentifier);
const org = user.orgIds.find((org) => org.orgId === args.orgId);
if (!org) {
throw new ConvexError(
"expected an org on the user but was not found when updating"
);
}
org.role = args.role;
await ctx.db.patch(user._id, {
orgIds: user.orgIds,
});
},
});
export const getUserProfile = query({
args: { userId: v.id("users") },
async handler(ctx, args) {
const user = await ctx.db.get(args.userId);
return {
name: user?.name,
image: user?.image,
};
},
});
export const getMe = query({
args: {},
async handler(ctx) {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
return null;
}
const user = await getUser(ctx, identity.tokenIdentifier);
if (!user) {
return null;
}
return user;
},
});