n8n-nodes-cleverflow
Version:
n8n node to integrate with CleverFlow API for creating workflow runs and processing Tally Forms
141 lines • 7.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const apiHelpers_1 = require("../helpers/apiHelpers");
const tallyHelpers_1 = require("../helpers/tallyHelpers");
async function execute(index) {
var _a;
const workspaceName = this.getNodeParameter('workspaceId', index);
const workflowKey = this.getNodeParameter('workflowId', index);
let runName = this.getNodeParameter('runName', index, 'Tally Form Submission');
const inputType = this.getNodeParameter('inputType', index);
const skipInvalidFields = this.getNodeParameter('skipInvalidFields', index, true);
const skipEmptyValues = this.getNodeParameter('skipEmptyValues', index, true);
const items = this.getInputData();
const item = items[index];
let webhookData;
if (inputType === 'direct') {
webhookData = item.json;
}
else {
const jsonField = this.getNodeParameter('jsonField', index);
webhookData = item.json[jsonField];
}
let fields = [];
try {
if (!webhookData || typeof webhookData !== 'object') {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid webhook data received' });
}
const bodyData = webhookData.body;
if (!bodyData) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Missing body data in webhook' });
}
if ((bodyData === null || bodyData === void 0 ? void 0 : bodyData.eventType) === 'FORM_RESPONSE' && (bodyData === null || bodyData === void 0 ? void 0 : bodyData.data)) {
const formData = bodyData.data;
if (!formData.fields || !Array.isArray(formData.fields)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid or missing form fields data' });
}
const formName = formData.formName || 'Tally Form';
const responseId = formData.responseId || '';
if (!runName || runName === 'Tally Form Submission') {
runName = `${formName} - ${responseId}`;
}
try {
const tallyFields = formData.fields;
const nameField = tallyFields.find(field => /name|full\s*name|first\s*name/i.test(field.label) && field.value);
const emailField = tallyFields.find(field => /email|e-?mail/i.test(field.label) && field.value);
const companyField = tallyFields.find(field => /company|organization|business/i.test(field.label) && field.value);
if ((nameField || emailField || companyField) && runName === `${formName} - ${responseId}`) {
const nameParts = [];
if (companyField && companyField.value) {
nameParts.push(companyField.value.toString());
}
if (nameField && nameField.value) {
nameParts.push(nameField.value.toString());
}
else if (emailField && emailField.value) {
nameParts.push(emailField.value.toString());
}
if (nameParts.length > 0) {
runName = `${formName} - ${nameParts.join(' - ')}`;
}
}
const credentials = await this.getCredentials('cleverFlowApi');
const baseUrl = credentials.baseUrl ? `${credentials.baseUrl}/api` : 'https://api.cleverflow.com/api';
const timestamp = Date.now();
const workspaceUrl = `/workspace?_nocache=${timestamp}`;
const workspaces = await this.helpers.httpRequest({
method: 'GET',
url: workspaceUrl,
baseURL: baseUrl,
headers: {
Authorization: `Token ${credentials.apiToken}`,
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
},
});
const workspace = workspaces.find((ws) => ws.name === workspaceName);
if (!workspace) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Workspace not found' });
}
const workflowsUrl = `/workspace/${workspace.id}/workflows/?_nocache=${timestamp}`;
const workflows = await this.helpers.httpRequest({
method: 'GET',
url: workflowsUrl,
baseURL: baseUrl,
headers: {
Authorization: `Token ${credentials.apiToken}`,
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
},
});
const workflow = workflows === null || workflows === void 0 ? void 0 : workflows.find((wf) => wf.key === workflowKey);
if (!workflow) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Workflow not found' });
}
const workflowFields = [];
if ((_a = workflow.form) === null || _a === void 0 ? void 0 : _a.sections) {
for (const section of workflow.form.sections) {
if (section.fields_set) {
for (const field of section.fields_set) {
workflowFields.push({
name: field.label,
value: field.slug || field.label,
});
}
}
}
}
if (!workflowFields.length) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'No workflow fields found in CleverFlow' });
}
fields = await (0, tallyHelpers_1.processTallyFormFields)(tallyFields, workflowFields, skipEmptyValues, skipInvalidFields, this.getNode());
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Error processing form data: ${error.message}` });
}
}
else {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid webhook data format' });
}
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Failed to process Tally form: ${error.message}`
});
}
const body = {
workspace: workspaceName,
workflow_key: workflowKey,
data: fields,
};
if (runName) {
body.name = runName;
}
return (0, apiHelpers_1.makeApiRequest)(this, 'POST', '/public/create_run', body);
}
exports.execute = execute;
//# sourceMappingURL=processTallyForm.js.map