-
-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathactivity.functions.ts
More file actions
63 lines (57 loc) · 1.69 KB
/
activity.functions.ts
File metadata and controls
63 lines (57 loc) · 1.69 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
import { createServerFn } from '@tanstack/react-start'
import * as v from 'valibot'
import {
recordDailyActivity,
getUserStreak,
getActiveUserStats,
getStreakLeaderboard,
getDailyActiveUserCounts,
} from './activity.server'
import { getAuthenticatedUser } from './auth.server-helpers'
import { requireAdmin } from './roles.server'
// Record activity for the current user (called on authenticated requests)
export const recordActivity = createServerFn({ method: 'POST' }).handler(
async () => {
const user = await getAuthenticatedUser()
await recordDailyActivity(user.userId)
return { success: true }
},
)
// Get current user's streak info
export const getMyStreak = createServerFn({ method: 'POST' }).handler(
async () => {
const user = await getAuthenticatedUser()
const streak = await getUserStreak(user.userId)
return streak
},
)
// Get activity stats for admin dashboard
export const getActivityStatsAdmin = createServerFn({ method: 'POST' }).handler(
async () => {
await requireAdmin()
const [activeUserStats, streakLeaderboard, dailyActiveUsers] =
await Promise.all([
getActiveUserStats(),
getStreakLeaderboard(10),
getDailyActiveUserCounts(),
])
return {
...activeUserStats,
streakLeaderboard,
dailyActiveUsers,
}
},
)
/**
* Get daily active user data for charts with configurable time range
*/
export const getDauChartData = createServerFn({ method: 'POST' })
.inputValidator(
v.object({
days: v.optional(v.nullable(v.number())), // null = all time
}),
)
.handler(async ({ data: { days } }) => {
await requireAdmin()
return getDailyActiveUserCounts(days ?? null)
})