( apiBase: string, headers: Record<string, string>, params: Record<string, unknown>, userId: string, signal?: AbortSignal )
| 197 | }) |
| 198 | |
| 199 | async function handleSendEnvelope( |
| 200 | apiBase: string, |
| 201 | headers: Record<string, string>, |
| 202 | params: Record<string, unknown>, |
| 203 | userId: string, |
| 204 | signal?: AbortSignal |
| 205 | ) { |
| 206 | const { signerEmail, signerName, emailSubject, emailBody, ccEmail, ccName, file, status } = params |
| 207 | |
| 208 | if (!signerEmail || !signerName || !emailSubject) { |
| 209 | return NextResponse.json( |
| 210 | { success: false, error: 'signerEmail, signerName, and emailSubject are required' }, |
| 211 | { status: 400 } |
| 212 | ) |
| 213 | } |
| 214 | |
| 215 | let documentBase64 = '' |
| 216 | let documentName = 'document.pdf' |
| 217 | |
| 218 | if (file) { |
| 219 | try { |
| 220 | const parsed = FileInputSchema.parse(file) |
| 221 | const userFiles = processFilesToUserFiles([parsed as RawFileInput], 'docusign-send', logger) |
| 222 | if (userFiles.length > 0) { |
| 223 | const userFile = userFiles[0] |
| 224 | const denied = await assertToolFileAccess(userFile.key, userId, 'docusign-send', logger) |
| 225 | if (denied) return denied |
| 226 | if (userFile.size > MAX_DOCUSIGN_DOCUMENT_BYTES) { |
| 227 | return NextResponse.json( |
| 228 | { success: false, error: 'Document is too large to send through DocuSign' }, |
| 229 | { status: 413 } |
| 230 | ) |
| 231 | } |
| 232 | const { buffer } = await downloadServableFileFromStorage( |
| 233 | userFile, |
| 234 | 'docusign-send', |
| 235 | logger, |
| 236 | { |
| 237 | maxBytes: MAX_DOCUSIGN_DOCUMENT_BYTES, |
| 238 | } |
| 239 | ) |
| 240 | assertKnownSizeWithinLimit(buffer.length, MAX_DOCUSIGN_DOCUMENT_BYTES, 'DocuSign document') |
| 241 | documentBase64 = buffer.toString('base64') |
| 242 | documentName = userFile.name |
| 243 | } |
| 244 | } catch (fileError) { |
| 245 | const notReady = docNotReadyResponse(fileError) |
| 246 | if (notReady) return notReady |
| 247 | logger.error('Failed to process file for DocuSign envelope', { fileError }) |
| 248 | return NextResponse.json( |
| 249 | { |
| 250 | success: false, |
| 251 | error: isPayloadSizeLimitError(fileError) |
| 252 | ? getErrorMessage(fileError, 'Document is too large to send through DocuSign') |
| 253 | : 'Failed to process uploaded file', |
| 254 | }, |
| 255 | { status: isPayloadSizeLimitError(fileError) ? 413 : 400 } |
| 256 | ) |
no test coverage detected