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
170 lines
5.6 KiB
TypeScript
170 lines
5.6 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { renderHelpConfirmationEmail } from '@/components/emails'
|
|
import { helpFormBodySchema } from '@/lib/api/contracts/common'
|
|
import { validationErrorResponse } from '@/lib/api/server'
|
|
import { getSession } from '@/lib/auth'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import {
|
|
isPayloadSizeLimitError,
|
|
MAX_MULTIPART_OVERHEAD_BYTES,
|
|
readFormDataWithLimit,
|
|
} from '@/lib/core/utils/stream-limits'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { sendEmail } from '@/lib/messaging/email/mailer'
|
|
import { getFromEmailAddress, getHelpEmailAddress } from '@/lib/messaging/email/utils'
|
|
import { MAX_WORKSPACE_FORMDATA_FILE_SIZE } from '@/lib/uploads/shared/types'
|
|
|
|
const logger = createLogger('HelpAPI')
|
|
|
|
/**
|
|
* The form can carry several image attachments with no server-side count
|
|
* cap, so this reuses the repo's largest existing per-request form-data
|
|
* bound (see files/upload route) rather than an arbitrary smaller limit
|
|
* that could reject a legitimate multi-image submission.
|
|
*/
|
|
const MAX_HELP_FORM_BYTES = MAX_WORKSPACE_FORMDATA_FILE_SIZE + MAX_MULTIPART_OVERHEAD_BYTES
|
|
|
|
export const POST = withRouteHandler(async (req: NextRequest) => {
|
|
const requestId = generateRequestId()
|
|
|
|
try {
|
|
const session = await getSession()
|
|
if (!session?.user?.email) {
|
|
logger.warn(`[${requestId}] Unauthorized help request attempt`)
|
|
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
|
}
|
|
|
|
const email = session.user.email
|
|
|
|
const formData = await readFormDataWithLimit(req, {
|
|
maxBytes: MAX_HELP_FORM_BYTES,
|
|
label: 'Help request form data',
|
|
})
|
|
|
|
const subject = formData.get('subject') as string
|
|
const message = formData.get('message') as string
|
|
const type = formData.get('type') as string
|
|
const workflowId = formData.get('workflowId') as string | null
|
|
const workspaceId = formData.get('workspaceId') as string
|
|
const userAgent = formData.get('userAgent') as string | null
|
|
|
|
logger.info(`[${requestId}] Processing help request`, {
|
|
type,
|
|
email: `${email.substring(0, 3)}***`, // Log partial email for privacy
|
|
})
|
|
|
|
const validationResult = helpFormBodySchema.safeParse({
|
|
subject,
|
|
message,
|
|
type,
|
|
})
|
|
|
|
if (!validationResult.success) {
|
|
logger.warn(`[${requestId}] Invalid help request data`, {
|
|
issues: validationResult.error.issues,
|
|
})
|
|
return validationErrorResponse(validationResult.error)
|
|
}
|
|
|
|
const images: { filename: string; content: Buffer; contentType: string }[] = []
|
|
|
|
for (const [key, value] of formData.entries()) {
|
|
if (key.startsWith('image_') && typeof value !== 'string') {
|
|
if (value && 'arrayBuffer' in value) {
|
|
const buffer = Buffer.from(await value.arrayBuffer())
|
|
const filename = value.name || `image_${key.split('_')[1]}`
|
|
|
|
images.push({
|
|
filename,
|
|
content: buffer,
|
|
contentType: value.type || 'application/octet-stream',
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
const userId = session.user.id
|
|
let emailText = `
|
|
Type: ${type}
|
|
From: ${email}
|
|
User ID: ${userId}
|
|
Workspace ID: ${workspaceId ?? 'N/A'}
|
|
Workflow ID: ${workflowId ?? 'N/A'}
|
|
Browser: ${userAgent ?? 'N/A'}
|
|
|
|
${message}
|
|
`
|
|
|
|
if (images.length > 0) {
|
|
emailText += `\n\n${images.length} image(s) attached.`
|
|
}
|
|
|
|
const emailResult = await sendEmail({
|
|
to: [getHelpEmailAddress()],
|
|
subject: `[${type.toUpperCase()}] ${subject}`,
|
|
text: emailText,
|
|
from: getFromEmailAddress(),
|
|
replyTo: email,
|
|
emailType: 'transactional',
|
|
attachments: images.map((image) => ({
|
|
filename: image.filename,
|
|
content: image.content.toString('base64'),
|
|
contentType: image.contentType,
|
|
disposition: 'attachment',
|
|
})),
|
|
})
|
|
|
|
if (!emailResult.success) {
|
|
logger.error(`[${requestId}] Error sending help request email`, emailResult.message)
|
|
return NextResponse.json({ error: 'Failed to send email' }, { status: 500 })
|
|
}
|
|
|
|
logger.info(`[${requestId}] Help request email sent successfully`)
|
|
|
|
try {
|
|
const confirmationHtml = await renderHelpConfirmationEmail(
|
|
type as 'bug' | 'feedback' | 'feature_request' | 'other',
|
|
images.length
|
|
)
|
|
|
|
await sendEmail({
|
|
to: [email],
|
|
subject: `Your ${type} request has been received: ${subject}`,
|
|
html: confirmationHtml,
|
|
from: getFromEmailAddress(),
|
|
replyTo: getHelpEmailAddress(),
|
|
emailType: 'transactional',
|
|
})
|
|
} catch (err) {
|
|
logger.warn(`[${requestId}] Failed to send confirmation email`, err)
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ success: true, message: 'Help request submitted successfully' },
|
|
{ status: 200 }
|
|
)
|
|
} catch (error) {
|
|
if (isPayloadSizeLimitError(error)) {
|
|
logger.warn(`[${requestId}] Help request form data too large`, { message: error.message })
|
|
return NextResponse.json(
|
|
{ error: `Request body exceeds the maximum allowed size of ${MAX_HELP_FORM_BYTES} bytes` },
|
|
{ status: 413 }
|
|
)
|
|
}
|
|
if (error instanceof Error && error.message.includes('not configured')) {
|
|
logger.error(`[${requestId}] Email service configuration error`, error)
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
'Email service configuration error. Please check your email service configuration.',
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
logger.error(`[${requestId}] Error processing help request`, error)
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
})
|