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