mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: replace getOrCreateActive session to getActiveSession
This commit is contained in:
1 parent
7cdc542c19
commit
c64043316e
3 files changed
+10
-22
No files matched your search
@@ -3,7 +3,6 @@ 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'
|
||||
@@ -36,6 +35,7 @@ export const getSessionList = asyncHandler(
|
||||
const { questId, status } = matchedData(req)
|
||||
const query = { user: user.id, quest: questId, status }
|
||||
const sessions = await Session.find(query)
|
||||
console.log(query, sessions)
|
||||
res.json(sessions.map(sessionSummary))
|
||||
}
|
||||
)
|
||||
@@ -56,7 +56,6 @@ export const createSession = asyncHandler(
|
||||
export const getSessionById = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { sessionId } = matchedData(req)
|
||||
console.log(sessionId)
|
||||
const session = await Session.findOne({
|
||||
_id: sessionId,
|
||||
user: (req.user as Express.ExistingUser).id,
|
||||
@@ -71,7 +70,7 @@ export const getSessionById = asyncHandler(
|
||||
}
|
||||
)
|
||||
|
||||
export const getOrCreateSession = asyncHandler(
|
||||
export const getActiveSessionControler = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { questId } = matchedData(req)
|
||||
const user = await User.findById((req.user as Express.ExistingUser).id)
|
||||
@@ -79,8 +78,12 @@ export const getOrCreateSession = asyncHandler(
|
||||
user &&
|
||||
((await isQuestUnlocked(user, questId)) || config.testing.enabled)
|
||||
) {
|
||||
const session = await getOrCreateActiveSession(user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
const session = await Session.findOne({
|
||||
user: user.id,
|
||||
quest: questId,
|
||||
status: 'active',
|
||||
})
|
||||
res.json(session ? sessionDetail(session) : null)
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ router.post(
|
||||
sessionRateLimit,
|
||||
sessionController.createSession
|
||||
)
|
||||
router.post(
|
||||
router.get(
|
||||
'/active',
|
||||
activeSessionValidator,
|
||||
sessionController.getOrCreateSession
|
||||
sessionController.getActiveSessionControler
|
||||
)
|
||||
router.get(
|
||||
'/:sessionId',
|
||||
|
||||
@@ -79,21 +79,6 @@ export async function createNewSession(
|
||||
return session
|
||||
}
|
||||
|
||||
export async function getOrCreateActiveSession(
|
||||
user: HydratedDocument<IUser>,
|
||||
questId: string
|
||||
): Promise<ISession> {
|
||||
const session = await Session.findOne({
|
||||
user,
|
||||
quest: questId,
|
||||
status: 'active',
|
||||
})
|
||||
if (session) {
|
||||
return session
|
||||
}
|
||||
return createNewSession(user, questId)
|
||||
}
|
||||
|
||||
export async function finishSession(
|
||||
session: HydratedDocument<ISession>
|
||||
): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user