* Resolve which user ID to use for execution. * Match sender email to a workspace member, fallback to workspace owner.
(
senderEmail: string,
ws: { id: string; ownerId: string }
)
| 327 | * Match sender email to a workspace member, fallback to workspace owner. |
| 328 | */ |
| 329 | async function resolveUserId( |
| 330 | senderEmail: string, |
| 331 | ws: { id: string; ownerId: string } |
| 332 | ): Promise<string> { |
| 333 | const [matchedUser] = await db |
| 334 | .select({ id: user.id }) |
| 335 | .from(user) |
| 336 | .where(sql`lower(${user.email}) = ${senderEmail.toLowerCase()}`) |
| 337 | .orderBy(user.createdAt) |
| 338 | .limit(1) |
| 339 | |
| 340 | if (matchedUser) { |
| 341 | const permission = await getUserEntityPermissions(matchedUser.id, 'workspace', ws.id) |
| 342 | if (permission !== null) { |
| 343 | return matchedUser.id |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return ws.ownerId |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Persist the user message and assistant response to the copilot chat. |
no test coverage detected