chore: import upstream snapshot with attribution
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
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
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,169 @@
|
||||
import { db, mothershipInboxAllowedSender, permissions, user } from '@sim/db'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { generateId } from '@sim/utils/id'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { addInboxSenderContract, removeInboxSenderContract } 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 { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
const logger = createLogger('InboxSendersAPI')
|
||||
|
||||
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 [hasAccess, permission] = await Promise.all([
|
||||
hasWorkspaceInboxAccess(workspaceId),
|
||||
getUserEntityPermissions(session.user.id, 'workspace', workspaceId),
|
||||
])
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json({ error: 'Sim Mailer requires a Max plan' }, { status: 403 })
|
||||
}
|
||||
if (!permission) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const [senders, members] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
id: mothershipInboxAllowedSender.id,
|
||||
email: mothershipInboxAllowedSender.email,
|
||||
label: mothershipInboxAllowedSender.label,
|
||||
createdAt: mothershipInboxAllowedSender.createdAt,
|
||||
})
|
||||
.from(mothershipInboxAllowedSender)
|
||||
.where(eq(mothershipInboxAllowedSender.workspaceId, workspaceId))
|
||||
.orderBy(mothershipInboxAllowedSender.createdAt),
|
||||
db
|
||||
.select({
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
})
|
||||
.from(permissions)
|
||||
.innerJoin(user, eq(permissions.userId, user.id))
|
||||
.where(and(eq(permissions.entityType, 'workspace'), eq(permissions.entityId, workspaceId))),
|
||||
])
|
||||
|
||||
return NextResponse.json({
|
||||
senders: senders.map((s) => ({
|
||||
id: s.id,
|
||||
email: s.email,
|
||||
label: s.label,
|
||||
createdAt: s.createdAt,
|
||||
})),
|
||||
workspaceMembers: members.map((m) => ({
|
||||
email: m.email,
|
||||
name: m.name,
|
||||
isAutoAllowed: true,
|
||||
})),
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
export const POST = 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 [hasAccess, permission] = await Promise.all([
|
||||
hasWorkspaceInboxAccess(workspaceId),
|
||||
getUserEntityPermissions(session.user.id, 'workspace', workspaceId),
|
||||
])
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json({ error: 'Sim Mailer requires a Max plan' }, { status: 403 })
|
||||
}
|
||||
if (permission !== 'admin') {
|
||||
return NextResponse.json({ error: 'Admin access required' }, { status: 403 })
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = await parseRequest(addInboxSenderContract, req, context)
|
||||
if (!parsed.success) return parsed.response
|
||||
const { email, label } = parsed.data.body
|
||||
const normalizedEmail = email.toLowerCase()
|
||||
|
||||
const [existing] = await db
|
||||
.select({ id: mothershipInboxAllowedSender.id })
|
||||
.from(mothershipInboxAllowedSender)
|
||||
.where(
|
||||
and(
|
||||
eq(mothershipInboxAllowedSender.workspaceId, workspaceId),
|
||||
eq(mothershipInboxAllowedSender.email, normalizedEmail)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: 'Sender already exists' }, { status: 409 })
|
||||
}
|
||||
|
||||
const [sender] = await db
|
||||
.insert(mothershipInboxAllowedSender)
|
||||
.values({
|
||||
id: generateId(),
|
||||
workspaceId,
|
||||
email: normalizedEmail,
|
||||
label: label || null,
|
||||
addedBy: session.user.id,
|
||||
})
|
||||
.returning()
|
||||
|
||||
return NextResponse.json({ sender })
|
||||
} catch (error) {
|
||||
logger.error('Failed to add sender', { workspaceId, error })
|
||||
return NextResponse.json({ error: 'Failed to add sender' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const DELETE = 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 [hasAccess, permission] = await Promise.all([
|
||||
hasWorkspaceInboxAccess(workspaceId),
|
||||
getUserEntityPermissions(session.user.id, 'workspace', workspaceId),
|
||||
])
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json({ error: 'Sim Mailer requires a Max plan' }, { status: 403 })
|
||||
}
|
||||
if (permission !== 'admin') {
|
||||
return NextResponse.json({ error: 'Admin access required' }, { status: 403 })
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = await parseRequest(removeInboxSenderContract, req, context)
|
||||
if (!parsed.success) return parsed.response
|
||||
const { senderId } = parsed.data.body
|
||||
|
||||
await db
|
||||
.delete(mothershipInboxAllowedSender)
|
||||
.where(
|
||||
and(
|
||||
eq(mothershipInboxAllowedSender.id, senderId),
|
||||
eq(mothershipInboxAllowedSender.workspaceId, workspaceId)
|
||||
)
|
||||
)
|
||||
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (error) {
|
||||
logger.error('Failed to delete sender', { workspaceId, error })
|
||||
return NextResponse.json({ error: 'Failed to delete sender' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,102 @@
|
||||
import { db, mothershipInboxTask } from '@sim/db'
|
||||
import { and, desc, eq, lt } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { inboxTasksQuerySchema, inboxWorkspaceParamsSchema } from '@/lib/api/contracts/inbox'
|
||||
import { getValidationErrorMessage } 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 { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
export const GET = withRouteHandler(
|
||||
async (req: NextRequest, { params }: { params: Promise<{ id: string }> }) => {
|
||||
const paramsResult = inboxWorkspaceParamsSchema.safeParse(await params)
|
||||
if (!paramsResult.success) {
|
||||
return NextResponse.json(
|
||||
{ error: getValidationErrorMessage(paramsResult.error, 'Invalid route parameters') },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
const { id: workspaceId } = paramsResult.data
|
||||
const session = await getSession()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const [hasAccess, permission] = await Promise.all([
|
||||
hasWorkspaceInboxAccess(workspaceId),
|
||||
getUserEntityPermissions(session.user.id, 'workspace', workspaceId),
|
||||
])
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json({ error: 'Sim Mailer requires a Max plan' }, { status: 403 })
|
||||
}
|
||||
if (!permission) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const queryResult = inboxTasksQuerySchema.safeParse(
|
||||
Object.fromEntries(req.nextUrl.searchParams.entries())
|
||||
)
|
||||
if (!queryResult.success) {
|
||||
return NextResponse.json(
|
||||
{ error: getValidationErrorMessage(queryResult.error, 'Invalid query parameters') },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const { cursor } = queryResult.data
|
||||
const status = queryResult.data.status ?? 'all'
|
||||
const limit = queryResult.data.limit ?? 20
|
||||
|
||||
const conditions = [eq(mothershipInboxTask.workspaceId, workspaceId)]
|
||||
|
||||
if (status !== 'all') {
|
||||
conditions.push(eq(mothershipInboxTask.status, status))
|
||||
}
|
||||
|
||||
if (cursor) {
|
||||
const cursorDate = new Date(cursor)
|
||||
if (Number.isNaN(cursorDate.getTime())) {
|
||||
return NextResponse.json({ error: 'Invalid cursor value' }, { status: 400 })
|
||||
}
|
||||
conditions.push(lt(mothershipInboxTask.createdAt, cursorDate))
|
||||
}
|
||||
|
||||
const tasks = await db
|
||||
.select({
|
||||
id: mothershipInboxTask.id,
|
||||
fromEmail: mothershipInboxTask.fromEmail,
|
||||
fromName: mothershipInboxTask.fromName,
|
||||
subject: mothershipInboxTask.subject,
|
||||
bodyPreview: mothershipInboxTask.bodyPreview,
|
||||
status: mothershipInboxTask.status,
|
||||
hasAttachments: mothershipInboxTask.hasAttachments,
|
||||
resultSummary: mothershipInboxTask.resultSummary,
|
||||
errorMessage: mothershipInboxTask.errorMessage,
|
||||
rejectionReason: mothershipInboxTask.rejectionReason,
|
||||
chatId: mothershipInboxTask.chatId,
|
||||
createdAt: mothershipInboxTask.createdAt,
|
||||
completedAt: mothershipInboxTask.completedAt,
|
||||
})
|
||||
.from(mothershipInboxTask)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(mothershipInboxTask.createdAt))
|
||||
.limit(limit + 1) // Fetch one extra to determine hasMore
|
||||
|
||||
const hasMore = tasks.length > limit
|
||||
const resultTasks = hasMore ? tasks.slice(0, limit) : tasks
|
||||
const nextCursor =
|
||||
hasMore && resultTasks.length > 0
|
||||
? resultTasks[resultTasks.length - 1].createdAt.toISOString()
|
||||
: null
|
||||
|
||||
return NextResponse.json({
|
||||
tasks: resultTasks,
|
||||
pagination: {
|
||||
limit,
|
||||
hasMore,
|
||||
nextCursor,
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user