( chatId: string, userId: string )
| 107 | * Check if user has access to view/edit/delete a specific chat |
| 108 | */ |
| 109 | export async function checkChatAccess( |
| 110 | chatId: string, |
| 111 | userId: string |
| 112 | ): Promise<{ hasAccess: boolean; chat?: any; workspaceId?: string }> { |
| 113 | const chatData = await db |
| 114 | .select({ |
| 115 | chat: chat, |
| 116 | workflowWorkspaceId: workflow.workspaceId, |
| 117 | }) |
| 118 | .from(chat) |
| 119 | .innerJoin(workflow, eq(chat.workflowId, workflow.id)) |
| 120 | .where(and(eq(chat.id, chatId), isNull(chat.archivedAt))) |
| 121 | .limit(1) |
| 122 | |
| 123 | if (chatData.length === 0) { |
| 124 | return { hasAccess: false } |
| 125 | } |
| 126 | |
| 127 | const { chat: chatRecord, workflowWorkspaceId } = chatData[0] |
| 128 | if (!workflowWorkspaceId) { |
| 129 | return { hasAccess: false } |
| 130 | } |
| 131 | |
| 132 | const authorization = await authorizeWorkflowByWorkspacePermission({ |
| 133 | workflowId: chatRecord.workflowId, |
| 134 | userId, |
| 135 | action: 'admin', |
| 136 | }) |
| 137 | |
| 138 | return authorization.allowed |
| 139 | ? { hasAccess: true, chat: chatRecord, workspaceId: workflowWorkspaceId } |
| 140 | : { hasAccess: false } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Validates auth for a deployed chat. Thin wrapper over the shared |
no test coverage detected