d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { pendingCredentialDraft } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { generateId } from '@sim/utils/id'
|
|
import { and, eq, lt } from 'drizzle-orm'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { createCredentialDraftContract } from '@/lib/api/contracts/credentials'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { getSession } from '@/lib/auth'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { getCredentialActorContext } from '@/lib/credentials/access'
|
|
import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils'
|
|
|
|
const logger = createLogger('CredentialDraftAPI')
|
|
|
|
const DRAFT_TTL_MS = 15 * 60 * 1000
|
|
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
try {
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const parsed = await parseRequest(createCredentialDraftContract, request, {})
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const { workspaceId, providerId, displayName, description, credentialId } = parsed.data.body
|
|
const userId = session.user.id
|
|
|
|
const workspaceAccess = await checkWorkspaceAccess(workspaceId, userId)
|
|
if (!workspaceAccess.canWrite) {
|
|
return NextResponse.json({ error: 'Write permission required' }, { status: 403 })
|
|
}
|
|
|
|
if (credentialId) {
|
|
const access = await getCredentialActorContext(credentialId, userId, { workspaceAccess })
|
|
if (!access.credential || access.credential.workspaceId !== workspaceId || !access.isAdmin) {
|
|
return NextResponse.json(
|
|
{ error: 'Admin access required on the target credential' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
}
|
|
|
|
const now = new Date()
|
|
|
|
await db
|
|
.delete(pendingCredentialDraft)
|
|
.where(
|
|
and(eq(pendingCredentialDraft.userId, userId), lt(pendingCredentialDraft.expiresAt, now))
|
|
)
|
|
|
|
await db
|
|
.insert(pendingCredentialDraft)
|
|
.values({
|
|
id: generateId(),
|
|
userId,
|
|
workspaceId,
|
|
providerId,
|
|
displayName,
|
|
description: description || null,
|
|
credentialId: credentialId || null,
|
|
expiresAt: new Date(now.getTime() + DRAFT_TTL_MS),
|
|
createdAt: now,
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [
|
|
pendingCredentialDraft.userId,
|
|
pendingCredentialDraft.providerId,
|
|
pendingCredentialDraft.workspaceId,
|
|
],
|
|
set: {
|
|
displayName,
|
|
description: description || null,
|
|
credentialId: credentialId || null,
|
|
expiresAt: new Date(now.getTime() + DRAFT_TTL_MS),
|
|
createdAt: now,
|
|
},
|
|
})
|
|
|
|
logger.info('Credential draft saved', {
|
|
userId,
|
|
workspaceId,
|
|
providerId,
|
|
displayName,
|
|
credentialId: credentialId || null,
|
|
})
|
|
|
|
return NextResponse.json({ success: true }, { status: 200 })
|
|
} catch (error) {
|
|
logger.error('Failed to save credential draft', { error })
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
})
|