tangled
alpha
login
or
join now
dunkirk.sh
/
smokie
1
fork
atom
a fun bot for the hc slack
1
fork
atom
overview
issues
pulls
pipelines
feat: add userName to projects too
dunkirk.sh
10 months ago
bc6daa01
9631f6b2
verified
This commit was signed with the committer's
known signature
.
dunkirk.sh
SSH Key Fingerprint:
SHA256:DqcG0RXYExE26KiWo3VxJnsxswN1QNfTBvB+bdSpk80=
+72
-1
1 changed file
expand all
collapse all
unified
split
src
features
api
routes
projects.ts
+72
-1
src/features/api/routes/projects.ts
···
2
2
import { users as usersTable } from "../../../libs/schema";
3
3
import { handleApiError } from "../../../libs/apiError";
4
4
import { eq } from "drizzle-orm";
5
5
+
import { fetchUserData } from "../../../libs/cachet";
5
6
6
7
export type Project = {
7
8
projectName: string;
···
10
11
/** Total time spent on takes, in seconds */
11
12
totalTakesTime: number;
12
13
userId: string;
14
14
+
userName?: string;
13
15
};
14
16
17
17
+
// Cache for user data from cachet
18
18
+
const userCache: Record<string, { name: string; timestamp: number }> = {};
19
19
+
const pendingRequests: Record<string, Promise<string>> = {};
20
20
+
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
21
21
+
22
22
+
// Function to get user name from cache or fetch it
23
23
+
async function getUserName(userId: string): Promise<string> {
24
24
+
const now = Date.now();
25
25
+
26
26
+
// Check if user data is in cache and still valid
27
27
+
if (userCache[userId] && now - userCache[userId].timestamp < CACHE_TTL) {
28
28
+
return userCache[userId].name;
29
29
+
}
30
30
+
31
31
+
// If there's already a pending request for this user, return that promise
32
32
+
// instead of creating a new request
33
33
+
if (pendingRequests[userId]) {
34
34
+
return pendingRequests[userId];
35
35
+
}
36
36
+
37
37
+
// Create a new promise for this user and store it
38
38
+
const fetchPromise = (async () => {
39
39
+
try {
40
40
+
const userData = await fetchUserData(userId);
41
41
+
const userName = userData?.displayName || "Unknown User";
42
42
+
43
43
+
userCache[userId] = {
44
44
+
name: userName,
45
45
+
timestamp: now,
46
46
+
};
47
47
+
48
48
+
return userName;
49
49
+
} catch (error) {
50
50
+
console.error("Error fetching user data:", error);
51
51
+
return "Unknown User";
52
52
+
} finally {
53
53
+
// Clean up the pending request when done
54
54
+
delete pendingRequests[userId];
55
55
+
}
56
56
+
})();
57
57
+
58
58
+
// Store the promise
59
59
+
pendingRequests[userId] = fetchPromise;
60
60
+
61
61
+
// Return the promise
62
62
+
return fetchPromise;
63
63
+
}
64
64
+
15
65
export async function projects(url: URL): Promise<Response> {
16
66
const user = url.searchParams.get("user");
17
67
try {
···
39
89
);
40
90
}
41
91
92
92
+
// Get unique user IDs
93
93
+
const userIds = [...new Set(projects.map((project) => project.userId))];
94
94
+
95
95
+
// Fetch all user names from cache or API
96
96
+
const userNamesPromises = userIds.map((id) => getUserName(id));
97
97
+
const userNames = await Promise.all(userNamesPromises);
98
98
+
99
99
+
// Create a map of user names
100
100
+
const userNameMap: Record<string, string> = {};
101
101
+
userIds.forEach((id, index) => {
102
102
+
userNameMap[id] = userNames[index] || "Unknown User";
103
103
+
});
104
104
+
105
105
+
// Add user names to projects
106
106
+
const projectsWithUserNames = projects.map((project) => ({
107
107
+
...project,
108
108
+
userName: userNameMap[project.userId] || "Unknown User",
109
109
+
}));
110
110
+
42
111
return new Response(
43
112
JSON.stringify({
44
44
-
projects: user ? projects[0] : projects,
113
113
+
projects: user
114
114
+
? projectsWithUserNames[0]
115
115
+
: projectsWithUserNames,
45
116
}),
46
117
{
47
118
headers: {