-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.ts
More file actions
183 lines (164 loc) · 4.41 KB
/
Copy pathusers.ts
File metadata and controls
183 lines (164 loc) · 4.41 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { ConvexError, v } from 'convex/values'
import { internalMutation, internalQuery, query } from './_generated/server'
import {
adminAuthQuery,
authQuery,
pointsPerSuggestionType
} from '~/convex/utils'
import type { SuggestionTypeCount } from '~/convex/types'
import type { Id } from '~/convex/_generated/dataModel'
import { paginationOptsValidator } from 'convex/server'
export const getUserByClerkId = internalQuery({
args: { userId: v.string() },
handler: async ({ db }, { userId }) => {
return await db
.query('users')
.withIndex('by_userId', (q) => q.eq('userId', userId))
.first()
}
})
export const getUserById = internalQuery({
args: { userId: v.id('users') },
handler: async ({ db }, { userId }) => {
return await db.get(userId)
}
})
export const getProfile = query({
args: { userId: v.id('users') },
handler: async ({ db }, { userId }) => {
const user = await db.get(userId)
return {
name: user?.name,
profileImage: user?.profileImage
}
}
})
export const createUser = internalMutation({
args: {
email: v.string(),
userId: v.string(),
name: v.string(),
profileImage: v.string()
},
handler: async ({ db }, args) => {
const { email, userId, profileImage, name } = args
const user = await db
.query('users')
.withIndex('by_userId', (q) => q.eq('userId', userId))
.first()
if (user) return
return await db.insert('users', {
email,
userId,
profileImage,
name
})
}
})
export const updateUser = internalMutation({
args: { userId: v.string(), name: v.string(), profileImage: v.string() },
handler: async ({ db }, args) => {
const { userId, profileImage, name } = args
const user = await db
.query('users')
.withIndex('by_userId', (q) => q.eq('userId', userId))
.first()
if (!user) {
throw new ConvexError('user not found')
}
await db.patch(user._id, {
name,
profileImage
})
}
})
export const getMyUser = authQuery({
async handler({ user }) {
return user
}
})
export const getTopContributors = authQuery({
async handler({ db }) {
const approvedSuggestions = await db
.query('suggestions')
.filter((q) => q.eq(q.field('approved'), true))
.collect()
// group by userId
const groupedSuggestions = approvedSuggestions.reduce(
(acc, suggestion) => {
const userId = suggestion.userId
const type = suggestion.type
if (!acc[userId]) {
acc[userId] = {
category: 0,
block: 0,
tech: 0,
points: 0
}
}
// increment the count for the type
acc[userId][type]++
// add points for the type
acc[userId].points += pointsPerSuggestionType[type]
return acc
},
{} as Record<string, SuggestionTypeCount>
)
// take top 5 contributors by points
return Object.entries(groupedSuggestions)
.sort(([, a], [, b]) => b.points - a.points)
.slice(0, 5)
.map(([userId, suggestionTypeCount]) => ({
userId: userId as Id<'users'>,
...suggestionTypeCount
}))
}
})
function countTopUsers<T extends { userId: Id<'users'> }[]>(data: T) {
const groupedData = data.reduce(
(acc, curr) => {
const userId = curr.userId
if (!acc[userId]) {
acc[userId] = 0
}
acc[userId]++
return acc
},
{} as Record<string, number>
)
// take top 5 stack builders
return Object.entries(groupedData)
.sort(([, a], [, b]) => b - a)
.slice(0, 5)
.map(([userId, count]) => ({
userId: userId as Id<'users'>,
count
}))
}
export const getTopStackBuilders = authQuery({
async handler({ db }) {
// get all stacks and group by userId
const stacks = await db
.query('stacks')
.filter((q) => q.eq(q.field('isPublic'), true))
.collect()
return countTopUsers<typeof stacks>(stacks)
}
})
export const getTopTemplateMakers = authQuery({
async handler({ db }) {
const templates = await db
.query('templates')
.filter((q) => q.eq(q.field('isPublic'), true))
.collect()
return countTopUsers<typeof templates>(templates)
}
})
export const getUsers = adminAuthQuery({
args: {
paginationOpts: paginationOptsValidator
},
async handler({ db }, { paginationOpts }) {
return db.query('users').order('desc').paginate(paginationOpts)
}
})