diff --git a/packages/backend-core/src/constants/db.ts b/packages/backend-core/src/constants/db.ts index f7d15b3880..d41098c405 100644 --- a/packages/backend-core/src/constants/db.ts +++ b/packages/backend-core/src/constants/db.ts @@ -68,6 +68,7 @@ export enum DocumentType { MEM_VIEW = "view", USER_FLAG = "flag", AUTOMATION_METADATA = "meta_au", + AUDIT_LOG = "al", } export const StaticDatabases = { @@ -88,6 +89,9 @@ export const StaticDatabases = { install: "install", }, }, + AUDIT_LOGS: { + name: "audit-logs", + }, } export const APP_PREFIX = DocumentType.APP + SEPARATOR diff --git a/packages/backend-core/src/context/mainContext.ts b/packages/backend-core/src/context/mainContext.ts index 9884d25d5a..1f14a20778 100644 --- a/packages/backend-core/src/context/mainContext.ts +++ b/packages/backend-core/src/context/mainContext.ts @@ -30,6 +30,13 @@ export function getGlobalDBName(tenantId?: string) { return baseGlobalDBName(tenantId) } +export function getAuditLogDBName(tenantId?: string) { + if (!tenantId) { + tenantId = getTenantId() + } + return `${tenantId}${SEPARATOR}${StaticDatabases.AUDIT_LOGS.name}` +} + export function baseGlobalDBName(tenantId: string | undefined | null) { let dbName if (!tenantId || tenantId === DEFAULT_TENANT_ID) { @@ -228,6 +235,13 @@ export function getGlobalDB(): Database { return getDB(baseGlobalDBName(context?.tenantId)) } +export function getAuditLogsDB(): Database { + if (!getTenantId()) { + throw new Error("Audit log DB not found") + } + return getDB(getAuditLogDBName()) +} + /** * Gets the app database based on whatever the request * contained, dev or prod. diff --git a/packages/types/src/api/web/global/auditLogs.ts b/packages/types/src/api/web/global/auditLogs.ts index 7281d0ca36..443cd79aa1 100644 --- a/packages/types/src/api/web/global/auditLogs.ts +++ b/packages/types/src/api/web/global/auditLogs.ts @@ -1,7 +1,7 @@ import { Event, AuditedEventFriendlyName } from "../../../sdk" import { PaginationResponse, PaginationRequest } from "../" -export interface DownloadAuditLogsRequest { +export interface AuditLogSearchParams { userId?: string[] appId?: string[] event?: Event[] @@ -10,9 +10,11 @@ export interface DownloadAuditLogsRequest { metadataSearch?: string } +export interface DownloadAuditLogsRequest extends AuditLogSearchParams {} + export interface SearchAuditLogsRequest extends PaginationRequest, - DownloadAuditLogsRequest {} + AuditLogSearchParams {} export interface SearchAuditLogsResponse extends PaginationResponse { data: { diff --git a/packages/types/src/documents/global/auditLogs.ts b/packages/types/src/documents/global/auditLogs.ts index bc07bc88d4..5b23650eec 100644 --- a/packages/types/src/documents/global/auditLogs.ts +++ b/packages/types/src/documents/global/auditLogs.ts @@ -1,8 +1,8 @@ import { Document } from "../document" import { Event } from "../../sdk" -export interface AuditLogDocument extends Document { - appId: string +export interface AuditLogDoc extends Document { + appId?: string event: Event userId: string timestamp: string diff --git a/packages/types/src/documents/global/index.ts b/packages/types/src/documents/global/index.ts index 11ce7513f2..b728439dd6 100644 --- a/packages/types/src/documents/global/index.ts +++ b/packages/types/src/documents/global/index.ts @@ -6,3 +6,4 @@ export * from "./quotas" export * from "./schedule" export * from "./templates" export * from "./environmentVariables" +export * from "./auditLogs"