d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
135 lines
4.6 KiB
TypeScript
135 lines
4.6 KiB
TypeScript
import { db, mothershipInboxTask, workspace } from '@sim/db'
|
|
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { eq, sql } from 'drizzle-orm'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { updateInboxConfigContract } from '@/lib/api/contracts/inbox'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { getSession } from '@/lib/auth'
|
|
import { hasWorkspaceInboxAccess } from '@/lib/billing/core/subscription'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { disableInbox, enableInbox, updateInboxAddress } from '@/lib/mothership/inbox/lifecycle'
|
|
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
|
|
|
const logger = createLogger('InboxConfigAPI')
|
|
|
|
export const GET = withRouteHandler(
|
|
async (_req: NextRequest, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id: workspaceId } = await params
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const permission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId)
|
|
if (!permission) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
|
|
const [wsResult, statsResult, entitled] = await Promise.all([
|
|
db
|
|
.select({
|
|
inboxEnabled: workspace.inboxEnabled,
|
|
inboxAddress: workspace.inboxAddress,
|
|
})
|
|
.from(workspace)
|
|
.where(eq(workspace.id, workspaceId))
|
|
.limit(1),
|
|
db
|
|
.select({
|
|
status: mothershipInboxTask.status,
|
|
count: sql<number>`count(*)::int`,
|
|
})
|
|
.from(mothershipInboxTask)
|
|
.where(eq(mothershipInboxTask.workspaceId, workspaceId))
|
|
.groupBy(mothershipInboxTask.status),
|
|
hasWorkspaceInboxAccess(workspaceId),
|
|
])
|
|
|
|
const [ws] = wsResult
|
|
if (!ws) {
|
|
return NextResponse.json({ error: 'Workspace not found' }, { status: 404 })
|
|
}
|
|
|
|
const stats = {
|
|
total: 0,
|
|
completed: 0,
|
|
processing: 0,
|
|
failed: 0,
|
|
}
|
|
for (const row of statsResult) {
|
|
const count = Number(row.count)
|
|
stats.total += count
|
|
if (row.status === 'completed') stats.completed = count
|
|
else if (row.status === 'processing') stats.processing = count
|
|
else if (row.status === 'failed') stats.failed = count
|
|
}
|
|
|
|
return NextResponse.json({
|
|
enabled: ws.inboxEnabled,
|
|
address: ws.inboxAddress,
|
|
entitled,
|
|
taskStats: stats,
|
|
})
|
|
}
|
|
)
|
|
|
|
export const PATCH = withRouteHandler(
|
|
async (req: NextRequest, context: { params: Promise<{ id: string }> }) => {
|
|
const { id: workspaceId } = await context.params
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const permission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId)
|
|
if (permission !== 'admin') {
|
|
return NextResponse.json({ error: 'Admin access required' }, { status: 403 })
|
|
}
|
|
|
|
const parsed = await parseRequest(updateInboxConfigContract, req, context)
|
|
if (!parsed.success) return parsed.response
|
|
const body = parsed.data.body
|
|
|
|
try {
|
|
if (body.enabled === false) {
|
|
await disableInbox(workspaceId)
|
|
return NextResponse.json({ enabled: false, address: null })
|
|
}
|
|
|
|
if (!(await hasWorkspaceInboxAccess(workspaceId))) {
|
|
return NextResponse.json({ error: 'Sim Mailer requires a Max plan' }, { status: 403 })
|
|
}
|
|
|
|
if (body.enabled === true) {
|
|
const [current] = await db
|
|
.select({ inboxEnabled: workspace.inboxEnabled })
|
|
.from(workspace)
|
|
.where(eq(workspace.id, workspaceId))
|
|
.limit(1)
|
|
if (current?.inboxEnabled) {
|
|
return NextResponse.json({ error: 'Inbox is already enabled' }, { status: 409 })
|
|
}
|
|
const config = await enableInbox(workspaceId, { username: body.username })
|
|
return NextResponse.json(config)
|
|
}
|
|
|
|
if (body.username) {
|
|
const config = await updateInboxAddress(workspaceId, body.username)
|
|
return NextResponse.json(config)
|
|
}
|
|
|
|
return NextResponse.json({ error: 'No valid update provided' }, { status: 400 })
|
|
} catch (error) {
|
|
logger.error('Inbox config update failed', {
|
|
workspaceId,
|
|
error: getErrorMessage(error, 'Unknown error'),
|
|
})
|
|
return NextResponse.json(
|
|
{ error: getErrorMessage(error, 'Failed to update inbox') },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
)
|