feat: user sessions history & fix ts <any>

This commit is contained in:
chreamy committed 2024-03-05 01:27:53 -06:00
1 parent 94ae2d7df4
commit 218a25daee
7 files changed
+95 -43

No files matched your search

+12 -15
View File
@@ -1,6 +1,11 @@
import { Command, Session } from '@linux-odyssey/models'
import Pagination from './pagination.ts'
import { Types } from 'mongoose'
import {
CommandObject,
SessionDetail,
SessionDocument,
SessionObject,
} from '../interface.ts'
function formatTime(time: number) {
const seconds = Math.floor(time / 1000)
@@ -9,7 +14,9 @@ function formatTime(time: number) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
}
export async function sessionList(pagination: Pagination): Promise<any[]> {
export async function sessionList(
pagination: Pagination
): Promise<SessionObject[]> {
const sessions = await Session.aggregate([
pagination.match('_id'),
{
@@ -57,26 +64,16 @@ export async function sessionList(pagination: Pagination): Promise<any[]> {
? formatTime(lastActivityAt.getTime() - createdAt.getTime())
: 'N/A', // Ensure both dates are present
commandCount: commands?.length || 0,
commands,
}
})
}
interface User {
username: string
}
interface SessionDocument {
_id: Types.ObjectId
user: User
quest: string
status: string
createdAt: Date
finishedAt: Date
}
export async function sessionDetail(id: string): Promise<any> {
export async function sessionDetail(id: string): Promise<SessionDetail> {
const session = (await Session.findById(id)
.populate('user')
.exec()) as unknown as SessionDocument
const commands = (await Command.find({ session: id })).map(
const commands: CommandObject[] = (await Command.find({ session: id })).map(
({ command, pwd, output, error, createdAt, stage }) => ({
command: command?.slice(0, 20),
pwd,
+9 -9
View File
@@ -1,11 +1,7 @@
import { Session, User } from '@linux-odyssey/models'
import Pagination from './pagination.ts'
import { ObjectId, Model } from 'mongoose'
interface IUser extends Document {
hashedPassword?: string
socialLogins?: Record<string, any>
}
import { Types } from 'mongoose'
import { IUser, UserSessionDetail } from '../interface.ts'
function loginMethods(user: IUser): string[] {
const methods: string[] = []
@@ -62,12 +58,15 @@ export async function userList(pagination: Pagination) {
}
})
}
export async function userDetail(id: ObjectId): Promise<Object> {
export async function userDetail(
id: Types.ObjectId
): Promise<UserSessionDetail[]> {
const sessions = await Session.find({
user: id,
})
return sessions.map((session) => {
const { _id, user, quest, status, createdAt, lastActivityAt } = session
const { _id, quest, status, createdAt, lastActivityAt } = session
return {
_id,
quest,
@@ -81,7 +80,8 @@ export async function userDetail(id: ObjectId): Promise<Object> {
}
})
}
export async function idToUser(id: ObjectId): Promise<String> {
export async function idToUser(id: Types.ObjectId): Promise<String> {
const user = await User.findOne({
_id: id,
})