n8n/packages/nodes-base/nodes/SurveyMonkey/SurveyMonkeyTrigger.node.ts

747 lines
20 KiB
TypeScript
Raw Normal View History

2020-04-26 10:51:20 -07:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
NodeApiError,
NodeOperationError,
2020-04-26 10:51:20 -07:00
} from 'n8n-workflow';
import {
idsExist,
surveyMonkeyApiRequest,
surveyMonkeyRequestAllItems,
} from './GenericFunctions';
2020-05-01 16:46:02 -07:00
import {
IAnswer,
IChoice,
IOther,
2020-05-01 16:46:02 -07:00
IQuestion,
IRow,
} from './Interfaces';
2020-04-26 10:51:20 -07:00
import {
createHmac,
2020-05-01 16:46:02 -07:00
} from 'crypto';
2020-04-26 10:51:20 -07:00
export class SurveyMonkeyTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'SurveyMonkey Trigger',
name: 'surveyMonkeyTrigger',
icon: 'file:surveyMonkey.svg',
2020-04-26 10:51:20 -07:00
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Survey Monkey events occur',
2020-04-26 10:51:20 -07:00
defaults: {
name: 'SurveyMonkey Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'surveyMonkeyApi',
required: true,
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'surveyMonkeyOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
2020-04-26 10:51:20 -07:00
},
],
webhooks: [
{
name: 'setup',
httpMethod: 'HEAD',
responseMode: 'onReceived',
path: 'webhook',
},
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'Method of authentication.',
},
2020-05-01 16:46:02 -07:00
{
displayName: 'Type',
name: 'objectType',
type: 'options',
options: [
{
name: 'Collector',
value: 'collector',
},
{
name: 'Survey',
value: 'survey',
},
],
default: '',
required: true,
},
2020-04-26 10:51:20 -07:00
{
displayName: 'Event',
name: 'event',
2020-05-01 16:46:02 -07:00
displayOptions: {
show: {
objectType: [
2020-10-22 06:46:03 -07:00
'survey',
2020-05-01 16:46:02 -07:00
],
},
},
2020-04-26 10:51:20 -07:00
type: 'options',
options: [
{
name: 'Collector Created',
value: 'collector_created',
description: 'A collector is created',
},
{
name: 'Collector Updated',
value: 'collector_updated',
description: 'A collector is updated',
},
{
name: 'Collector Deleted',
value: 'collector_deleted',
description: 'A collector is deleted',
},
{
name: 'Response Completed',
value: 'response_completed',
description: 'A survey response is completed',
},
{
name: 'Response Created',
value: 'response_created',
description: 'A respondent begins a survey',
},
{
name: 'Response Deleted',
value: 'response_deleted',
description: 'A response is deleted',
},
{
2020-05-01 16:46:02 -07:00
name: 'Response Disqualified',
value: 'response_disqualified',
2020-04-26 10:51:20 -07:00
description: 'A survey response is disqualified ',
},
{
name: 'Response Overquota',
value: 'response_overquota',
description: `A response is over a surveys quota`,
},
{
name: 'Response Updated',
value: 'response_updated',
description: 'A survey response is updated',
},
{
name: 'Survey Created',
value: 'survey_created',
description: 'A survey is created',
},
{
name: 'Survey Deleted',
value: 'survey_deleted',
description: 'A survey is deleted',
},
{
name: 'Survey Updated',
value: 'survey_updated',
description: 'A survey is updated',
},
],
default: '',
required: true,
},
{
2020-05-01 16:46:02 -07:00
displayName: 'Event',
name: 'event',
2020-04-26 10:51:20 -07:00
type: 'options',
2020-05-01 16:46:02 -07:00
displayOptions: {
show: {
objectType: [
'collector',
],
},
},
2020-04-26 10:51:20 -07:00
options: [
{
2020-05-01 16:46:02 -07:00
name: 'Collector Updated',
value: 'collector_updated',
description: 'A collector is updated',
2020-04-26 10:51:20 -07:00
},
{
2020-05-01 16:46:02 -07:00
name: 'Collector Deleted',
value: 'collector_deleted',
description: 'A collector is deleted',
},
{
name: 'Response Completed',
value: 'response_completed',
description: 'A survey response is completed',
},
{
name: 'Response Created',
value: 'response_created',
description: 'A respondent begins a survey',
},
{
name: 'Response Deleted',
value: 'response_deleted',
description: 'A response is deleted',
},
{
name: 'Response Disqualified',
value: 'response_disqualified',
description: 'A survey response is disqualified ',
},
{
name: 'Response Overquota',
value: 'response_overquota',
description: `A response is over a surveys quota`,
},
{
name: 'Response Updated',
value: 'response_updated',
description: 'A survey response is updated',
2020-04-26 10:51:20 -07:00
},
],
default: '',
required: true,
},
{
displayName: 'Survey IDs',
name: 'surveyIds',
type: 'multiOptions',
displayOptions: {
show: {
objectType: [
'survey',
],
},
hide: {
event: [
'survey_created',
],
},
},
typeOptions: {
loadOptionsMethod: 'getSurveys',
},
options: [],
default: [],
required: true,
},
{
displayName: 'Survey ID',
name: 'surveyId',
type: 'options',
displayOptions: {
show: {
objectType: [
'collector',
],
},
},
typeOptions: {
loadOptionsMethod: 'getSurveys',
},
default: [],
required: true,
},
{
displayName: 'Collector IDs',
name: 'collectorIds',
type: 'multiOptions',
displayOptions: {
show: {
objectType: [
'collector',
],
},
},
typeOptions: {
loadOptionsMethod: 'getCollectors',
loadOptionsDependsOn: [
'surveyId',
],
},
options: [],
default: [],
required: true,
},
{
displayName: 'Resolve Data',
name: 'resolveData',
type: 'boolean',
displayOptions: {
show: {
event: [
'response_completed',
],
},
},
default: true,
:zap: Remove unnessasry <br/> (#2340) * introduce analytics * add user survey backend * add user survey backend * set answers on survey submit Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> * change name to personalization * lint Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> * N8n 2495 add personalization modal (#2280) * update modals * add onboarding modal * implement questions * introduce analytics * simplify impl * implement survey handling * add personalized cateogry * update modal behavior * add thank you view * handle empty cases * rename modal * standarize modal names * update image, add tags to headings * remove unused file * remove unused interfaces * clean up footer spacing * introduce analytics * refactor to fix bug * update endpoint * set min height * update stories * update naming from questions to survey * remove spacing after core categories * fix bug in logic * sort nodes * rename types * merge with be * rename userSurvey * clean up rest api * use constants for keys * use survey keys * clean up types * move personalization to its own file Co-authored-by: ahsan-virani <ahsan.virani@gmail.com> * update parameter inputs to be multiline * update spacing * Survey new options (#2300) * split up options * fix quotes * remove unused import * refactor node credentials * add user created workflow event (#2301) * update multi params * simplify env vars * fix versionCli on FE * update personalization env * clean up node detail settings * fix event User opened Credentials panel * fix font sizes across modals * clean up input spacing * fix select modal spacing * increase spacing * fix input copy * fix webhook, tab spacing, retry button * fix button sizes * fix button size * add mini xlarge sizes * fix webhook spacing * fix nodes panel event * fix workflow id in workflow execute event * improve telemetry error logging * fix config and stop process events * add flush call on n8n stop * ready for release * fix input error highlighting * revert change * update toggle spacing * fix delete positioning * keep tooltip while focused * set strict size * increase left spacing * fix sort icons * remove unnessasry <br/> * remove unnessary break * remove unnessary margin * clean unused functionality * remove unnessary css * remove duplicate tracking * only show tooltip when hovering over label * remove extra space * add br * remove extra space * clean up commas * clean up commas * remove extra space * remove extra space * rewrite desc * add commas * add space * remove extra space * add space * add dot * update credentials section * use includes Co-authored-by: ahsan-virani <ahsan.virani@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-10-27 13:00:13 -07:00
description: 'By default the webhook-data only contain the IDs. If this option gets activated, it will resolve the data automatically.',
2020-04-26 10:51:20 -07:00
},
2020-05-01 16:46:02 -07:00
{
displayName: 'Only Answers',
name: 'onlyAnswers',
displayOptions: {
show: {
resolveData: [
true,
],
event: [
'response_completed',
],
},
},
type: 'boolean',
default: true,
description: 'Returns only the answers of the form and not any of the other data.',
},
2020-04-26 10:51:20 -07:00
],
};
methods = {
loadOptions: {
// Get all the survey's collectors to display them to user so that he can
// select them easily
async getCollectors(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const surveyId = this.getCurrentNodeParameter('surveyId');
const returnData: INodePropertyOptions[] = [];
const collectors = await surveyMonkeyRequestAllItems.call(this, 'data', 'GET', `/surveys/${surveyId}/collectors`);
for (const collector of collectors) {
const collectorName = collector.name;
const collectorId = collector.id;
returnData.push({
name: collectorName,
value: collectorId,
});
}
return returnData;
},
// Get all the surveys to display them to user so that he can
// select them easily
async getSurveys(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const surveys = await surveyMonkeyRequestAllItems.call(this, 'data', 'GET', '/surveys');
for (const survey of surveys) {
const surveyName = survey.title;
const surveyId = survey.id;
returnData.push({
name: surveyName,
value: surveyId,
});
}
return returnData;
},
},
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const objectType = this.getNodeParameter('objectType') as string;
const event = this.getNodeParameter('event') as string;
// Check all the webhooks which exist already if it is identical to the
// one that is supposed to get created.
const endpoint = '/webhooks';
const responseData = await surveyMonkeyRequestAllItems.call(this, 'data', 'GET', endpoint, {});
const webhookUrl = this.getNodeWebhookUrl('default');
const ids: string[] = [];
2020-05-01 16:46:02 -07:00
if (objectType === 'survey' && event !== 'survey_created') {
2020-04-26 10:51:20 -07:00
const surveyIds = this.getNodeParameter('surveyIds') as string[];
ids.push.apply(ids, surveyIds);
} else if (objectType === 'collector') {
const collectorIds = this.getNodeParameter('collectorIds') as string[];
ids.push.apply(ids, collectorIds);
}
for (const webhook of responseData) {
const webhookDetails = await surveyMonkeyApiRequest.call(this, 'GET', `/webhooks/${webhook.id}`);
if (webhookDetails.subscription_url === webhookUrl
&& idsExist(webhookDetails.object_ids as string[], ids as string[])
&& webhookDetails.event_type === event) {
// Set webhook-id to be sure that it can be deleted
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id as string;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const event = this.getNodeParameter('event') as string;
const objectType = this.getNodeParameter('objectType') as string;
const endpoint = '/webhooks';
const ids: string[] = [];
2020-05-01 16:46:02 -07:00
if (objectType === 'survey' && event !== 'survey_created') {
2020-04-26 10:51:20 -07:00
const surveyIds = this.getNodeParameter('surveyIds') as string[];
ids.push.apply(ids, surveyIds);
} else if (objectType === 'collector') {
const collectorIds = this.getNodeParameter('collectorIds') as string[];
ids.push.apply(ids, collectorIds);
}
const body: IDataObject = {
name: `n8n - Webhook [${event}]`,
object_type: objectType,
object_ids: ids,
subscription_url: webhookUrl,
event_type: event,
};
if (objectType === 'survey' && event === 'survey_created') {
delete body.object_type;
2020-05-01 16:46:02 -07:00
delete body.object_ids;
2020-04-26 10:51:20 -07:00
}
let responseData: IDataObject = {};
responseData = await surveyMonkeyApiRequest.call(this, 'POST', endpoint, body);
if (responseData.id === undefined) {
// Required data is missing so was not successful
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = responseData.id as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
const endpoint = `/webhooks/${webhookData.webhookId}`;
try {
await surveyMonkeyApiRequest.call(this, 'DELETE', endpoint);
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
} catch (error) {
2020-04-26 10:51:20 -07:00
return false;
}
// Remove from the static workflow data so that it is clear
// that no webhooks are registred anymore
delete webhookData.webhookId;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const event = this.getNodeParameter('event') as string;
const objectType = this.getNodeParameter('objectType') as string;
const authenticationMethod = this.getNodeParameter('authentication') as string;
let credentials : IDataObject;
2020-04-26 10:51:20 -07:00
const headerData = this.getHeaderData() as IDataObject;
const req = this.getRequestObject();
const webhookName = this.getWebhookName();
if (authenticationMethod === 'accessToken') {
credentials = await this.getCredentials('surveyMonkeyApi');
} else {
credentials = await this.getCredentials('surveyMonkeyOAuth2Api');
}
2020-04-26 10:51:20 -07:00
if (webhookName === 'setup') {
// It is a create webhook confirmation request
return {};
}
if (headerData['sm-signature'] === undefined) {
return {};
}
return new Promise((resolve, reject) => {
const data: Buffer[] = [];
req.on('data', (chunk) => {
data.push(chunk);
});
req.on('end', async () => {
const computedSignature = createHmac('sha1', `${credentials.clientId}&${credentials.clientSecret}`).update(data.join('')).digest('base64');
if (headerData['sm-signature'] !== computedSignature) {
// Signature is not valid so ignore call
return {};
}
let responseData = JSON.parse(data.join(''));
let endpoint = '';
2020-05-01 16:46:02 -07:00
let returnItem: INodeExecutionData[] = [
{
json: responseData,
2020-10-22 06:46:03 -07:00
},
2020-05-01 16:46:02 -07:00
];
2020-04-26 10:51:20 -07:00
if (event === 'response_completed') {
const resolveData = this.getNodeParameter('resolveData') as boolean;
if (resolveData) {
if (objectType === 'survey') {
endpoint = `/surveys/${responseData.resources.survey_id}/responses/${responseData.object_id}/details`;
} else {
endpoint = `/collectors/${responseData.resources.collector_id}/responses/${responseData.object_id}/details`;
}
responseData = await surveyMonkeyApiRequest.call(this, 'GET', endpoint);
2020-05-01 16:46:02 -07:00
const surveyId = responseData.survey_id;
const questions: IQuestion[] = [];
const answers = new Map<string, IAnswer[]>();
const { pages } = await surveyMonkeyApiRequest.call(this, 'GET', `/surveys/${surveyId}/details`);
for (const page of pages) {
questions.push.apply(questions, page.questions);
}
for (const page of responseData.pages as IDataObject[]) {
for (const question of page.questions as IDataObject[]) {
answers.set(question.id as string, question.answers as IAnswer[]);
}
}
const responseQuestions = new Map<string, number | string | string[] | IDataObject>();
for (const question of questions) {
/*
2020-05-02 03:30:23 -07:00
TODO: add support for premium components
2020-05-01 16:46:02 -07:00
- File Upload
- Matrix of dropdowm menus
*/
// if question does not have an answer ignore it
if (!answers.get(question.id)) {
continue;
}
const heading = question.headings![0].heading as string;
if (question.family === 'open_ended' || question.family === 'datetime') {
if (question.subtype !== 'multi') {
responseQuestions.set(heading, answers.get(question.id)![0].text as string);
} else {
const results: IDataObject = {};
const keys = (question.answers.rows as IRow[]).map(e => e.text) as string[];
const values = answers.get(question.id)?.map(e => e.text) as string[];
for (let i = 0; i < keys.length; i++) {
// if for some reason there are questions texts repeted add the index to the key
if (results[keys[i]] !== undefined) {
results[`${keys[i]}(${i})`] = values[i] || '';
} else {
results[keys[i]] = values[i] || '';
}
}
responseQuestions.set(heading, results);
}
}
if (question.family === 'single_choice') {
const other = question.answers.other as IOther;
if (other && other.visible && other.is_answer_choice && answers.get(question.id)![0].other_id) {
responseQuestions.set(heading, answers.get(question.id)![0].text as string);
} else if (other && other.visible && !other.is_answer_choice){
const choiceId = answers.get(question.id)![0].choice_id;
const choice = (question.answers.choices as IChoice[])
.filter(e => e.id === choiceId)[0];
const comment = answers.get(question.id)
?.find(e => e.other_id === other.id)?.text as string;
responseQuestions.set(heading, { value: choice.text, comment });
} else {
const choiceId = answers.get(question.id)![0].choice_id;
const choice = (question.answers.choices as IChoice[])
.filter(e => e.id === choiceId)[0];
responseQuestions.set(heading, choice.text);
}
}
if (question.family === 'multiple_choice') {
const other = question.answers.other as IOther;
const choiceIds = answers.get(question.id)?.map((e) => e.choice_id);
const value = (question.answers.choices as IChoice[])
.filter(e => choiceIds?.includes(e.id))
.map(e => e.text) as string[];
// if "Add an "Other" Answer Option for Comments" is active and was selected
if (other && other.is_answer_choice && other.visible) {
const text = answers.get(question.id)
?.find(e => e.other_id === other.id)?.text as string;
value.push(text);
}
responseQuestions.set(heading, value);
}
if (question.family === 'matrix') {
// if more than one row it's a matrix/rating-scale
const rows = question.answers.rows as IRow[];
if (rows.length > 1) {
const results: IDataObject = {};
const choiceIds = answers.get(question.id)?.map(e => e.choice_id) as string[];
const rowIds = answers.get(question.id)?.map(e => e.row_id) as string[];
const rowsValues = (question.answers.rows as IRow[])
.filter(e => rowIds!.includes(e.id as string))
.map(e => e.text);
const choicesValues = (question.answers.choices as IChoice[])
.filter(e => choiceIds!.includes(e.id as string))
.map(e => e.text);
for (let i = 0; i < rowsValues.length; i++) {
results[rowsValues[i]] = choicesValues[i] || '';
}
// add the rows that were not answered
for (const row of question.answers.rows as IDataObject[]) {
if (!rowIds.includes(row.id as string)) {
results[row.text as string] = '';
}
}
// the comment then add the comment
const other = question.answers.other as IOther;
if (other !== undefined && other.visible) {
results.comment = answers.get(question.id)?.filter((e) => e.other_id)[0].text;
}
responseQuestions.set(heading, results);
} else {
const choiceIds = answers.get(question.id)?.map((e) => e.choice_id);
const value = (question.answers.choices as IChoice[])
.filter(e => choiceIds!.includes(e.id as string))
.map(e => (e.text === '') ? e.weight : e.text)[0];
responseQuestions.set(heading, value);
// if "Add an Other Answer Option for Comments" is active then add comment to the answer
const other = question.answers.other as IOther;
if (other !== undefined && other.visible) {
const response: IDataObject = {};
//const questionName = (question.answers.other as IOther).text as string;
const text = answers.get(question.id)?.filter((e) => e.other_id)[0].text;
response.value = value;
response.comment = text;
responseQuestions.set(heading, response);
}
}
}
if (question.family === 'demographic') {
const rows: IDataObject = {};
for (const row of answers.get(question.id) as IAnswer[]) {
rows[row.row_id as string] = row.text;
}
const addressInfo: IDataObject = {};
for (const answer of question.answers.rows as IDataObject[]) {
addressInfo[answer.type as string] = rows[answer.id as string] || '';
}
responseQuestions.set(heading, addressInfo);
}
if (question.family === 'presentation') {
if (question.subtype === 'image') {
const { url } = question.headings![0].image as IDataObject;
responseQuestions.set(heading, url as string);
}
}
}
delete responseData.pages;
responseData.questions = {};
// Map the "Map" to JSON
const tuples = JSON.parse(JSON.stringify([...responseQuestions]));
for (const [key, value] of tuples) {
responseData.questions[key] = value;
}
const onlyAnswers = this.getNodeParameter('onlyAnswers') as boolean;
if (onlyAnswers) {
responseData = responseData.questions;
}
returnItem = [
{
json: responseData,
2020-10-22 06:46:03 -07:00
},
2020-05-01 16:46:02 -07:00
];
2020-04-26 10:51:20 -07:00
}
}
return resolve({
workflowData: [
2020-05-01 16:46:02 -07:00
returnItem,
2020-04-26 10:51:20 -07:00
],
});
});
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
req.on('error', (error) => {
throw new NodeOperationError(this.getNode(), error);
2020-04-26 10:51:20 -07:00
});
});
}
}