mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
wip: get active session
This commit is contained in:
1 parent
423548804a
commit
963655c1f7
7 files changed
+69
-12
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Session } from '@linux-odyssey/models'
|
||||
import { z } from 'zod'
|
||||
import { isQuestUnlocked } from '../models/sessionManager.js'
|
||||
import config from '../config.js'
|
||||
import { protectedProcedure, router } from '../trpc.js'
|
||||
|
||||
export const sessionRouter = router({
|
||||
getActiveSession: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
questId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async (opts) => {
|
||||
const { questId } = opts.input
|
||||
const { user } = opts.ctx
|
||||
if (
|
||||
user &&
|
||||
((await isQuestUnlocked(user, questId)) || config.testing.enabled)
|
||||
) {
|
||||
const session = await Session.findOne({
|
||||
user: user.id,
|
||||
quest: questId,
|
||||
status: 'active',
|
||||
})
|
||||
if (!session) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
_id: session._id.toString(),
|
||||
user: session.user,
|
||||
quest: session.quest,
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
responses: session.responses,
|
||||
graph: session.graph,
|
||||
}
|
||||
}
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Quest is locked',
|
||||
})
|
||||
}),
|
||||
})
|
||||
Reference in new issue
Block a user