|
1 | 1 | import { db } from '@sim/db' |
2 | 2 | import { account } from '@sim/db/schema' |
3 | 3 | import { createLogger } from '@sim/logger' |
4 | | -import { and, eq } from 'drizzle-orm' |
| 4 | +import { eq } from 'drizzle-orm' |
5 | 5 | import { type NextRequest, NextResponse } from 'next/server' |
6 | 6 | import { getSession } from '@/lib/auth' |
7 | 7 | import { validateAlphanumericId } from '@/lib/core/security/input-validation' |
8 | 8 | import { generateRequestId } from '@/lib/core/utils/request' |
9 | | -import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils' |
| 9 | +import { refreshAccessTokenIfNeeded, resolveOAuthAccountId } from '@/app/api/auth/oauth/utils' |
10 | 10 | export const dynamic = 'force-dynamic' |
11 | 11 |
|
12 | 12 | const logger = createLogger('GmailLabelsAPI') |
@@ -45,27 +45,45 @@ export async function GET(request: NextRequest) { |
45 | 45 | return NextResponse.json({ error: credentialIdValidation.error }, { status: 400 }) |
46 | 46 | } |
47 | 47 |
|
48 | | - let credentials = await db |
| 48 | + const resolved = await resolveOAuthAccountId(credentialId) |
| 49 | + if (!resolved) { |
| 50 | + return NextResponse.json({ error: 'Credential not found' }, { status: 404 }) |
| 51 | + } |
| 52 | + |
| 53 | + if (resolved.workspaceId) { |
| 54 | + const { getUserEntityPermissions } = await import('@/lib/workspaces/permissions/utils') |
| 55 | + const perm = await getUserEntityPermissions( |
| 56 | + session.user.id, |
| 57 | + 'workspace', |
| 58 | + resolved.workspaceId |
| 59 | + ) |
| 60 | + if (perm === null) { |
| 61 | + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const credentials = await db |
49 | 66 | .select() |
50 | 67 | .from(account) |
51 | | - .where(and(eq(account.id, credentialId), eq(account.userId, session.user.id))) |
| 68 | + .where(eq(account.id, resolved.accountId)) |
52 | 69 | .limit(1) |
53 | 70 |
|
54 | 71 | if (!credentials.length) { |
55 | | - credentials = await db.select().from(account).where(eq(account.id, credentialId)).limit(1) |
56 | | - if (!credentials.length) { |
57 | | - logger.warn(`[${requestId}] Credential not found`) |
58 | | - return NextResponse.json({ error: 'Credential not found' }, { status: 404 }) |
59 | | - } |
| 72 | + logger.warn(`[${requestId}] Credential not found`) |
| 73 | + return NextResponse.json({ error: 'Credential not found' }, { status: 404 }) |
60 | 74 | } |
61 | 75 |
|
62 | | - const credential = credentials[0] |
| 76 | + const accountRow = credentials[0] |
63 | 77 |
|
64 | 78 | logger.info( |
65 | | - `[${requestId}] Using credential: ${credential.id}, provider: ${credential.providerId}` |
| 79 | + `[${requestId}] Using credential: ${accountRow.id}, provider: ${accountRow.providerId}` |
66 | 80 | ) |
67 | 81 |
|
68 | | - const accessToken = await refreshAccessTokenIfNeeded(credentialId, credential.userId, requestId) |
| 82 | + const accessToken = await refreshAccessTokenIfNeeded( |
| 83 | + resolved.accountId, |
| 84 | + accountRow.userId, |
| 85 | + requestId |
| 86 | + ) |
69 | 87 |
|
70 | 88 | if (!accessToken) { |
71 | 89 | return NextResponse.json({ error: 'Failed to obtain valid access token' }, { status: 401 }) |
|
0 commit comments