feat: add UserProfile collection, create on login

* add progress
This commit is contained in:
wancat authored and Lin, Hung-Shin committed 2023-11-14 10:44:52 +08:00
1 parent e981bff3bb
commit 45f12acbd9
5 files changed
+75 -22

No files matched your search

+5 -1
View File
@@ -1,4 +1,5 @@
import passport from 'passport'
import { UserProfile } from '@linux-odyssey/models'
import passwordStrategy from './passwordStrategy.js'
import jwtStrategy from './jwtStrategy.js'
import googleStrategy from './googleStrategy.js'
@@ -24,7 +25,10 @@ if (enabledMethods.github) {
}
passport.serializeUser((user, done) => {
process.nextTick(() => {
process.nextTick(async () => {
if (!(await UserProfile.exists({ user: user._id }))) {
UserProfile.create({ user: user._id })
}
done(null, {
_id: user._id,
username: user.username,
+18 -1
View File
@@ -1,4 +1,4 @@
import { Session, Quest } from '@linux-odyssey/models'
import { Session, Quest, UserProfile } from '@linux-odyssey/models'
import { createContainer, deleteContainer } from '../containers/docker.js'
import SessionHandler from './sessionHandler.js'
@@ -48,6 +48,23 @@ export async function createNewSession(user, questId) {
const session = sessionHandler.getSession()
await session.save()
const userProfile = await UserProfile.findOne({ user: user._id })
if (!userProfile) {
throw new Error(`UserProfile ${user._id} not found`)
}
const progress = userProfile.progress.get(quest._id)
if (!progress) {
userProfile.progress.set(quest._id, {
quest: quest._id,
sessions: [session._id],
startedAt: new Date(),
})
} else {
progress.sessions.push(session._id)
}
await userProfile.save()
return session
}