n8n-nodes-rd-station-crm
Version:
n8n community node for RD Station CRM: API v1 (private token) and API v2 (OAuth2) — contacts, deals, organizations, tasks, notes, pipelines, stages, products, custom fields, sources, campaigns, loss reasons, users, teams and a real webhook trigger.
119 lines • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RdStationCrmTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const constants_1 = require("../RdStationCrm/helpers/constants");
const transport_1 = require("../RdStationCrm/transport");
class RdStationCrmTrigger {
constructor() {
this.description = {
displayName: 'RD Station CRM Trigger',
name: 'rdStationCrmTrigger',
icon: { light: 'file:rdStationCrm.svg', dark: 'file:rdStationCrm.dark.svg' },
group: ['trigger'],
version: 1,
subtitle: '=Events: {{$parameter["events"].join(", ")}}',
description: 'Starts the workflow when RD Station CRM events happen',
defaults: {
name: 'RD Station CRM Trigger',
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
credentials: [
{
name: 'rdStationCrmApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Trigger On',
name: 'events',
type: 'multiOptions',
required: true,
default: [],
description: 'The RD Station CRM events to subscribe to (one webhook is registered per event)',
options: constants_1.RD_CRM_WEBHOOK_EVENTS,
},
],
};
this.webhookMethods = {
default: {
async checkExists() {
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events');
const staticData = this.getWorkflowStaticData('node');
const response = await transport_1.rdCrmApiRequest.call(this, 'GET', '/webhooks');
const existing = (response?.webhooks ?? response ?? []);
const registered = [];
for (const event of events) {
const match = existing.find((w) => w.url === webhookUrl && w.event_type === event);
if (!match) {
return false;
}
registered.push({ id: match.uuid, event });
}
staticData.webhooks = registered;
return true;
},
async create() {
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events');
const staticData = this.getWorkflowStaticData('node');
// Re-read existing webhooks so we don't create duplicates.
const response = await transport_1.rdCrmApiRequest.call(this, 'GET', '/webhooks');
const existing = (response?.webhooks ?? response ?? []);
const registered = [];
for (const event of events) {
const match = existing.find((w) => w.url === webhookUrl && w.event_type === event);
if (match) {
registered.push({ id: match.uuid, event });
continue;
}
const created = await transport_1.rdCrmApiRequest.call(this, 'POST', '/webhooks', {
event_type: event,
url: webhookUrl,
http_method: 'POST',
});
if (created?.uuid === undefined) {
return false;
}
registered.push({ id: created.uuid, event });
}
staticData.webhooks = registered;
return true;
},
async delete() {
const staticData = this.getWorkflowStaticData('node');
const registered = staticData.webhooks ?? [];
for (const webhook of registered) {
try {
await transport_1.rdCrmApiRequest.call(this, 'DELETE', `/webhooks/${webhook.id}`);
}
catch {
// The webhook may already be gone; keep removing the rest.
}
}
delete staticData.webhooks;
return true;
},
},
};
}
async webhook() {
const bodyData = this.getBodyData();
return {
workflowData: [this.helpers.returnJsonArray(bodyData)],
};
}
}
exports.RdStationCrmTrigger = RdStationCrmTrigger;
//# sourceMappingURL=RdStationCrmTrigger.node.js.map