Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/components/Firebase/Firebase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4738,18 +4738,18 @@ class Firebase {
* @returns {Array} Array of user objects with id, name, email, role, status, and lastLogin
*/
/**
* Get last action date for all users by querying activity collections.
* Get last action date and type for all users by querying activity collections.
* Uses optimized approach: 5 total queries instead of per-user queries.
* @returns Map<userId, Date> - Last action date per user
* @returns Map<userId, {date: Date, type: string}> - Last action info per user
*/
getUsersLastAction = async (): Promise<Map<string, Date>> => {
const lastActionMap = new Map<string, Date>()
getUsersLastAction = async (): Promise<Map<string, { date: Date; type: string }>> => {
const lastActionMap = new Map<string, { date: Date; type: string }>()

const updateIfNewer = (userId: string, date: Date | null) => {
const updateIfNewer = (userId: string, date: Date | null, type: string) => {
if (!date || !userId) return
const current = lastActionMap.get(userId)
if (!current || date > current) {
lastActionMap.set(userId, date)
if (!current || date > current.date) {
lastActionMap.set(userId, { date, type })
}
}

Expand All @@ -4773,15 +4773,15 @@ class Firebase {
const data = doc.data()
const userId = extractUserId(data.teacher)
const endDate = data.end?.toDate?.() || null
updateIfNewer(userId, endDate)
updateIfNewer(userId, endDate, 'Observation')
})

// 2. Knowledge Checks (6K+)
knowledgeChecks.docs.forEach(doc => {
const data = doc.data()
const userId = data.answeredBy
const timestamp = data.timestamp?.toDate?.() || null
updateIfNewer(userId, timestamp)
updateIfNewer(userId, timestamp, 'Training')
})

// 3. Conference Plans
Expand All @@ -4790,8 +4790,8 @@ class Firebase {
const userId = data.teacher
const created = data.dateCreated?.toDate?.() || null
const modified = data.dateModified?.toDate?.() || null
updateIfNewer(userId, created)
updateIfNewer(userId, modified)
updateIfNewer(userId, created, 'Conference Plan')
updateIfNewer(userId, modified, 'Conference Plan')
})

// 4. Action Plans
Expand All @@ -4800,8 +4800,8 @@ class Firebase {
const userId = data.teacher
const created = data.dateCreated?.toDate?.() || null
const modified = data.dateModified?.toDate?.() || null
updateIfNewer(userId, created)
updateIfNewer(userId, modified)
updateIfNewer(userId, created, 'Action Plan')
updateIfNewer(userId, modified, 'Action Plan')
})

// 5. Emails (check both sender and recipient)
Expand All @@ -4811,10 +4811,10 @@ class Firebase {
const recipientId = data.recipientId
const created = data.dateCreated?.toDate?.() || null
const modified = data.dateModified?.toDate?.() || null
updateIfNewer(senderId, created)
updateIfNewer(senderId, modified)
updateIfNewer(recipientId, created)
updateIfNewer(recipientId, modified)
updateIfNewer(senderId, created, 'Email')
updateIfNewer(senderId, modified, 'Email')
updateIfNewer(recipientId, created, 'Email')
updateIfNewer(recipientId, modified, 'Email')
})

return lastActionMap
Expand All @@ -4831,6 +4831,7 @@ class Firebase {
archived: boolean
lastLogin: Date | null
lastAction: Date | null
lastActionType: string
}> = []

// Fetch programs, users, and last action data in parallel
Expand Down Expand Up @@ -4879,6 +4880,7 @@ class Firebase {
}
}
}
const lastActionData = lastActionMap.get(doc.id)
result.push({
id: doc.id,
firstName: data.firstName || '',
Expand All @@ -4888,7 +4890,8 @@ class Firebase {
program: programName,
archived: data.archived || false,
lastLogin: data.lastLogin ? data.lastLogin.toDate() : null,
lastAction: lastActionMap.get(doc.id) || null,
lastAction: lastActionData?.date || null,
lastActionType: lastActionData?.type || '',
})
})

Expand Down
13 changes: 10 additions & 3 deletions src/components/UsersComponents/AllUsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,23 @@ class AllUsersTable extends React.Component<Props, State> {

formatDate = (d: Date | null) => d ? d.toLocaleString([], { dateStyle: 'short', timeStyle: 'short' }) : 'Never'

formatLastAction = (user: Types.User) => {
if (!user.lastAction) return 'Never'
const date = user.lastAction.toLocaleString([], { dateStyle: 'short', timeStyle: 'short' })
return user.lastActionType ? `${date} (${user.lastActionType})` : date
}

handleExport = () => {
const users = this.getFilteredUsers()
const headers = ['Last Name', 'First Name', 'Email', 'Role', 'Program', 'Status', 'Last Login', 'Last Action']
const headers = ['Last Name', 'First Name', 'Email', 'Role', 'Program', 'Status', 'Last Login', 'Last Action', 'Action Type']
const escape = (val: string) => `"${(val || '').replace(/"/g, '""')}"`
const rows = users.map(u => [
escape(u.lastName), escape(u.firstName), escape(u.email),
escape(this.formatRole(u.role)), escape(u.program || ''),
escape(u.archived ? 'Archived' : 'Active'),
escape(this.formatDate(u.lastLogin)),
escape(this.formatDate(u.lastAction))
escape(this.formatDate(u.lastAction)),
escape(u.lastActionType || '')
].join(','))
const csv = [headers.join(','), ...rows].join('\n')
const blob = new Blob(['\ufeff' + csv], { type: 'text/csv;charset=utf-8;' })
Expand Down Expand Up @@ -207,7 +214,7 @@ class AllUsersTable extends React.Component<Props, State> {
</Tooltip>
</TableCell>
<TableCell>{this.formatDate(user.lastLogin)}</TableCell>
<TableCell>{this.formatDate(user.lastAction)}</TableCell>
<TableCell>{this.formatLastAction(user)}</TableCell>
<TableCell onClick={e => e.stopPropagation()} style={{ textAlign: 'center' }}>
<Tooltip title="Edit user">
<IconButton size="small" onClick={() => this.props.onUserClick?.(user)}>
Expand Down
1 change: 1 addition & 0 deletions src/constants/Types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export interface User {
}>,
lastLogin?: Date,
lastAction?: Date,
lastActionType?: string,
email?: string,
school?: string,
program?: string,
Expand Down
2 changes: 1 addition & 1 deletion src/views/protected/AdminViews/AllUsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AllUsersPage extends React.Component<Props, State> {
handleUserClick = (user: Types.User) => {
this.setState({
selected: user, firstName: user.firstName, lastName: user.lastName,
email: user.email, editOpen: true,
email: user.email || '', editOpen: true,
})
}

Expand Down
Loading