feat: replace getOrCreateActive session to getActiveSession

This commit is contained in:
wancat committed 2024-06-27 11:54:28 +08:00
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 { User, Session, ISession } from '@linux-odyssey/models'
import { import {
createNewSession, createNewSession,
getOrCreateActiveSession,
isQuestUnlocked, isQuestUnlocked,
} from '../../models/sessionManager.js' } from '../../models/sessionManager.js'
import { asyncHandler } from '../../middleware/error.js' import { asyncHandler } from '../../middleware/error.js'
@@ -36,6 +35,7 @@ export const getSessionList = asyncHandler(
const { questId, status } = matchedData(req) const { questId, status } = matchedData(req)
const query = { user: user.id, quest: questId, status } const query = { user: user.id, quest: questId, status }
const sessions = await Session.find(query) const sessions = await Session.find(query)
console.log(query, sessions)
res.json(sessions.map(sessionSummary)) res.json(sessions.map(sessionSummary))
} }
) )
@@ -56,7 +56,6 @@ export const createSession = asyncHandler(
export const getSessionById = asyncHandler( export const getSessionById = asyncHandler(
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { sessionId } = matchedData(req) const { sessionId } = matchedData(req)
console.log(sessionId)
const session = await Session.findOne({ const session = await Session.findOne({
_id: sessionId, _id: sessionId,
user: (req.user as Express.ExistingUser).id, 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) => { async (req: Request, res: Response) => {
const { questId } = matchedData(req) const { questId } = matchedData(req)
const user = await User.findById((req.user as Express.ExistingUser).id) const user = await User.findById((req.user as Express.ExistingUser).id)
@@ -79,8 +78,12 @@ export const getOrCreateSession = asyncHandler(
user && user &&
((await isQuestUnlocked(user, questId)) || config.testing.enabled) ((await isQuestUnlocked(user, questId)) || config.testing.enabled)
) { ) {
const session = await getOrCreateActiveSession(user, questId) const session = await Session.findOne({
res.json(sessionDetail(session)) user: user.id,
quest: questId,
status: 'active',
})
res.json(session ? sessionDetail(session) : null)
} else { } else {
res.status(403).json({ message: 'Quest is locked.' }) res.status(403).json({ message: 'Quest is locked.' })
} }
+2 -2
View File
@@ -17,10 +17,10 @@ router.post(
sessionRateLimit, sessionRateLimit,
sessionController.createSession sessionController.createSession
) )
router.post( router.get(
'/active', '/active',
activeSessionValidator, activeSessionValidator,
sessionController.getOrCreateSession sessionController.getActiveSessionControler
) )
router.get( router.get(
'/:sessionId', '/:sessionId',
-15
View File
@@ -79,21 +79,6 @@ export async function createNewSession(
return session 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( export async function finishSession(
session: HydratedDocument<ISession> session: HydratedDocument<ISession>
): Promise<void> { ): Promise<void> {