-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.ts
More file actions
235 lines (217 loc) · 7.12 KB
/
database.ts
File metadata and controls
235 lines (217 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { userToTeam, db, and, eq, log, team, teamJoinRequest } from "db";
import type { UserType, SiteRoleType } from "db/types";
import type { LoggingOptions, LoggingType, LoggingSource } from "../types";
import { type Context } from "hono";
import { isInDevMode } from ".";
/**
* Checks if a user has site admin privileges.
* @param permissionEnum - The user's site role
* @returns True if the user is an ADMIN or SUPER_ADMIN, false otherwise
*/
export function isSiteAdminUser(
permissionEnum: NonNullable<UserType>["siteRole"],
): boolean {
return ["ADMIN", "SUPER_ADMIN"].some((role) => role === permissionEnum);
}
/**
* Retrieves a team by its ID from the database.
* @param teamId - The unique identifier of the team
* @returns The team object if found, undefined otherwise
*/
export async function findTeam(teamId: string) {
return db.query.team.findFirst({
where: eq(team.id, teamId),
});
}
/**
* Removes a user from a team by deleting their userToTeam relationship.
* @param userId - The unique identifier of the user
* @param teamId - The unique identifier of the team
* @returns The teamId that was deleted
*/
export async function leaveTeam(userId: string, teamId: string) {
return db
.delete(userToTeam)
.where(
and(eq(userToTeam.userId, userId), eq(userToTeam.teamId, teamId)),
)
.returning({ teamId: userToTeam.teamId });
}
/**
* Retrieves the admin relationship between a user and a team.
* @param userId - The unique identifier of the user
* @param teamId - The unique identifier of the team
* @returns The userToTeam record if the user is an admin of the team, undefined otherwise
*/
export async function getAdminUserForTeam(userId: string, teamId: string) {
return db.query.userToTeam.findFirst({
where: and(
eq(userToTeam.userId, userId),
eq(userToTeam.teamId, teamId),
eq(userToTeam.role, "ADMIN"),
),
});
}
/**
* Retrieves a specific team join request by ID, user ID, and team ID.
* @param requestId - The unique identifier of the join request
* @param userId - The unique identifier of the user who made the request
* @param teamId - The unique identifier of the team
* @returns The join request record if found, undefined otherwise
*/
export async function getJoinTeamRequest(
requestId: string,
userId: string,
teamId: string,
) {
return db.query.teamJoinRequest.findFirst({
where: and(
eq(teamJoinRequest.id, requestId),
eq(teamJoinRequest.userId, userId),
eq(teamJoinRequest.teamId, teamId),
),
});
}
/**
* Retrieves a team join request by ID and team ID (admin view).
* Does not require user ID validation.
* @param requestId - The unique identifier of the join request
* @param teamId - The unique identifier of the team
* @returns The join request record if found, undefined otherwise
*/
export async function getJoinTeamRequestAdmin(
requestId: string,
teamId: string,
) {
return db.query.teamJoinRequest.findFirst({
where: and(
eq(teamJoinRequest.id, requestId),
eq(teamJoinRequest.teamId, teamId),
),
});
}
/**
* Checks if a user is a site admin OR if a provided query returns a truthy result.
* Useful for authorization checks that can shortcut if the user is already a site admin.
*
* @param userSiteRole - The site role of the user
* @param query - Either a Promise that resolves to a permission check result, or a function that returns such a Promise
* @returns True if the user is a site admin or if the query resolves to a truthy value, false otherwise
*/
export async function isUserSiteAdminOrQueryHasPermissions<T = unknown>(
userSiteRole: SiteRoleType,
// Accept either a Promise (already invoked query) or a function that returns a Promise
query: Promise<T> | (() => Promise<T>),
): Promise<boolean> {
if (isSiteAdminUser(userSiteRole)) {
return true;
}
const result = typeof query === "function" ? await query() : await query;
return !!result;
}
/**
* Logs an error message to the database with context information.
* @param message - The error message to log
* @param c - Optional Hono context for extracting request metadata
*/
export async function logError(message: string, c?: Context) {
const options = getAllContextValues(c);
const source = getLoggingSourceFromContext(c);
await logToDb("ERROR", message, source, options);
}
/**
* Logs an info message to the database with context information.
* @param message - The info message to log
* @param c - Optional Hono context for extracting request metadata
*/
export async function logInfo(message: string, c?: Context) {
const options = getAllContextValues(c);
const source = getLoggingSourceFromContext(c);
await logToDb("INFO", message, source, options);
}
/**
* Logs a warning message to the database with context information.
* @param message - The warning message to log
* @param c - Optional Hono context for extracting request metadata
*/
export async function logWarning(message: string, c?: Context) {
const options = getAllContextValues(c);
const source = getLoggingSourceFromContext(c);
await logToDb("WARNING", message, source, options);
}
/**
* Inserts a log record into the database. In development mode, logs to console instead.
* Silently fails if database insertion fails to prevent cascading errors.
* @param loggingType - The type of log (ERROR, INFO, WARNING, etc.)
* @param message - The log message
* @param options - Optional logging metadata (user ID, team ID, route, request ID)
*/
export async function logToDb(
logType: LoggingType,
message: string,
source: LoggingSource,
options?: LoggingOptions,
) {
if (isInDevMode()) {
console.log(
`[${logType}] from ${source} - ${message} - Options: `,
options,
);
return;
}
try {
await db.insert(log).values({
...options,
logType,
message,
source,
});
} catch (e) {
// Silently fail if logging to the db fails.
console.error("Failed to log to database: ", e);
}
}
/**
* Extracts relevant context values from a Hono request context for logging purposes.
* @param c - Optional Hono context
* @returns An object containing route, userId, teamId, and requestId, or undefined if no context provided
*/
function getAllContextValues(c?: Context): LoggingOptions | undefined {
if (!c) {
return undefined;
}
const user = c.get("user") as UserType;
return {
route: c.req.path,
userId: user?.id || null,
teamId: c.get("teamId"),
requestId: c.get("requestId"),
};
}
function getLoggingSourceFromContext(c?: Context): LoggingSource {
if (!c) {
return "SERVER";
}
return "SERVER";
}
/**
* Safely extract an error code string from an unknown thrown value from a db error.
* Returns the code as a string when present, otherwise null.
* @param e - The unknown error object thrown from a database operation
*/
export function maybeGetDbErrorCode(e: unknown): string | null {
if (e == null) return null;
if (typeof e === "object") {
const anyE = e as Record<string, unknown>;
const errorCauseKey = anyE["cause"];
if (errorCauseKey && typeof errorCauseKey === "object") {
const codeKey = (errorCauseKey as Record<string, unknown>)["code"];
if (typeof codeKey === "string") {
return codeKey;
} else if (typeof codeKey === "number") {
return codeKey.toString();
}
}
}
return null;
}