mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 21:49:41 -08:00
6dcdb30bf4
* ✏️ Alphabetize lint rules * 🔥 Remove duplicates * ⚡ Update `lintfix` script * 👕 Apply `node-param-operation-without-no-data-expression` (#3329) * 👕 Apply `node-param-operation-without-no-data-expression` * 👕 Add exceptions * 👕 Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-value-duplicate` (#3331) * 👕 Apply `node-param-description-miscased-json` (#3337) * 👕 Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * ✏️ Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-description-wrong-for-simplify` (#3334) * ⚡ fix * ⚡ exceptions * ⚡ changed rule ignoring from file to line * 👕 Apply `node-param-resource-without-no-data-expression` (#3339) * 👕 Apply `node-param-display-name-untrimmed` (#3341) * 👕 Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-resource-with-plural-option` (#3342) * 👕 Apply `node-param-description-wrong-for-upsert` (#3333) * ⚡ fix * ⚡ replaced record with contact in description * ⚡ fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-description-identical-to-name` (#3343) * 👕 Apply `node-param-option-name-containing-star` (#3347) * 👕 Apply `node-param-display-name-wrong-for-update-fields` (#3348) * 👕 Apply `node-param-option-name-wrong-for-get-all` (#3345) * ⚡ fix * ⚡ exceptions * 👕 Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * 👕 Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * ⚡ Alphabetize lint rules * ⚡ Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
434 lines
11 KiB
TypeScript
434 lines
11 KiB
TypeScript
import {
|
|
IExecuteFunctions
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
JsonObject
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
emeliaApiTest,
|
|
emeliaGraphqlRequest,
|
|
loadResource,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
campaignFields,
|
|
campaignOperations,
|
|
} from './CampaignDescription';
|
|
|
|
import {
|
|
contactListFields,
|
|
contactListOperations,
|
|
} from './ContactListDescription';
|
|
|
|
import {
|
|
isEmpty,
|
|
} from 'lodash';
|
|
|
|
export class Emelia implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Emelia',
|
|
name: 'emelia',
|
|
icon: 'file:emelia.svg',
|
|
group: ['input'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume the Emelia API',
|
|
defaults: {
|
|
name: 'Emelia',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'emeliaApi',
|
|
required: true,
|
|
testedBy: 'emeliaApiTest',
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Campaign',
|
|
value: 'campaign',
|
|
},
|
|
{
|
|
name: 'Contact List',
|
|
value: 'contactList',
|
|
},
|
|
],
|
|
default: 'campaign',
|
|
required: true,
|
|
},
|
|
...campaignOperations,
|
|
...campaignFields,
|
|
...contactListOperations,
|
|
...contactListFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
credentialTest: {
|
|
emeliaApiTest,
|
|
},
|
|
|
|
loadOptions: {
|
|
async getCampaigns(this: ILoadOptionsFunctions) {
|
|
return loadResource.call(this, 'campaign');
|
|
},
|
|
|
|
async getContactLists(this: ILoadOptionsFunctions) {
|
|
return loadResource.call(this, 'contactList');
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'campaign') {
|
|
|
|
// **********************************
|
|
// campaign
|
|
// **********************************
|
|
|
|
if (operation === 'addContact') {
|
|
|
|
// ----------------------------------
|
|
// campaign: addContact
|
|
// ----------------------------------
|
|
|
|
const contact = {
|
|
email: this.getNodeParameter('contactEmail', i) as string,
|
|
};
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
if (!isEmpty(additionalFields)) {
|
|
Object.assign(contact, additionalFields);
|
|
}
|
|
|
|
if (additionalFields.customFieldsUi) {
|
|
const customFields = (additionalFields.customFieldsUi as IDataObject || {}).customFieldsValues as IDataObject[] || [];
|
|
const data = customFields.reduce((obj, value) => Object.assign(obj, { [`${value.fieldName}`]: value.value }), {});
|
|
Object.assign(contact, data);
|
|
//@ts-ignore
|
|
delete contact.customFieldsUi;
|
|
}
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
mutation AddContactToCampaignHook($id: ID!, $contact: JSON!) {
|
|
addContactToCampaignHook(id: $id, contact: $contact)
|
|
}`,
|
|
operationName: 'AddContactToCampaignHook',
|
|
variables: {
|
|
id: this.getNodeParameter('campaignId', i),
|
|
contact,
|
|
},
|
|
});
|
|
|
|
returnData.push({ contactId: responseData.data.addContactToCampaignHook });
|
|
|
|
} else if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// campaign: create
|
|
// ----------------------------------
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
operationName: 'createCampaign',
|
|
query: `
|
|
mutation createCampaign($name: String!) {
|
|
createCampaign(name: $name) {
|
|
_id
|
|
name
|
|
status
|
|
createdAt
|
|
provider
|
|
startAt
|
|
estimatedEnd
|
|
}
|
|
}`,
|
|
variables: {
|
|
name: this.getNodeParameter('campaignName', i),
|
|
},
|
|
});
|
|
|
|
returnData.push(responseData.data.createCampaign);
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// campaign: get
|
|
// ----------------------------------
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
query campaign($id: ID!){
|
|
campaign(id: $id){
|
|
_id
|
|
name
|
|
status
|
|
createdAt
|
|
schedule{
|
|
dailyContact
|
|
dailyLimit
|
|
minInterval
|
|
maxInterval
|
|
trackLinks
|
|
trackOpens
|
|
timeZone
|
|
days
|
|
start
|
|
end
|
|
eventToStopMails
|
|
}
|
|
provider
|
|
startAt
|
|
recipients{
|
|
total_count
|
|
}
|
|
estimatedEnd
|
|
}
|
|
}`,
|
|
operationName: 'campaign',
|
|
variables: {
|
|
id: this.getNodeParameter('campaignId', i),
|
|
},
|
|
});
|
|
|
|
returnData.push(responseData.data.campaign);
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
// campaign: getAll
|
|
// ----------------------------------
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
query all_campaigns {
|
|
all_campaigns {
|
|
_id
|
|
name
|
|
status
|
|
createdAt
|
|
stats {
|
|
mailsSent
|
|
uniqueOpensPercent
|
|
opens
|
|
linkClickedPercent
|
|
repliedPercent
|
|
bouncedPercent
|
|
unsubscribePercent
|
|
progressPercent
|
|
}
|
|
}
|
|
}`,
|
|
operationName: 'all_campaigns',
|
|
});
|
|
|
|
let campaigns = responseData.data.all_campaigns;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
campaigns = campaigns.slice(0, limit);
|
|
}
|
|
|
|
returnData.push(...campaigns);
|
|
|
|
} else if (operation === 'pause') {
|
|
|
|
// ----------------------------------
|
|
// campaign: pause
|
|
// ----------------------------------
|
|
|
|
await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
mutation pauseCampaign($id: ID!) {
|
|
pauseCampaign(id: $id)
|
|
}`,
|
|
operationName: 'pauseCampaign',
|
|
variables: {
|
|
id: this.getNodeParameter('campaignId', i),
|
|
},
|
|
});
|
|
|
|
returnData.push({ success: true });
|
|
|
|
} else if (operation === 'start') {
|
|
|
|
// ----------------------------------
|
|
// campaign: start
|
|
// ----------------------------------
|
|
|
|
await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
mutation startCampaign($id: ID!) {
|
|
startCampaign(id: $id)
|
|
}`,
|
|
operationName: 'startCampaign',
|
|
variables: {
|
|
id: this.getNodeParameter('campaignId', i),
|
|
},
|
|
});
|
|
|
|
returnData.push({ success: true });
|
|
|
|
} else if (operation === 'duplicate') {
|
|
|
|
// ----------------------------------
|
|
// campaign: duplicate
|
|
// ----------------------------------
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const variables = {
|
|
fromId: this.getNodeParameter('campaignId', i),
|
|
name: this.getNodeParameter('campaignName', i),
|
|
copySettings: true,
|
|
copyMails: true,
|
|
copyContacts: false,
|
|
copyProvider: true,
|
|
...options,
|
|
};
|
|
const { data: { duplicateCampaign } } = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
mutation duplicateCampaign(
|
|
$fromId: ID!
|
|
$name: String!
|
|
$copySettings: Boolean!
|
|
$copyMails: Boolean!
|
|
$copyContacts: Boolean!
|
|
$copyProvider: Boolean!
|
|
) {
|
|
duplicateCampaign(
|
|
fromId: $fromId
|
|
name: $name
|
|
copySettings: $copySettings
|
|
copyMails: $copyMails
|
|
copyContacts: $copyContacts
|
|
copyProvider: $copyProvider
|
|
)
|
|
}`,
|
|
operationName: 'duplicateCampaign',
|
|
variables,
|
|
});
|
|
|
|
returnData.push({ _id: duplicateCampaign });
|
|
}
|
|
|
|
} else if (resource === 'contactList') {
|
|
|
|
// **********************************
|
|
// ContactList
|
|
// **********************************
|
|
|
|
if (operation === 'add') {
|
|
|
|
// ----------------------------------
|
|
// contactList: add
|
|
// ----------------------------------
|
|
|
|
const contact = {
|
|
email: this.getNodeParameter('contactEmail', i) as string,
|
|
};
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
if (!isEmpty(additionalFields)) {
|
|
Object.assign(contact, additionalFields);
|
|
}
|
|
|
|
if (additionalFields.customFieldsUi) {
|
|
const customFields = (additionalFields.customFieldsUi as IDataObject || {}).customFieldsValues as IDataObject[] || [];
|
|
const data = customFields.reduce((obj, value) => Object.assign(obj, { [`${value.fieldName}`]: value.value }), {});
|
|
Object.assign(contact, data);
|
|
//@ts-ignore
|
|
delete contact.customFieldsUi;
|
|
}
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
mutation AddContactsToListHook($id: ID!, $contact: JSON!) {
|
|
addContactsToListHook(id: $id, contact: $contact)
|
|
}`,
|
|
operationName: 'AddContactsToListHook',
|
|
variables: {
|
|
id: this.getNodeParameter('contactListId', i),
|
|
contact,
|
|
},
|
|
});
|
|
|
|
returnData.push({ contactId: responseData.data.addContactsToListHook });
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
// contactList: getAll
|
|
// ----------------------------------
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, {
|
|
query: `
|
|
query contact_lists{
|
|
contact_lists{
|
|
_id
|
|
name
|
|
contactCount
|
|
fields
|
|
usedInCampaign
|
|
}
|
|
}`,
|
|
operationName: 'contact_lists',
|
|
});
|
|
|
|
let contactLists = responseData.data.contact_lists;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
contactLists = contactLists.slice(0, limit);
|
|
}
|
|
|
|
returnData.push(...contactLists);
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: (error as JsonObject).message });
|
|
continue;
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
}
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|