* Resolves the user's DocuSign account info from their access token * by calling the DocuSign userinfo endpoint.
( accessToken: string, signal?: AbortSignal )
| 75 | * by calling the DocuSign userinfo endpoint. |
| 76 | */ |
| 77 | async function resolveAccount( |
| 78 | accessToken: string, |
| 79 | signal?: AbortSignal |
| 80 | ): Promise<DocuSignAccountInfo> { |
| 81 | const response = await fetchDocusign( |
| 82 | 'https://account-d.docusign.com/oauth/userinfo', |
| 83 | { |
| 84 | headers: { Authorization: `Bearer ${accessToken}` }, |
| 85 | }, |
| 86 | signal |
| 87 | ) |
| 88 | |
| 89 | if (!response.ok) { |
| 90 | const errorText = await readResponseTextWithLimit(response, { |
| 91 | maxBytes: DEFAULT_MAX_ERROR_BODY_BYTES, |
| 92 | label: 'DocuSign account error response', |
| 93 | }).catch(() => '') |
| 94 | logger.error('Failed to resolve DocuSign account', { |
| 95 | status: response.status, |
| 96 | error: errorText, |
| 97 | }) |
| 98 | throw new Error(`Failed to resolve DocuSign account: ${response.status}`) |
| 99 | } |
| 100 | |
| 101 | const data = await readDocusignJson(response, 'DocuSign account response') |
| 102 | const accounts = Array.isArray(data.accounts) |
| 103 | ? (data.accounts as Array<{ |
| 104 | is_default?: boolean |
| 105 | base_uri?: string |
| 106 | account_id?: string |
| 107 | }>) |
| 108 | : [] |
| 109 | |
| 110 | const defaultAccount = accounts.find((account) => account.is_default) ?? accounts[0] |
| 111 | if (!defaultAccount) { |
| 112 | throw new Error('No DocuSign accounts found for this user') |
| 113 | } |
| 114 | |
| 115 | const baseUri = defaultAccount.base_uri |
| 116 | if (!baseUri) { |
| 117 | throw new Error('DocuSign account is missing base_uri') |
| 118 | } |
| 119 | const accountId = defaultAccount.account_id |
| 120 | if (!accountId) { |
| 121 | throw new Error('DocuSign account is missing account_id') |
| 122 | } |
| 123 | |
| 124 | return { |
| 125 | accountId, |
| 126 | baseUri, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | export const POST = withRouteHandler(async (request: NextRequest) => { |
| 131 | const authResult = await checkInternalAuth(request, { requireWorkflowId: false }) |
no test coverage detected