MCPcopy
hub / github.com/CodebuffAI/codebuff / getUserIdForAnalytics

Function getUserIdForAnalytics

packages/internal/src/db/transaction.ts:73–93  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

71 * Falls back to 'system' if no user ID can be determined.
72 */
73function 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
95function getPostgresErrorCode(error: unknown): string | null {
96 if (!error || typeof error !== 'object') {

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected