@@ -2,225 +2,14 @@ import { auditLog, db } from '@sim/db'
22import { user } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { eq } from 'drizzle-orm'
5+ import type { AuditActionType , AuditResourceTypeValue } from '@/lib/audit/types'
56import { getClientIp } from '@/lib/core/utils/request'
67import { generateShortId } from '@/lib/core/utils/uuid'
78
8- const logger = createLogger ( 'AuditLog' )
9-
10- /**
11- * All auditable actions in the platform, grouped by resource type.
12- */
13- export const AuditAction = {
14- // API Keys
15- API_KEY_CREATED : 'api_key.created' ,
16- API_KEY_UPDATED : 'api_key.updated' ,
17- API_KEY_REVOKED : 'api_key.revoked' ,
18- PERSONAL_API_KEY_CREATED : 'personal_api_key.created' ,
19- PERSONAL_API_KEY_REVOKED : 'personal_api_key.revoked' ,
20-
21- // BYOK Keys
22- BYOK_KEY_CREATED : 'byok_key.created' ,
23- BYOK_KEY_UPDATED : 'byok_key.updated' ,
24- BYOK_KEY_DELETED : 'byok_key.deleted' ,
25-
26- // Chat
27- CHAT_DEPLOYED : 'chat.deployed' ,
28- CHAT_UPDATED : 'chat.updated' ,
29- CHAT_DELETED : 'chat.deleted' ,
30-
31- // Custom Tools
32- CUSTOM_TOOL_CREATED : 'custom_tool.created' ,
33- CUSTOM_TOOL_UPDATED : 'custom_tool.updated' ,
34- CUSTOM_TOOL_DELETED : 'custom_tool.deleted' ,
35-
36- // Billing
37- CREDIT_PURCHASED : 'credit.purchased' ,
38-
39- // Credential Sets
40- CREDENTIAL_SET_CREATED : 'credential_set.created' ,
41- CREDENTIAL_SET_UPDATED : 'credential_set.updated' ,
42- CREDENTIAL_SET_DELETED : 'credential_set.deleted' ,
43- CREDENTIAL_SET_MEMBER_REMOVED : 'credential_set_member.removed' ,
44- CREDENTIAL_SET_MEMBER_LEFT : 'credential_set_member.left' ,
45- CREDENTIAL_SET_INVITATION_CREATED : 'credential_set_invitation.created' ,
46- CREDENTIAL_SET_INVITATION_ACCEPTED : 'credential_set_invitation.accepted' ,
47- CREDENTIAL_SET_INVITATION_RESENT : 'credential_set_invitation.resent' ,
48- CREDENTIAL_SET_INVITATION_REVOKED : 'credential_set_invitation.revoked' ,
49-
50- // Connector Documents
51- CONNECTOR_DOCUMENT_RESTORED : 'connector_document.restored' ,
52- CONNECTOR_DOCUMENT_EXCLUDED : 'connector_document.excluded' ,
53-
54- // Documents
55- DOCUMENT_UPLOADED : 'document.uploaded' ,
56- DOCUMENT_UPDATED : 'document.updated' ,
57- DOCUMENT_DELETED : 'document.deleted' ,
58-
59- // Environment
60- ENVIRONMENT_UPDATED : 'environment.updated' ,
61- ENVIRONMENT_DELETED : 'environment.deleted' ,
62-
63- // Files
64- FILE_UPLOADED : 'file.uploaded' ,
65- FILE_UPDATED : 'file.updated' ,
66- FILE_DELETED : 'file.deleted' ,
67- FILE_RESTORED : 'file.restored' ,
68-
69- // Folders
70- FOLDER_CREATED : 'folder.created' ,
71- FOLDER_DELETED : 'folder.deleted' ,
72- FOLDER_DUPLICATED : 'folder.duplicated' ,
73- FOLDER_RESTORED : 'folder.restored' ,
74-
75- // Forms
76- FORM_CREATED : 'form.created' ,
77- FORM_UPDATED : 'form.updated' ,
78- FORM_DELETED : 'form.deleted' ,
79-
80- // Invitations
81- INVITATION_ACCEPTED : 'invitation.accepted' ,
82- INVITATION_RESENT : 'invitation.resent' ,
83- INVITATION_REVOKED : 'invitation.revoked' ,
84-
85- // Knowledge Base Connectors
86- CONNECTOR_CREATED : 'connector.created' ,
87- CONNECTOR_UPDATED : 'connector.updated' ,
88- CONNECTOR_DELETED : 'connector.deleted' ,
89- CONNECTOR_SYNCED : 'connector.synced' ,
90-
91- // Knowledge Bases
92- KNOWLEDGE_BASE_CREATED : 'knowledge_base.created' ,
93- KNOWLEDGE_BASE_UPDATED : 'knowledge_base.updated' ,
94- KNOWLEDGE_BASE_DELETED : 'knowledge_base.deleted' ,
95- KNOWLEDGE_BASE_RESTORED : 'knowledge_base.restored' ,
96-
97- // MCP Servers
98- MCP_SERVER_ADDED : 'mcp_server.added' ,
99- MCP_SERVER_UPDATED : 'mcp_server.updated' ,
100- MCP_SERVER_REMOVED : 'mcp_server.removed' ,
9+ export type { AuditActionType , AuditResourceTypeValue } from '@/lib/audit/types'
10+ export { AuditAction , AuditResourceType } from '@/lib/audit/types'
10111
102- // Members
103- MEMBER_INVITED : 'member.invited' ,
104- MEMBER_REMOVED : 'member.removed' ,
105- MEMBER_ROLE_CHANGED : 'member.role_changed' ,
106-
107- // Notifications
108- NOTIFICATION_CREATED : 'notification.created' ,
109- NOTIFICATION_UPDATED : 'notification.updated' ,
110- NOTIFICATION_DELETED : 'notification.deleted' ,
111-
112- // OAuth / Credentials
113- OAUTH_DISCONNECTED : 'oauth.disconnected' ,
114- CREDENTIAL_CREATED : 'credential.created' ,
115- CREDENTIAL_UPDATED : 'credential.updated' ,
116- CREDENTIAL_RENAMED : 'credential.renamed' ,
117- CREDENTIAL_DELETED : 'credential.deleted' ,
118-
119- // Password
120- PASSWORD_RESET_REQUESTED : 'password.reset_requested' ,
121- PASSWORD_RESET : 'password.reset' ,
122-
123- // Organizations
124- ORGANIZATION_CREATED : 'organization.created' ,
125- ORGANIZATION_UPDATED : 'organization.updated' ,
126- ORG_MEMBER_ADDED : 'org_member.added' ,
127- ORG_MEMBER_REMOVED : 'org_member.removed' ,
128- ORG_MEMBER_ROLE_CHANGED : 'org_member.role_changed' ,
129- ORG_INVITATION_CREATED : 'org_invitation.created' ,
130- ORG_INVITATION_ACCEPTED : 'org_invitation.accepted' ,
131- ORG_INVITATION_REJECTED : 'org_invitation.rejected' ,
132- ORG_INVITATION_CANCELLED : 'org_invitation.cancelled' ,
133- ORG_INVITATION_REVOKED : 'org_invitation.revoked' ,
134- ORG_INVITATION_RESENT : 'org_invitation.resent' ,
135-
136- // Permission Groups
137- PERMISSION_GROUP_CREATED : 'permission_group.created' ,
138- PERMISSION_GROUP_UPDATED : 'permission_group.updated' ,
139- PERMISSION_GROUP_DELETED : 'permission_group.deleted' ,
140- PERMISSION_GROUP_MEMBER_ADDED : 'permission_group_member.added' ,
141- PERMISSION_GROUP_MEMBER_REMOVED : 'permission_group_member.removed' ,
142-
143- // Skills
144- SKILL_CREATED : 'skill.created' ,
145- SKILL_UPDATED : 'skill.updated' ,
146- SKILL_DELETED : 'skill.deleted' ,
147-
148- // Schedules
149- SCHEDULE_CREATED : 'schedule.created' ,
150- SCHEDULE_UPDATED : 'schedule.updated' ,
151- SCHEDULE_DELETED : 'schedule.deleted' ,
152-
153- // Tables
154- TABLE_CREATED : 'table.created' ,
155- TABLE_UPDATED : 'table.updated' ,
156- TABLE_DELETED : 'table.deleted' ,
157- TABLE_RESTORED : 'table.restored' ,
158-
159- // Templates
160- TEMPLATE_CREATED : 'template.created' ,
161- TEMPLATE_UPDATED : 'template.updated' ,
162- TEMPLATE_DELETED : 'template.deleted' ,
163-
164- // Webhooks
165- WEBHOOK_CREATED : 'webhook.created' ,
166- WEBHOOK_DELETED : 'webhook.deleted' ,
167-
168- // Workflows
169- WORKFLOW_CREATED : 'workflow.created' ,
170- WORKFLOW_DELETED : 'workflow.deleted' ,
171- WORKFLOW_RESTORED : 'workflow.restored' ,
172- WORKFLOW_DEPLOYED : 'workflow.deployed' ,
173- WORKFLOW_UNDEPLOYED : 'workflow.undeployed' ,
174- WORKFLOW_DUPLICATED : 'workflow.duplicated' ,
175- WORKFLOW_DEPLOYMENT_ACTIVATED : 'workflow.deployment_activated' ,
176- WORKFLOW_DEPLOYMENT_REVERTED : 'workflow.deployment_reverted' ,
177- WORKFLOW_LOCKED : 'workflow.locked' ,
178- WORKFLOW_UNLOCKED : 'workflow.unlocked' ,
179- WORKFLOW_VARIABLES_UPDATED : 'workflow.variables_updated' ,
180-
181- // Workspaces
182- WORKSPACE_CREATED : 'workspace.created' ,
183- WORKSPACE_UPDATED : 'workspace.updated' ,
184- WORKSPACE_DELETED : 'workspace.deleted' ,
185- WORKSPACE_DUPLICATED : 'workspace.duplicated' ,
186- } as const
187-
188- export type AuditActionType = ( typeof AuditAction ) [ keyof typeof AuditAction ]
189-
190- /**
191- * All resource types that can appear in audit log entries.
192- */
193- export const AuditResourceType = {
194- API_KEY : 'api_key' ,
195- BILLING : 'billing' ,
196- BYOK_KEY : 'byok_key' ,
197- CHAT : 'chat' ,
198- CONNECTOR : 'connector' ,
199- CREDENTIAL : 'credential' ,
200- CREDENTIAL_SET : 'credential_set' ,
201- CUSTOM_TOOL : 'custom_tool' ,
202- DOCUMENT : 'document' ,
203- ENVIRONMENT : 'environment' ,
204- FILE : 'file' ,
205- FOLDER : 'folder' ,
206- FORM : 'form' ,
207- KNOWLEDGE_BASE : 'knowledge_base' ,
208- MCP_SERVER : 'mcp_server' ,
209- NOTIFICATION : 'notification' ,
210- OAUTH : 'oauth' ,
211- ORGANIZATION : 'organization' ,
212- PASSWORD : 'password' ,
213- PERMISSION_GROUP : 'permission_group' ,
214- SCHEDULE : 'schedule' ,
215- SKILL : 'skill' ,
216- TABLE : 'table' ,
217- TEMPLATE : 'template' ,
218- WEBHOOK : 'webhook' ,
219- WORKFLOW : 'workflow' ,
220- WORKSPACE : 'workspace' ,
221- } as const
222-
223- export type AuditResourceTypeValue = ( typeof AuditResourceType ) [ keyof typeof AuditResourceType ]
12+ const logger = createLogger ( 'AuditLog' )
22413
22514interface AuditLogParams {
22615 workspaceId ?: string | null
0 commit comments