* Extracts a user ID for analytics tracking from context or lock key. * Falls back to 'system' if no user ID can be determined.
( context: Record<string, unknown>, lockKey?: string, )
| 71 | * Falls back to 'system' if no user ID can be determined. |
| 72 | */ |
| 73 | function getUserIdForAnalytics( |
| 74 | context: Record<string, unknown>, |
| 75 | lockKey?: string, |
| 76 | ): string { |
| 77 | // Try to get userId from context |
| 78 | if (typeof context.userId === 'string' && context.userId) { |
| 79 | return context.userId |
| 80 | } |
| 81 | // Try to get organizationId from context |
| 82 | if (typeof context.organizationId === 'string' && context.organizationId) { |
| 83 | return context.organizationId |
| 84 | } |
| 85 | // Try to extract from lockKey (format: "user:id" or "org:id") |
| 86 | if (lockKey) { |
| 87 | const colonIndex = lockKey.indexOf(':') |
| 88 | if (colonIndex > 0 && colonIndex < lockKey.length - 1) { |
| 89 | return lockKey.substring(colonIndex + 1) |
| 90 | } |
| 91 | } |
| 92 | return 'system' |
| 93 | } |
| 94 | |
| 95 | function getPostgresErrorCode(error: unknown): string | null { |
| 96 | if (!error || typeof error !== 'object') { |
no outgoing calls
no test coverage detected