mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
finish session controller
This commit is contained in:
1 parent
c0ec382510
commit
fd543ed594
3 files changed
+88
-77
No files matched your search
@@ -1,75 +0,0 @@
|
||||
import { matchedData } from 'express-validator'
|
||||
import { Session } from '@linux-odyssey/models'
|
||||
import {
|
||||
createNewSession,
|
||||
getOrCreateActiveSession,
|
||||
isQuestUnlocked,
|
||||
} from '../../models/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
import config from '../../config.js'
|
||||
|
||||
function sessionSummary(session) {
|
||||
return {
|
||||
_id: session._id,
|
||||
user: session.user._id || session.user,
|
||||
quest: session.quest._id || session.quest,
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
}
|
||||
}
|
||||
|
||||
function sessionDetail(session) {
|
||||
return {
|
||||
...sessionSummary(session),
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
graph: session.graph,
|
||||
}
|
||||
}
|
||||
|
||||
export const getSessionList = asyncHandler(async (req, res) => {
|
||||
const query = { user: req.user._id }
|
||||
const { questId, status } = matchedData(req)
|
||||
if (questId) {
|
||||
query.quest = questId
|
||||
}
|
||||
query.status = status || 'active'
|
||||
const sessions = await Session.find(query)
|
||||
res.json(sessions.map(sessionSummary))
|
||||
})
|
||||
|
||||
export const createSession = asyncHandler(async (req, res) => {
|
||||
const { questId } = matchedData(req)
|
||||
if (await isQuestUnlocked(req.user, questId)) {
|
||||
const session = await createNewSession(req.user, questId)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
})
|
||||
|
||||
export const getSessionById = asyncHandler(async (req, res) => {
|
||||
const { sessionId } = matchedData(req)
|
||||
const session = await Session.findOne({
|
||||
_id: sessionId,
|
||||
user: req.user._id,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'Session not found.' })
|
||||
return
|
||||
}
|
||||
|
||||
res.json(sessionDetail(session))
|
||||
})
|
||||
|
||||
export const getOrCreateSession = asyncHandler(async (req, res) => {
|
||||
const { questId } = matchedData(req)
|
||||
if ((await isQuestUnlocked(req.user, questId)) || config.testing.enabled) {
|
||||
const session = await getOrCreateActiveSession(req.user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,86 @@
|
||||
import { matchedData } from 'express-validator'
|
||||
import type { Express, Request, Response } from 'express'
|
||||
import { User, Session, ISession } from '@linux-odyssey/models'
|
||||
import {
|
||||
createNewSession,
|
||||
getOrCreateActiveSession,
|
||||
isQuestUnlocked,
|
||||
} from '../../models/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
import config from '../../config.js'
|
||||
|
||||
function sessionSummary(session: ISession) {
|
||||
return {
|
||||
_id: session._id,
|
||||
user: session.user,
|
||||
quest: session.quest,
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
}
|
||||
}
|
||||
|
||||
function sessionDetail(session: ISession) {
|
||||
return {
|
||||
...sessionSummary(session),
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
graph: session.graph,
|
||||
}
|
||||
}
|
||||
|
||||
export const getSessionList = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const user = req.user as Express.ExistingUser
|
||||
const { questId, status } = matchedData(req)
|
||||
const query = { user: user.id, quest: questId, status }
|
||||
const sessions = await Session.find(query)
|
||||
res.json(sessions.map(sessionSummary))
|
||||
}
|
||||
)
|
||||
|
||||
export const createSession = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { questId } = matchedData(req)
|
||||
const user = await User.findById((req.user as Express.ExistingUser).id)
|
||||
if (user && (await isQuestUnlocked(user, questId))) {
|
||||
const session = await createNewSession(user, questId)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const getSessionById = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { sessionId } = matchedData(req)
|
||||
const session = await Session.findOne({
|
||||
_id: sessionId,
|
||||
user: (req.user as Express.ExistingUser).id,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'Session not found.' })
|
||||
return
|
||||
}
|
||||
|
||||
res.json(sessionDetail(session))
|
||||
}
|
||||
)
|
||||
|
||||
export const getOrCreateSession = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { questId } = matchedData(req)
|
||||
const user = await User.findById((req.user as Express.ExistingUser).id)
|
||||
if (
|
||||
user &&
|
||||
((await isQuestUnlocked(user, questId)) || config.testing.enabled)
|
||||
) {
|
||||
const session = await getOrCreateActiveSession(user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -120,9 +120,9 @@ export async function isQuestUnlocked(
|
||||
user: HydratedDocument<IUser>,
|
||||
questId: string
|
||||
): Promise<boolean> {
|
||||
const userProfile = await UserProfile.findOne({ user: user._id })
|
||||
const userProfile = await UserProfile.findOne({ user: user.id })
|
||||
if (!userProfile) {
|
||||
throw new Error(`UserProfile ${user._id} not found`)
|
||||
throw new Error(`UserProfile ${user.id} not found`)
|
||||
}
|
||||
const quest = await Quest.findById(questId)
|
||||
if (!quest) {
|
||||
|
||||
Reference in new issue
Block a user