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

+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
}