* Lookup workspace file by storage key from database * @param key Storage key to lookup * @returns Workspace file info or null if not found
(
key: string,
options?: { includeDeleted?: boolean }
)
| 49 | * @returns Workspace file info or null if not found |
| 50 | */ |
| 51 | async function lookupWorkspaceFileByKey( |
| 52 | key: string, |
| 53 | options?: { includeDeleted?: boolean } |
| 54 | ): Promise<{ workspaceId: string; uploadedBy: string } | null> { |
| 55 | try { |
| 56 | const { includeDeleted = false } = options ?? {} |
| 57 | // Priority 1: Check new workspaceFiles table |
| 58 | const fileRecord = await getFileMetadataByKey(key, 'workspace', { includeDeleted }) |
| 59 | |
| 60 | if (fileRecord) { |
| 61 | return { |
| 62 | workspaceId: fileRecord.workspaceId || '', |
| 63 | uploadedBy: fileRecord.userId, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Priority 2: Check legacy workspace_file table (for backward compatibility during migration) |
| 68 | try { |
| 69 | const [legacyFile] = await db |
| 70 | .select({ |
| 71 | workspaceId: workspaceFile.workspaceId, |
| 72 | uploadedBy: workspaceFile.uploadedBy, |
| 73 | }) |
| 74 | .from(workspaceFile) |
| 75 | .where( |
| 76 | includeDeleted |
| 77 | ? eq(workspaceFile.key, key) |
| 78 | : and(eq(workspaceFile.key, key), isNull(workspaceFile.deletedAt)) |
| 79 | ) |
| 80 | .limit(1) |
| 81 | |
| 82 | if (legacyFile) { |
| 83 | return { |
| 84 | workspaceId: legacyFile.workspaceId, |
| 85 | uploadedBy: legacyFile.uploadedBy, |
| 86 | } |
| 87 | } |
| 88 | } catch (legacyError) { |
| 89 | // Ignore errors when checking legacy table (it may not exist after migration) |
| 90 | logger.debug('Legacy workspace_file table check failed (may not exist):', legacyError) |
| 91 | } |
| 92 | |
| 93 | return null |
| 94 | } catch (error) { |
| 95 | logger.error('Error looking up workspace file by key:', { key, error }) |
| 96 | return null |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Extract workspace ID from workspace file key pattern |
no test coverage detected