mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Feat: User Session History
This commit is contained in:
1 parent
dc972173a4
commit
a71411d5bf
9 files changed
+92
-19
No files matched your search
@@ -1,13 +1,27 @@
|
||||
import { Session, User } from '@linux-odyssey/models'
|
||||
import Pagination from './pagination.ts'
|
||||
import { ObjectId, Model } from 'mongoose'
|
||||
|
||||
function loginMethods(user: any) {
|
||||
const methods = []
|
||||
interface IUser extends Document {
|
||||
hashedPassword?: string
|
||||
socialLogins?: Record<string, any>
|
||||
}
|
||||
|
||||
function loginMethods(user: IUser): string[] {
|
||||
const methods: string[] = []
|
||||
if (user.hashedPassword) methods.push('password')
|
||||
if (user.socialLogins) methods.push(...Object.keys(user.socialLogins))
|
||||
if (user.socialLogins && typeof user.socialLogins === 'object') {
|
||||
methods.push(...Object.keys(user.socialLogins))
|
||||
}
|
||||
return methods
|
||||
}
|
||||
|
||||
function formatTime(time: number) {
|
||||
const seconds = Math.floor(time / 1000)
|
||||
const minutes = Math.floor(seconds / 60)
|
||||
const hours = Math.floor(minutes / 60)
|
||||
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
|
||||
}
|
||||
export function userCount() {
|
||||
return User.countDocuments({})
|
||||
}
|
||||
@@ -48,3 +62,28 @@ export async function userList(pagination: Pagination) {
|
||||
}
|
||||
})
|
||||
}
|
||||
export async function userDetail(id: ObjectId): Promise<Object> {
|
||||
const sessions = await Session.find({
|
||||
user: id,
|
||||
})
|
||||
return sessions.map((session) => {
|
||||
const { _id, user, quest, status, createdAt, lastActivityAt } = session
|
||||
return {
|
||||
_id,
|
||||
quest,
|
||||
status,
|
||||
createdAt: createdAt?.toLocaleString(),
|
||||
lastActivityAt: lastActivityAt?.toLocaleString(),
|
||||
usedTime:
|
||||
lastActivityAt && createdAt
|
||||
? formatTime(lastActivityAt.getTime() - createdAt.getTime())
|
||||
: 'N/A', // Ensure both dates are present
|
||||
}
|
||||
})
|
||||
}
|
||||
export async function idToUser(id: ObjectId): Promise<String> {
|
||||
const user = await User.findOne({
|
||||
_id: id,
|
||||
})
|
||||
return user?.username || 'User not found'
|
||||
}
|
||||
Reference in new issue
Block a user