2022-08-17 08:50:24 -07:00
|
|
|
|
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
2020-04-26 10:51:20 -07:00
|
|
|
|
|
|
|
|
|
import {
|
2022-10-21 08:24:58 -07:00
|
|
|
|
deepCopy,
|
2020-04-26 10:51:20 -07:00
|
|
|
|
IDataObject,
|
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodePropertyOptions,
|
|
|
|
|
INodeType,
|
|
|
|
|
INodeTypeDescription,
|
|
|
|
|
IWebhookResponseData,
|
2022-10-21 11:52:43 -07:00
|
|
|
|
jsonParse,
|
2021-04-16 09:33:36 -07:00
|
|
|
|
NodeOperationError,
|
2020-04-26 10:51:20 -07:00
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
import { idsExist, surveyMonkeyApiRequest, surveyMonkeyRequestAllItems } from './GenericFunctions';
|
2020-04-26 10:51:20 -07:00
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
import { IAnswer, IChoice, IOther, IQuestion, IRow } from './Interfaces';
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
import { createHmac } from 'crypto';
|
2020-04-26 10:51:20 -07:00
|
|
|
|
|
|
|
|
|
export class SurveyMonkeyTrigger implements INodeType {
|
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
|
displayName: 'SurveyMonkey Trigger',
|
|
|
|
|
name: 'surveyMonkeyTrigger',
|
2021-06-12 12:00:37 -07:00
|
|
|
|
icon: 'file:surveyMonkey.svg',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
group: ['trigger'],
|
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
|
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,
|
2020-06-10 03:48:21 -07:00
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
authentication: ['accessToken'],
|
2020-06-10 03:48:21 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'surveyMonkeyOAuth2Api',
|
|
|
|
|
required: true,
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
authentication: ['oAuth2'],
|
2020-06-10 03:48:21 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
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: [
|
2020-06-10 03:48:21 -07:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Authentication',
|
|
|
|
|
name: 'authentication',
|
|
|
|
|
type: 'options',
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Access Token',
|
|
|
|
|
value: 'accessToken',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'OAuth2',
|
|
|
|
|
value: 'oAuth2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: 'accessToken',
|
|
|
|
|
},
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
objectType: ['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 Deleted',
|
|
|
|
|
value: 'collector_deleted',
|
|
|
|
|
description: 'A collector is deleted',
|
|
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
|
{
|
|
|
|
|
name: 'Collector Updated',
|
|
|
|
|
value: 'collector_updated',
|
|
|
|
|
description: 'A collector is updated',
|
|
|
|
|
},
|
2020-04-26 10:51:20 -07:00
|
|
|
|
{
|
|
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'A survey response is disqualified',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Response Overquota',
|
|
|
|
|
value: 'response_overquota',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'A response is over a survey’s quota',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
objectType: ['collector'],
|
2020-05-01 16:46:02 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
2020-04-26 10:51:20 -07:00
|
|
|
|
options: [
|
|
|
|
|
{
|
2020-05-01 16:46:02 -07:00
|
|
|
|
name: 'Collector Deleted',
|
|
|
|
|
value: 'collector_deleted',
|
|
|
|
|
description: 'A collector is deleted',
|
|
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
|
{
|
|
|
|
|
name: 'Collector Updated',
|
|
|
|
|
value: 'collector_updated',
|
|
|
|
|
description: 'A collector is updated',
|
|
|
|
|
},
|
2020-05-01 16:46:02 -07:00
|
|
|
|
{
|
|
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'A survey response is disqualified',
|
2020-05-01 16:46:02 -07:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Response Overquota',
|
|
|
|
|
value: 'response_overquota',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'A response is over a survey’s quota',
|
2020-05-01 16:46:02 -07:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Response Updated',
|
|
|
|
|
value: 'response_updated',
|
|
|
|
|
description: 'A survey response is updated',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: '',
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-06-20 07:54:01 -07:00
|
|
|
|
displayName: 'Survey Names or IDs',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
name: 'surveyIds',
|
|
|
|
|
type: 'multiOptions',
|
2022-08-17 08:50:24 -07:00
|
|
|
|
description:
|
|
|
|
|
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
objectType: ['survey'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
hide: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
event: ['survey_created'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
typeOptions: {
|
|
|
|
|
loadOptionsMethod: 'getSurveys',
|
|
|
|
|
},
|
|
|
|
|
options: [],
|
|
|
|
|
default: [],
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
|
displayName: 'Survey Name or ID',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
name: 'surveyId',
|
|
|
|
|
type: 'options',
|
2022-08-17 08:50:24 -07:00
|
|
|
|
description:
|
|
|
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
objectType: ['collector'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
typeOptions: {
|
|
|
|
|
loadOptionsMethod: 'getSurveys',
|
|
|
|
|
},
|
|
|
|
|
default: [],
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-06-20 07:54:01 -07:00
|
|
|
|
displayName: 'Collector Names or IDs',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
name: 'collectorIds',
|
|
|
|
|
type: 'multiOptions',
|
2022-08-17 08:50:24 -07:00
|
|
|
|
description:
|
|
|
|
|
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-04-26 10:51:20 -07:00
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
objectType: ['collector'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
typeOptions: {
|
|
|
|
|
loadOptionsMethod: 'getCollectors',
|
2022-08-17 08:50:24 -07:00
|
|
|
|
loadOptionsDependsOn: ['surveyId'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
options: [],
|
|
|
|
|
default: [],
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Resolve Data',
|
|
|
|
|
name: 'resolveData',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
event: ['response_completed'],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: true,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
2022-08-17 08:50:24 -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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
resolveData: [true],
|
|
|
|
|
event: ['response_completed'],
|
2020-05-01 16:46:02 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: true,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
description: 'Whether to return only the answers of the form and not any of the other data',
|
2020-05-01 16:46:02 -07:00
|
|
|
|
},
|
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[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const collectors = await surveyMonkeyRequestAllItems.call(
|
|
|
|
|
this,
|
|
|
|
|
'data',
|
|
|
|
|
'GET',
|
|
|
|
|
`/surveys/${surveyId}/collectors`,
|
|
|
|
|
);
|
2020-04-26 10:51:20 -07:00
|
|
|
|
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';
|
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const responseData = await surveyMonkeyRequestAllItems.call(
|
|
|
|
|
this,
|
|
|
|
|
'data',
|
|
|
|
|
'GET',
|
|
|
|
|
endpoint,
|
|
|
|
|
{},
|
|
|
|
|
);
|
2020-04-26 10:51:20 -07:00
|
|
|
|
|
|
|
|
|
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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const webhookDetails = await surveyMonkeyApiRequest.call(
|
|
|
|
|
this,
|
|
|
|
|
'GET',
|
|
|
|
|
`/webhooks/${webhook.id}`,
|
|
|
|
|
);
|
|
|
|
|
if (
|
|
|
|
|
webhookDetails.subscription_url === webhookUrl &&
|
2022-12-02 12:54:28 -08:00
|
|
|
|
idsExist(webhookDetails.object_ids as string[], ids) &&
|
2022-08-17 08:50:24 -07:00
|
|
|
|
webhookDetails.event_type === event
|
|
|
|
|
) {
|
2020-04-26 10:51:20 -07:00
|
|
|
|
// 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);
|
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;
|
2020-06-10 03:48:21 -07:00
|
|
|
|
const authenticationMethod = this.getNodeParameter('authentication') as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
|
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();
|
|
|
|
|
|
2020-06-10 03:48:21 -07:00
|
|
|
|
if (authenticationMethod === 'accessToken') {
|
2022-04-14 23:00:47 -07:00
|
|
|
|
credentials = await this.getCredentials('surveyMonkeyApi');
|
2020-06-10 03:48:21 -07:00
|
|
|
|
} else {
|
2022-04-14 23:00:47 -07:00
|
|
|
|
credentials = await this.getCredentials('surveyMonkeyOAuth2Api');
|
2020-06-10 03:48:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
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 {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
|
return new Promise((resolve, _reject) => {
|
2020-04-26 10:51:20 -07:00
|
|
|
|
const data: Buffer[] = [];
|
|
|
|
|
|
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
|
data.push(chunk);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('end', async () => {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const computedSignature = createHmac(
|
|
|
|
|
'sha1',
|
|
|
|
|
`${credentials.clientId}&${credentials.clientSecret}`,
|
|
|
|
|
)
|
|
|
|
|
.update(data.join(''))
|
|
|
|
|
.digest('base64');
|
2020-04-26 10:51:20 -07:00
|
|
|
|
if (headerData['sm-signature'] !== computedSignature) {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
// Signature is not valid so ignore call
|
2020-04-26 10:51:20 -07:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 11:52:43 -07:00
|
|
|
|
let responseData = jsonParse<any>(data.join(''));
|
2020-04-26 10:51:20 -07:00
|
|
|
|
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[]>();
|
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const { pages } = await surveyMonkeyApiRequest.call(
|
|
|
|
|
this,
|
|
|
|
|
'GET',
|
|
|
|
|
`/surveys/${surveyId}/details`,
|
|
|
|
|
);
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
|
|
|
|
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 = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
const keys = (question.answers.rows as IRow[]).map((e) => e.text);
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const values = answers.get(question.id)?.map((e) => e.text) as string[];
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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;
|
2022-08-17 08:50:24 -07:00
|
|
|
|
if (
|
2022-12-02 12:54:28 -08:00
|
|
|
|
other?.visible &&
|
2022-08-17 08:50:24 -07:00
|
|
|
|
other.is_answer_choice &&
|
|
|
|
|
answers.get(question.id)![0].other_id
|
|
|
|
|
) {
|
2020-05-01 16:46:02 -07:00
|
|
|
|
responseQuestions.set(heading, answers.get(question.id)![0].text as string);
|
2022-12-02 12:54:28 -08:00
|
|
|
|
} else if (other?.visible && !other.is_answer_choice) {
|
2020-05-01 16:46:02 -07:00
|
|
|
|
const choiceId = answers.get(question.id)![0].choice_id;
|
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const choice = (question.answers.choices as IChoice[]).filter(
|
|
|
|
|
(e) => e.id === choiceId,
|
|
|
|
|
)[0];
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const comment = answers.get(question.id)?.find((e) => e.other_id === other.id)
|
|
|
|
|
?.text as string;
|
2020-05-01 16:46:02 -07:00
|
|
|
|
responseQuestions.set(heading, { value: choice.text, comment });
|
|
|
|
|
} else {
|
|
|
|
|
const choiceId = answers.get(question.id)![0].choice_id;
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const choice = (question.answers.choices as IChoice[]).filter(
|
|
|
|
|
(e) => e.id === choiceId,
|
|
|
|
|
)[0];
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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[])
|
2022-08-17 08:50:24 -07:00
|
|
|
|
.filter((e) => choiceIds?.includes(e.id))
|
2022-12-02 12:54:28 -08:00
|
|
|
|
.map((e) => e.text);
|
2020-05-01 16:46:02 -07:00
|
|
|
|
// if "Add an "Other" Answer Option for Comments" is active and was selected
|
2022-12-02 12:54:28 -08:00
|
|
|
|
if (other?.is_answer_choice && other.visible) {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const text = answers.get(question.id)?.find((e) => e.other_id === other.id)
|
|
|
|
|
?.text as string;
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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 = {};
|
2022-08-17 08:50:24 -07:00
|
|
|
|
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[];
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
|
|
|
|
const rowsValues = (question.answers.rows as IRow[])
|
2022-12-02 12:54:28 -08:00
|
|
|
|
.filter((e) => rowIds.includes(e.id))
|
2022-08-17 08:50:24 -07:00
|
|
|
|
.map((e) => e.text);
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
|
|
|
|
const choicesValues = (question.answers.choices as IChoice[])
|
2022-12-02 12:54:28 -08:00
|
|
|
|
.filter((e) => choiceIds.includes(e.id))
|
2022-08-17 08:50:24 -07:00
|
|
|
|
.map((e) => e.text);
|
2020-05-01 16:46:02 -07:00
|
|
|
|
|
|
|
|
|
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;
|
2022-12-02 12:54:28 -08:00
|
|
|
|
if (other?.visible) {
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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[])
|
2022-12-02 12:54:28 -08:00
|
|
|
|
.filter((e) => choiceIds!.includes(e.id))
|
2022-08-17 08:50:24 -07:00
|
|
|
|
.map((e) => (e.text === '' ? e.weight : e.text))[0];
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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;
|
2022-12-02 12:54:28 -08:00
|
|
|
|
if (other?.visible) {
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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[]) {
|
2023-01-19 04:37:19 -08:00
|
|
|
|
addressInfo[answer.type as string] = rows[answer.id as string] || '';
|
2020-05-01 16:46:02 -07:00
|
|
|
|
}
|
|
|
|
|
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
|
2022-10-21 08:24:58 -07:00
|
|
|
|
const tuples = deepCopy([...responseQuestions]);
|
2020-05-01 16:46:02 -07:00
|
|
|
|
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({
|
2022-08-17 08:50:24 -07:00
|
|
|
|
workflowData: [returnItem],
|
2020-04-26 10:51:20 -07:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|