n8n/packages/nodes-base/nodes/Workable/WorkableTrigger.node.ts

201 lines
5 KiB
TypeScript
Raw Normal View History

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
workableApiRequest,
} from './GenericFunctions';
import {
snakeCase,
} from 'change-case';
export class WorkableTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Workable Trigger',
name: 'workableTrigger',
icon: 'file:workable.png',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["triggerOn"]}}',
description: 'Starts the workflow when Workable events occur',
defaults: {
name: 'Workable Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'workableApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Trigger On',
name: 'triggerOn',
type: 'options',
options: [
{
name: 'Candidate Created',
value: 'candidateCreated',
},
{
name: 'Candidate Moved',
value: 'candidateMoved',
},
],
default: '',
required: true,
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
options: [
{
displayName: 'Job',
name: 'job',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getJobs',
},
default: '',
description: `Get notifications only for one job`,
},
{
displayName: 'Stage',
name: 'stage',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getStages',
},
default: '',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
description: 'Get notifications for specific stages. e.g. \'hired\'.',
},
],
},
],
};
methods = {
loadOptions: {
async getJobs(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { jobs } = await workableApiRequest.call(this, 'GET', '/jobs');
for (const job of jobs) {
returnData.push({
name: job.full_title,
value: job.shortcode,
});
}
return returnData;
},
async getStages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { stages } = await workableApiRequest.call(this, 'GET', '/stages');
for (const stage of stages) {
returnData.push({
name: stage.name,
value: stage.slug,
});
}
return returnData;
},
},
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
// Check all the webhooks which exist already if it is identical to the
// one that is supposed to get created.
const { subscriptions } = await workableApiRequest.call(this, 'GET', `/subscriptions`);
for (const subscription of subscriptions) {
if (subscription.target === webhookUrl) {
webhookData.webhookId = subscription.id as string;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const credentials = await this.getCredentials('workableApi') as { accessToken: string, subdomain: string };
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const triggerOn = this.getNodeParameter('triggerOn') as string;
const { stage, job } = this.getNodeParameter('filters') as IDataObject;
const endpoint = '/subscriptions';
const body: IDataObject = {
event: snakeCase(triggerOn).toLowerCase(),
args: {
account_id: credentials.subdomain,
...(job) && { job_shortcode: job },
...(stage) && { stage_slug: stage },
},
target: webhookUrl,
};
const responseData = await workableApiRequest.call(this, 'POST', endpoint, body);
if (responseData.id === undefined) {
// Required data is missing so was not successful
return false;
}
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 = `/subscriptions/${webhookData.webhookId}`;
try {
await workableApiRequest.call(this, 'DELETE', endpoint);
} catch (error) {
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 bodyData = this.getBodyData();
return {
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
}
}