133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
import type { Prisma, PrismaClient } from '@prisma/client';
|
|
import { logger } from '../lib/logger.js';
|
|
|
|
export interface ActivityLogInput {
|
|
actorId: string;
|
|
action: string;
|
|
entityType: string;
|
|
entityId?: string | null;
|
|
details?: Record<string, unknown> | null;
|
|
}
|
|
|
|
/**
|
|
* Best-effort audit write — a failure must never break the main request.
|
|
* Errors are reported to the app logger only.
|
|
* `details` always includes the actor's ID (merged under `actorId`).
|
|
*/
|
|
export async function logActivity(prisma: PrismaClient, input: ActivityLogInput) {
|
|
try {
|
|
await prisma.activityLog.create({
|
|
data: {
|
|
actorId: input.actorId,
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId ?? null,
|
|
details: { actorId: input.actorId, ...(input.details ?? {}) } as Prisma.InputJsonValue,
|
|
},
|
|
});
|
|
} catch (e) {
|
|
logger.error(
|
|
{ err: e, action: input.action, actorId: input.actorId },
|
|
'Failed to write activity log',
|
|
);
|
|
}
|
|
}
|
|
|
|
export interface ActivityLogFilters {
|
|
action?: string;
|
|
userId?: string;
|
|
/** Historia operacji WYKONANYCH NA użytkowniku: logi o bycie `user` z jego ID jako entityId. */
|
|
entityUserId?: string;
|
|
/** Inclusive ISO date (YYYY-MM-DD). */
|
|
from?: string;
|
|
/** Inclusive ISO date (YYYY-MM-DD). */
|
|
to?: string;
|
|
limit: number;
|
|
offset: number;
|
|
}
|
|
|
|
export interface ActivityLogActor {
|
|
id: string;
|
|
email: string;
|
|
firstName: string | null;
|
|
lastName: string | null;
|
|
}
|
|
|
|
export interface ActivityLogEntry {
|
|
id: string;
|
|
action: string;
|
|
entityType: string;
|
|
entityId: string | null;
|
|
details: Record<string, unknown> | null;
|
|
createdAt: Date;
|
|
actor: ActivityLogActor;
|
|
}
|
|
|
|
async function fetchActivityLogs(
|
|
prisma: PrismaClient,
|
|
where: Prisma.ActivityLogWhereInput,
|
|
limit: number,
|
|
offset: number,
|
|
) {
|
|
const [logs, total] = await Promise.all([
|
|
prisma.activityLog.findMany({
|
|
where,
|
|
include: {
|
|
actor: { select: { id: true, email: true, firstName: true, lastName: true } },
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
take: limit,
|
|
skip: offset,
|
|
}),
|
|
prisma.activityLog.count({ where }),
|
|
]);
|
|
return { logs, total };
|
|
}
|
|
|
|
export async function listActivityLogs(prisma: PrismaClient, filters: ActivityLogFilters) {
|
|
const where: Prisma.ActivityLogWhereInput = {
|
|
...(filters.action ? { action: filters.action } : {}),
|
|
...(filters.userId ? { actorId: filters.userId } : {}),
|
|
...(filters.entityUserId ? { entityType: 'user', entityId: filters.entityUserId } : {}),
|
|
// Granice dat w strefie serwera (jak reszta systemu), nie w UTC.
|
|
createdAt:
|
|
filters.from || filters.to
|
|
? {
|
|
...(filters.from ? { gte: new Date(`${filters.from}T00:00:00`) } : {}),
|
|
...(filters.to ? { lte: new Date(`${filters.to}T23:59:59.999`) } : {}),
|
|
}
|
|
: undefined,
|
|
};
|
|
return fetchActivityLogs(prisma, where, filters.limit, filters.offset);
|
|
}
|
|
|
|
/**
|
|
* Logi przypisane do jednej siłowni: operacje na encji `gym`,
|
|
* `membership.*` powiązane przez `details.gymId` oraz wnioski (claims)
|
|
* i zaproszenia tej siłowni.
|
|
*/
|
|
export async function listGymAuditLogs(
|
|
prisma: PrismaClient,
|
|
gymId: string,
|
|
limit: number,
|
|
offset: number,
|
|
) {
|
|
const [claims, invitations] = await Promise.all([
|
|
prisma.gymClaim.findMany({ where: { gymId }, select: { id: true } }),
|
|
prisma.gymInvitation.findMany({ where: { gymId }, select: { id: true } }),
|
|
]);
|
|
const where: Prisma.ActivityLogWhereInput = {
|
|
OR: [
|
|
{ entityType: 'gym', entityId: gymId },
|
|
{ entityType: 'membership', details: { path: ['gymId'], equals: gymId } },
|
|
...(claims.length
|
|
? [{ entityType: 'gym-claim', entityId: { in: claims.map((c) => c.id) } }]
|
|
: []),
|
|
...(invitations.length
|
|
? [{ entityType: 'gym-invitation', entityId: { in: invitations.map((i) => i.id) } }]
|
|
: []),
|
|
],
|
|
};
|
|
return fetchActivityLogs(prisma, where, limit, offset);
|
|
}
|