mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add UserProfile collection, create on login
* add progress
This commit is contained in:
1 parent
e981bff3bb
commit
45f12acbd9
5 files changed
+75
-22
No files matched your search
@@ -4,6 +4,7 @@ export { default as User } from './user.js'
|
|||||||
export { default as Quest } from './quest.js'
|
export { default as Quest } from './quest.js'
|
||||||
export { default as Session } from './session.js'
|
export { default as Session } from './session.js'
|
||||||
export { default as Command } from './command.js'
|
export { default as Command } from './command.js'
|
||||||
|
export { default as UserProfile } from './userProfile.js'
|
||||||
|
|
||||||
export default async function connectDB(uri) {
|
export default async function connectDB(uri) {
|
||||||
const db = await mongoose.connect(uri, {
|
const db = await mongoose.connect(uri, {
|
||||||
|
|||||||
@@ -1,24 +1,5 @@
|
|||||||
import { model, Schema } from 'mongoose'
|
import { model, Schema } from 'mongoose'
|
||||||
|
|
||||||
const progressSchema = new Schema({
|
|
||||||
quest: {
|
|
||||||
type: String,
|
|
||||||
ref: 'Quest',
|
|
||||||
},
|
|
||||||
sessions: [
|
|
||||||
{
|
|
||||||
type: Schema.ObjectId,
|
|
||||||
ref: 'Session',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
completionStatus: {
|
|
||||||
type: String,
|
|
||||||
enum: ['incomplete', 'completed'],
|
|
||||||
},
|
|
||||||
startedAt: Date,
|
|
||||||
finishedAt: Date,
|
|
||||||
})
|
|
||||||
|
|
||||||
const User = model(
|
const User = model(
|
||||||
'User',
|
'User',
|
||||||
new Schema(
|
new Schema(
|
||||||
@@ -42,7 +23,6 @@ const User = model(
|
|||||||
id: String,
|
id: String,
|
||||||
displayName: String,
|
displayName: String,
|
||||||
},
|
},
|
||||||
progress: [progressSchema],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { model, Schema } from 'mongoose'
|
||||||
|
|
||||||
|
const progressSchema = new Schema({
|
||||||
|
quest: {
|
||||||
|
type: String,
|
||||||
|
ref: 'Quest',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
sessions: [
|
||||||
|
{
|
||||||
|
type: Schema.ObjectId,
|
||||||
|
ref: 'Session',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
completed: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
startedAt: {
|
||||||
|
type: Date,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
finishedAt: Date,
|
||||||
|
})
|
||||||
|
|
||||||
|
const UserProfile = model(
|
||||||
|
'UserProfile',
|
||||||
|
new Schema(
|
||||||
|
{
|
||||||
|
user: {
|
||||||
|
type: Schema.ObjectId,
|
||||||
|
ref: 'User',
|
||||||
|
unique: true,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
progress: {
|
||||||
|
type: Map,
|
||||||
|
of: progressSchema,
|
||||||
|
default: {},
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
export default UserProfile
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import passport from 'passport'
|
import passport from 'passport'
|
||||||
|
import { UserProfile } from '@linux-odyssey/models'
|
||||||
import passwordStrategy from './passwordStrategy.js'
|
import passwordStrategy from './passwordStrategy.js'
|
||||||
import jwtStrategy from './jwtStrategy.js'
|
import jwtStrategy from './jwtStrategy.js'
|
||||||
import googleStrategy from './googleStrategy.js'
|
import googleStrategy from './googleStrategy.js'
|
||||||
@@ -24,7 +25,10 @@ if (enabledMethods.github) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
passport.serializeUser((user, done) => {
|
passport.serializeUser((user, done) => {
|
||||||
process.nextTick(() => {
|
process.nextTick(async () => {
|
||||||
|
if (!(await UserProfile.exists({ user: user._id }))) {
|
||||||
|
UserProfile.create({ user: user._id })
|
||||||
|
}
|
||||||
done(null, {
|
done(null, {
|
||||||
_id: user._id,
|
_id: user._id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
|
|||||||
@@ -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 { createContainer, deleteContainer } from '../containers/docker.js'
|
||||||
import SessionHandler from './sessionHandler.js'
|
import SessionHandler from './sessionHandler.js'
|
||||||
|
|
||||||
@@ -48,6 +48,23 @@ export async function createNewSession(user, questId) {
|
|||||||
|
|
||||||
const session = sessionHandler.getSession()
|
const session = sessionHandler.getSession()
|
||||||
await session.save()
|
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
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user