Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -6,6 +6,7 @@ import { db } from "../db.server";
import RowFiltersHelper from "../helpers/RowFiltersHelper";
import { getUserInfo } from "../session.server";
import { UserWithoutPassword } from "./users.db.server";
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
export type LogWithDetails = Log & {
user: UserWithoutPassword | null;
@@ -13,6 +14,8 @@ export type LogWithDetails = Log & {
tenant?: Tenant | null;
};
export type AdminUserLastActivity = Pick<Log, "id" | "userId" | "action" | "createdAt">;
const include = {
user: true,
tenant: true,
@@ -74,75 +77,87 @@ export async function getLogs(
export async function createLog(request: Request, tenantId: string | null, action: string, details: string) {
const userInfo = await getUserInfo(request);
await db.log.create({
data: {
tenantId,
userId: userInfo.userId,
url: new URL(request.url).pathname,
action,
details,
},
});
await withLogRlsContext(userInfo.userId, tenantId, () =>
db.log.create({
data: {
tenantId,
userId: userInfo.userId,
url: new URL(request.url).pathname,
action,
details,
},
}),
);
}
export async function createManualLog(userId: string, tenantId: string | null, pathname: string, action: string, details: string) {
await db.log.create({
data: {
tenantId,
userId,
url: pathname,
action,
details,
},
});
await withLogRlsContext(userId, tenantId, () =>
db.log.create({
data: {
tenantId,
userId,
url: pathname,
action,
details,
},
}),
);
}
export async function createLogLogin(request: Request, user: UserWithoutPassword) {
// eslint-disable-next-line no-console
// console.log({ clientIpAddress: getClientIPAddress(request.headers) });
await db.log.create({
data: {
userId: user.id,
url: "",
action: DefaultLogActions.Login,
details: "",
},
});
await withLogRlsContext(user.id, null, () =>
db.log.create({
data: {
userId: user.id,
url: "",
action: DefaultLogActions.Login,
details: "",
},
}),
);
}
export async function createLogLogout(request: Request, userId: string) {
await db.log.create({
data: {
userId,
url: "",
action: DefaultLogActions.Logout,
details: "",
},
});
await withLogRlsContext(userId, null, () =>
db.log.create({
data: {
userId,
url: "",
action: DefaultLogActions.Logout,
details: "",
},
}),
);
}
export async function createAdminLog(request: Request, action: string, details: string) {
const userInfo = await getUserInfo(request);
await db.log.create({
data: {
userId: userInfo.userId,
url: new URL(request.url).pathname,
action,
details,
},
});
await withLogRlsContext(userInfo.userId, null, () =>
db.log.create({
data: {
userId: userInfo.userId,
url: new URL(request.url).pathname,
action,
details,
},
}),
);
}
export async function getLastUserLog(userId: string): Promise<Log | null> {
return await db.log.findFirst({
where: {
userId,
},
orderBy: {
createdAt: "desc",
},
});
return await withLogRlsContext(userId, null, () =>
db.log.findFirst({
where: {
userId,
},
orderBy: {
createdAt: "desc",
},
}),
);
}
/**
@@ -150,7 +165,7 @@ export async function getLastUserLog(userId: string): Promise<Log | null> {
* constant number of queries (1 groupBy + 1 findMany), avoiding the N+1
* pattern of calling `getLastUserLog` per user.
*/
export async function getLastUserLogsByUserIds(userIds: string[]): Promise<Map<string, Log>> {
export async function getLastUserLogsByUserIds(userIds: string[]): Promise<Map<string, AdminUserLastActivity>> {
if (userIds.length === 0) {
return new Map();
}
@@ -167,8 +182,9 @@ export async function getLastUserLogsByUserIds(userIds: string[]): Promise<Map<s
}
const logs = await db.log.findMany({
where: { OR: filters },
select: { id: true, userId: true, action: true, createdAt: true },
});
const byUserId = new Map<string, Log>();
const byUserId = new Map<string, AdminUserLastActivity>();
for (const log of logs) {
const existing = byUserId.get(log.userId ?? "");
if (!log.userId) continue;
@@ -178,3 +194,17 @@ export async function getLastUserLogsByUserIds(userIds: string[]): Promise<Map<s
}
return byUserId;
}
function withLogRlsContext<T>(userId: string | undefined, tenantId: string | null, operation: () => T): T {
const current = getRlsExecutionContext();
if (current?.userId || current?.tenantId) {
return operation();
}
return runWithRlsContext(
{
...(userId ? { userId } : {}),
...(tenantId ? { tenantId } : {}),
},
operation,
);
}