mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 02:51:14 +00:00
✨ Mailjet node and trigger
This commit is contained in:
138
packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts
Normal file
138
packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import {
|
||||
IHookFunctions,
|
||||
IWebhookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
INodeTypeDescription,
|
||||
INodeType,
|
||||
IWebhookResponseData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
mailjetApiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class MailjetTrigger implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Mailjet Trigger',
|
||||
name: 'mailjetTrigger',
|
||||
icon: 'file:mailjet.png',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
description: 'Handle Mailjet events via webhooks',
|
||||
defaults: {
|
||||
name: 'Mailjet Trigger',
|
||||
color: '#ff9f48',
|
||||
},
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'mailjetEmailApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
httpMethod: 'POST',
|
||||
responseMode: 'onReceived',
|
||||
path: 'webhook',
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Event',
|
||||
name: 'event',
|
||||
type: 'options',
|
||||
required: true,
|
||||
default: 'open',
|
||||
options: [
|
||||
{
|
||||
name: 'email.bounce',
|
||||
value: 'bounce',
|
||||
},
|
||||
{
|
||||
name: 'email.blocked',
|
||||
value: 'blocked',
|
||||
},
|
||||
{
|
||||
name: 'email.open',
|
||||
value: 'open',
|
||||
},
|
||||
{
|
||||
name: 'email.spam',
|
||||
value: 'spam',
|
||||
},
|
||||
{
|
||||
name: 'email.sent',
|
||||
value: 'sent',
|
||||
},
|
||||
{
|
||||
name: 'email.unsub',
|
||||
value: 'unsub',
|
||||
},
|
||||
],
|
||||
description: 'Determines which resource events the webhook is triggered for.',
|
||||
},
|
||||
],
|
||||
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
webhookMethods = {
|
||||
default: {
|
||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
if (webhookData.webhookId === undefined) {
|
||||
return false;
|
||||
}
|
||||
const endpoint = `/v3/rest/eventcallbackurl/${webhookData.webhookId}`;
|
||||
try {
|
||||
await mailjetApiRequest.call(this, 'GET', endpoint);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
async create(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
const event = this.getNodeParameter('event') as string;
|
||||
const endpoint = '/v3/rest/eventcallbackurl';
|
||||
const body: IDataObject = {
|
||||
Url: webhookUrl,
|
||||
EventType: event,
|
||||
Status: 'alive',
|
||||
isBackup: 'false',
|
||||
};
|
||||
const { Data } = await mailjetApiRequest.call(this, 'POST', endpoint, body);
|
||||
webhookData.webhookId = Data[0].ID;
|
||||
return true;
|
||||
},
|
||||
async delete(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
const endpoint = `/v3/rest/eventcallbackurl/${webhookData.webhookId}`;
|
||||
try {
|
||||
await mailjetApiRequest.call(this, 'DELETE', endpoint);
|
||||
} catch(error) {
|
||||
return false;
|
||||
}
|
||||
delete webhookData.webhookId;
|
||||
return true;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
//@ts-ignore
|
||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
||||
const req = this.getRequestObject();
|
||||
return {
|
||||
workflowData: [
|
||||
this.helpers.returnJsonArray(req.body),
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user