Fix saving session finished time, display session locale (#213)

- Show session locale
- Remove command counts on session list for performance
This commit is contained in:
Justin Lin authored and GitHub committed 2026-10-10 00:19:10 +11:00
commit 1f8d4162f8
5 files changed
+33 -28

No files matched your search

+1
View File
@@ -97,6 +97,7 @@ export async function finishSession(
session: HydratedDocument<ISession>
): Promise<void> {
session.status = 'finished'
session.finishedAt = new Date()
await session.save()
const userProfile = await UserProfile.findOne({ user: session.user })
+6 -10
View File
@@ -1,6 +1,7 @@
import { Types } from 'mongoose'
export interface User {
_id: string
username: string
}
export interface SessionDocument {
@@ -8,8 +9,10 @@ export interface SessionDocument {
user: User
quest: string
status: string
locale: string
createdAt: Date
finishedAt: Date
lastActivityAt: Date
}
export interface CommandObject {
command: string
@@ -21,24 +24,17 @@ export interface CommandObject {
}
export interface SessionObject {
_id: Types.ObjectId
user: string
user: User
quest: string
locale: string
status: string
createdAt: string
finishedAt: string
lastActivityAt: string
usedTime: string
commandCount: number
commands: CommandObject[]
}
export interface SessionDetail {
_id: Types.ObjectId
user: string
quest: string
status: string
createdAt: string
finishedAt: string
usedTime: string
export interface SessionDetail extends SessionObject {
commands: CommandObject[]
}
export interface IUser extends Document {
+18 -14
View File
@@ -28,14 +28,6 @@ export async function sessionList(
},
},
{ $unwind: '$user' },
{
$lookup: {
from: 'commands',
localField: '_id',
foreignField: 'session',
as: 'commands',
},
},
pagination.sort('_id'),
pagination.limit(),
])
@@ -45,16 +37,17 @@ export async function sessionList(
_id,
user,
quest,
locale,
status,
createdAt,
finishedAt,
commands,
lastActivityAt,
} = session
return {
_id,
user: user.username, // Assuming `user` is always populated.
user: { _id: user._id, username: user.username },
quest,
locale: locale ?? 'zh-TW',
status,
createdAt: createdAt?.toLocaleString(),
finishedAt: finishedAt?.toLocaleString(),
@@ -63,8 +56,7 @@ export async function sessionList(
lastActivityAt && createdAt
? formatTime(lastActivityAt.getTime() - createdAt.getTime())
: 'N/A', // Ensure both dates are present
commandCount: commands?.length || 0,
commands,
commandCount: 0,
}
})
}
@@ -83,14 +75,26 @@ export async function sessionDetail(id: string): Promise<SessionDetail> {
createdAt: createdAt?.toLocaleString(),
})
)
const { _id, user, quest, status, createdAt, finishedAt } = session
const {
_id,
user,
quest,
locale,
status,
createdAt,
finishedAt,
lastActivityAt,
} = session
return {
_id,
user: user.username,
user: { _id: user._id, username: user.username },
quest,
locale: locale ?? 'zh-TW',
status,
createdAt: createdAt?.toLocaleString(),
finishedAt: finishedAt?.toLocaleString(),
lastActivityAt: lastActivityAt?.toLocaleDateString(),
commandCount: commands.length,
usedTime: finishedAt
? formatTime(finishedAt.getTime() - createdAt.getTime())
: '',
@@ -6,9 +6,13 @@
<th>Quest</th>
<td>{{quest}}</td>
</tr>
<tr>
<th>Locale</th>
<td>{{locale}}</td>
</tr>
<tr>
<th>User</th>
<td>{{user}}</td>
<td><a href="/users/{{user._id}}">{{user.username}}</a></td>
</tr>
<tr>
<th>Status</th>
+3 -3
View File
@@ -12,22 +12,22 @@
<tr>
<th>User</th>
<th>Quest</th>
<th>Locale</th>
<th>Status</th>
<th>Created At</th>
<th>Finished At</th>
<th>Used Time</th>
<th>Command Count</th>
<th>Session</th>
</tr>
{{#each sessions}}
<tr>
<td>{{user}}</td>
<td><a href="/users/{{user._id}}">{{user.username}}</a></td>
<td>{{quest}}</td>
<td>{{locale}}</td>
<td>{{status}}</td>
<td>{{createdAt}}</td>
<td>{{finishedAt}}</td>
<td>{{usedTime}}</td>
<td>{{commandCount}}</td>
<td><a href='/sessions/{{_id}}'>Detail</a></td>
</tr>
{{/each}}