import { extractPullerResponseFilters, extractStepResponseFields, findConnection, searchPuller, } from '../../../helpers/profileGeneratorHelper'; import inquirer from 'inquirer'; import { getConnectorById, IConnection, } from '../../profile/generators/connections/connectors'; import { resolvedConnectorIdOf } from '@beehexa/hexasync-template-model'; export async function getTaskDataFromPuller(pullerName: string): Promise<{ connection: IConnection; puller: { id: string; resultKey: string }; pusher: null; // Always null for puller table: { columns: string[] }; }> { const puller = (await searchPuller(pullerName)) as any; if (!puller.pullSteps || !puller.pullSteps.length) { throw new Error('No pullSteps provided for the specified puller.'); } const primaryStep = puller.pullSteps.find( (step: any) => step.key === 'PULL_DATA', ); if (!primaryStep) { throw new Error('The PULL_DATA step is missing.'); } if (!primaryStep.connectorId) { throw new Error('The PULL_DATA step has no connection defined.'); } const connection = findConnection(primaryStep.connectorId); if (!connection) { throw new Error( `Connection ${primaryStep.connectorId} does not exist. Check your variables.`, ); } // FR-1 / AD-24: `connectorId` first, `providerId` second. Reading only the legacy spelling made a // connection converted by FR-3's quick fix hard-stop here with "Connector undefined not found". const connectorIdentity = resolvedConnectorIdOf(connection); const connector = connectorIdentity ? getConnectorById(connectorIdentity) : undefined; if (!connector) { throw new Error( `Connector ${connectorIdentity ?? "'connectorId'/'providerId' not set"} not found. Contact HexaSync Admin.`, ); } const responseFilters = extractPullerResponseFilters(puller); const { selectedFilter } = await inquirer.prompt([ { type: 'list', name: 'selectedFilter', message: 'Select a response filter:', choices: responseFilters.map((filter, index) => ({ name: `${index + 1}. ${filter}`, value: filter, })), }, ]); const responseFields = extractStepResponseFields( selectedFilter, primaryStep.displayType, primaryStep.data, ); const { selectedColumns } = await inquirer.prompt([ { type: 'checkbox', name: 'selectedColumns', message: 'Select columns for the task:', choices: responseFields.map((field) => ({ name: field, value: field, })), }, ]); if (selectedColumns.length === 0) { throw new Error('At least one column must be selected.'); } return { connection, puller: { id: puller.id, resultKey: selectedFilter }, pusher: null, table: { columns: selectedColumns }, }; }