74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
// src/app/services/access-log.service.ts
|
|
//
|
|
// PURPOSE:
|
|
// - Core service for audit logging and access tracking
|
|
// - Records all authentication attempts and system access
|
|
// - Provides query capabilities for audit trails
|
|
// - Integrates with Prisma ORM for database operations
|
|
//
|
|
// RULES:
|
|
// - All methods return Promises for async operations
|
|
// - Input validation must be done at controller level
|
|
// - Errors should be propagated to caller with context
|
|
// - Audit records should never be modified after creation
|
|
// - Sensitive data should be hashed before logging
|
|
// - Metadata should be stored as JSON for flexibility
|
|
|
|
import type { AccessLog } from '@prisma/client';
|
|
|
|
import prisma from '../lib/prisma';
|
|
|
|
async function listAccessLogs(): Promise<AccessLog[]> {
|
|
return prisma.accessLog.findMany({
|
|
orderBy: { timestamp: 'desc' },
|
|
take: 100,
|
|
});
|
|
}
|
|
|
|
// TODO: obsoleted getAccessLog, use getAccessLogById instead
|
|
async function getAccessLog(id: string): Promise<AccessLog | null> {
|
|
return prisma.accessLog.findUnique({ where: { id } });
|
|
}
|
|
|
|
async function getAccessLogById(id: string): Promise<AccessLog | null> {
|
|
return prisma.accessLog.findUnique({ where: { id } });
|
|
}
|
|
|
|
async function createAccessLog(userId?: string, message?: string, metadata?: Record<string, any>): Promise<AccessLog> {
|
|
return prisma.accessLog.create({
|
|
data: {
|
|
userId,
|
|
message,
|
|
metadata,
|
|
},
|
|
});
|
|
}
|
|
|
|
function helloworld(): string {
|
|
return 'helloworld';
|
|
}
|
|
|
|
// async function update(id: string, data: UpdateAccessLog): Promise<AccessLog> {
|
|
// return prisma.accessLog.update({
|
|
// where: { id },
|
|
// data: {
|
|
// ...data,
|
|
// metadata: data.metadata || {},
|
|
// },
|
|
// });
|
|
// }
|
|
|
|
// async function deleteAccessLog(id: string): Promise<AccessLog> {
|
|
// return prisma.accessLog.delete({ where: { id } });
|
|
// }
|
|
|
|
export {
|
|
//
|
|
getAccessLog,
|
|
listAccessLogs,
|
|
createAccessLog,
|
|
getAccessLogById,
|
|
//
|
|
helloworld,
|
|
};
|