UNPKG

alepha

Version:

Easy-to-use modern TypeScript framework for building many kind of applications.

159 lines (158 loc) 4.81 kB
import { z } from "alepha"; import { $entity, db, pageQuerySchema } from "alepha/orm"; //#region ../../src/api/audits/entities/audits.ts /** * Audit severity levels for categorizing events. */ const auditSeveritySchema = z.enum([ "info", "warning", "critical" ]).describe("Severity level of the audit event").default("info"); /** * Audit log entity for tracking important system events. * * Stores comprehensive audit information including: * - Who performed the action (userId, userRealm) * - What happened (type, action, resource) * - When it happened (createdAt) * - Context and details (metadata, ipAddress, userAgent) */ const audits = $entity({ name: "audits", schema: z.object({ id: db.primaryKey(z.bigint()), createdAt: db.createdAt(), organizationId: db.organization(), /** * Audit event type (e.g., "auth", "user", "payment", "system"). * Used for categorizing and filtering audit events. */ type: z.text({ description: "Audit event type (e.g., auth, user, payment, system)" }), /** * Specific action performed (e.g., "login", "logout", "create", "update", "delete"). */ action: z.text({ description: "Specific action performed (e.g., login, create, update)" }), /** * Severity level of the event. */ severity: db.default(auditSeveritySchema, "info"), /** * User ID who performed the action (null for system events). */ userId: z.uuid().optional(), /** * User realm for multi-tenant support. */ userRealm: z.text().optional(), /** * User email at the time of the event (denormalized for history). */ userEmail: z.email().optional(), /** * Resource type affected (e.g., "user", "order", "file"). */ resourceType: z.text().optional(), /** * Resource ID affected. */ resourceId: z.text().optional(), /** * Human-readable description of the event. */ description: z.text().optional(), /** * Additional metadata/context as JSON. */ metadata: z.json().optional(), /** * Client IP address. */ ipAddress: z.text().optional(), /** * Client user agent. */ userAgent: z.text().optional(), /** * Session ID if applicable. */ sessionId: z.uuid().optional(), /** * Request ID for correlation. */ requestId: z.text().optional(), /** * Whether the action was successful. */ success: db.default(z.boolean(), true), /** * Error message if the action failed. */ errorMessage: z.text().optional() }), indexes: [ "createdAt", "type", "action", "userId", "userRealm", "resourceType", "resourceId", "severity", { columns: ["type", "action"] }, { columns: ["userId", "createdAt"] }, { columns: ["userRealm", "createdAt"] } ] }); const auditEntitySchema = audits.schema; const auditEntityInsertSchema = audits.insertSchema; //#endregion //#region ../../src/api/audits/schemas/auditQuerySchema.ts /** * Query schema for searching and filtering audit logs. */ const auditQuerySchema = pageQuerySchema.extend({ type: z.text({ description: "Filter by audit type" }).optional(), action: z.text({ description: "Filter by action" }).optional(), severity: auditSeveritySchema.optional(), userId: z.uuid().describe("Filter by user ID").optional(), userRealm: z.text({ description: "Filter by user realm" }).optional(), resourceType: z.text({ description: "Filter by resource type" }).optional(), resourceId: z.text({ description: "Filter by resource ID" }).optional(), success: z.boolean().describe("Filter by success status").optional(), from: z.datetime().describe("Start date filter").optional(), to: z.datetime().describe("End date filter").optional(), search: z.text({ description: "Search in description" }).optional() }); //#endregion //#region ../../src/api/audits/schemas/auditResourceSchema.ts /** * Resource schema for audit log responses. */ const auditResourceSchema = audits.schema; //#endregion //#region ../../src/api/audits/schemas/createAuditSchema.ts /** * Schema for creating a new audit log entry. */ const createAuditSchema = z.object({ type: z.text({ description: "Audit event type" }), action: z.text({ description: "Specific action performed" }), severity: auditSeveritySchema.optional(), userId: z.uuid().optional(), userRealm: z.text().optional(), userEmail: z.email().optional(), resourceType: z.text().optional(), resourceId: z.text().optional(), description: z.text().optional(), metadata: z.json().optional(), ipAddress: z.text().optional(), userAgent: z.text().optional(), sessionId: z.uuid().optional(), requestId: z.text().optional(), success: z.boolean().optional(), errorMessage: z.text().optional() }); //#endregion export { auditEntityInsertSchema, auditEntitySchema, auditQuerySchema, auditResourceSchema, auditSeveritySchema, audits, createAuditSchema }; //# sourceMappingURL=index.browser.js.map