UNPKG

n8n-nodes-referral-factory

Version:
229 lines 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferralFactoryTrigger = void 0; class ReferralFactoryTrigger { constructor() { this.description = { displayName: 'Referral Factory Trigger', name: 'referralFactoryTrigger', icon: 'file:logo.svg', group: ['trigger'], version: 1, subtitle: '={{Trigger On: $parameter["event"]}}', description: 'Get data from Referral Factory API', defaults: { name: 'Referral Factory Trigger', }, inputs: [], outputs: ["main"], credentials: [ { name: 'referralFactoryApi', required: true, }, ], properties: [ { displayName: 'Trigger On', name: 'event', type: 'options', options: [ { name: 'When a New User Joins Your Campaign', value: 'user.join', description: 'Triggers when a new user joins or is added to your campaign', }, { name: 'When a User Is Qualified', value: 'user.qualified', description: 'Triggers when a user is qualified in Referral Factory', }, { name: 'When a Reward Issued via N8n', value: 'reward_issued', description: 'Triggers when a reward issued in Referral Factory via n8n', }, ], required: true, default: 'user.join', }, { displayName: 'With Source Names or IDs', description: 'Choose user with which sources should be triggered. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', required: true, name: 'sources', type: 'multiOptions', typeOptions: { loadOptionsMethod: 'getUserSources', }, displayOptions: { show: { event: ['user.join'], }, }, default: ['Referred'], }, { displayName: 'Choose Your Campaign Name or ID', description: 'Select the campaign which was created in Referral Factory. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', name: 'campaign_id', type: 'options', required: true, default: '', displayOptions: { show: { event: ['user.join', 'user.qualified'], }, }, typeOptions: { loadOptionsMethod: 'getCampaigns', }, }, { displayName: 'Choose Your Reward Name or ID', description: 'Select the reward which was created in Referral Factory. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', name: 'reward_id', type: 'options', required: true, default: '', displayOptions: { show: { event: ['reward_issued'], }, }, typeOptions: { loadOptionsMethod: 'getRewards', }, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'referral-factory', }, ], }; this.webhookMethods = { default: { async checkExists() { const data = this.getWorkflowStaticData('node'); return !!data.webhookId; }, async create() { const credentials = await this.getCredentials('referralFactoryApi'); const baseUrl = credentials.baseUrl; const apiKey = credentials.apiKey; const webhookUrl = this.getNodeWebhookUrl('default'); const event = this.getNodeParameter('event'); let body = {}; let url = `${baseUrl}/webhooks`; let method = 'POST'; if (event === 'user.join') { body = { url: webhookUrl, event, sources: this.getNodeParameter('sources'), campaign_id: this.getNodeParameter('campaign_id'), }; } if (event === 'user.qualified') { body = { url: webhookUrl, event, campaign_id: this.getNodeParameter('campaign_id'), }; } if (event === 'reward_issued') { method = 'PUT'; url = `${baseUrl}/rewards/${this.getNodeParameter('reward_id')}`; body = { url: webhookUrl, event, reward_id: this.getNodeParameter('reward_id'), }; } const response = await this.helpers.requestWithAuthentication.call(this, 'referralFactoryApi', { method: method, url: url, headers: { Authorization: `Bearer ${apiKey}`, Accept: 'application/json', }, body: body, json: true, }); this.getWorkflowStaticData('node').webhookId = response.webhook.id; return true; }, async delete() { const credentials = await this.getCredentials('referralFactoryApi'); const baseUrl = credentials.baseUrl; const webhookId = this.getWorkflowStaticData('node').webhookId; if (!webhookId) return true; let url = `${baseUrl}/webhooks/${webhookId}`; if (this.getNodeParameter('event') === 'reward_issued') { url = `${baseUrl}/rewards/${this.getNodeParameter('reward_id')}`; } await this.helpers.requestWithAuthentication.call(this, 'referralFactoryApi', { method: 'DELETE', url: url, }); this.getWorkflowStaticData('node').webhookId = null; return true; }, }, }; this.methods = { loadOptions: { async getUserSources() { const credentials = await this.getCredentials('referralFactoryApi'); const response = await this.helpers.requestWithAuthentication.call(this, 'referralFactoryApi', { method: 'GET', url: `${credentials.baseUrl}/user-sources`, }); const parsed = JSON.parse(response); return parsed.sources.map((source) => ({ name: source.name, value: source.id, })); }, async getCampaigns() { const credentials = await this.getCredentials('referralFactoryApi'); const response = await this.helpers.requestWithAuthentication.call(this, 'referralFactoryApi', { method: 'GET', url: `${credentials.baseUrl}/campaigns`, }); const parsed = JSON.parse(response); return parsed.campaigns.map((campaign) => ({ name: campaign.name, value: campaign.id, })); }, async getRewards() { const credentials = await this.getCredentials('referralFactoryApi'); const response = await this.helpers.requestWithAuthentication.call(this, 'referralFactoryApi', { method: 'GET', url: `${credentials.baseUrl}/rewards`, }); const parsed = JSON.parse(response); return parsed.rewards.map((reward) => ({ name: reward.name, value: reward.id, })); }, }, }; } async webhook() { const body = this.getBodyData(); const workflowData = this.helpers.returnJsonArray([body]); return { workflowData: [workflowData], }; } } exports.ReferralFactoryTrigger = ReferralFactoryTrigger; //# sourceMappingURL=ReferralFactoryTrigger.node.js.map