n8n/packages/nodes-base/nodes/Lemlist/Lemlist.node.ts

337 lines
8.9 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
activityFields,
activityOperations,
campaignFields,
campaignOperations,
leadFields,
leadOperations,
teamFields,
teamOperations,
unsubscribeFields,
unsubscribeOperations,
} from './descriptions';
import {
lemlistApiRequest,
lemlistApiRequestAllItems,
} from './GenericFunctions';
import {
isEmpty,
omit,
} from 'lodash';
export class Lemlist implements INodeType {
description: INodeTypeDescription = {
displayName: 'Lemlist',
name: 'lemlist',
icon: 'file:lemlist.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the Lemlist API',
defaults: {
name: 'Lemlist',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'lemlistApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
refactor: Apply more nodelinting rules (#3324) * :pencil2: Alphabetize lint rules * :fire: Remove duplicates * :zap: Update `lintfix` script * :shirt: Apply `node-param-operation-without-no-data-expression` (#3329) * :shirt: Apply `node-param-operation-without-no-data-expression` * :shirt: Add exceptions * :shirt: Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-value-duplicate` (#3331) * :shirt: Apply `node-param-description-miscased-json` (#3337) * :shirt: Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * :pencil2: Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-simplify` (#3334) * :zap: fix * :zap: exceptions * :zap: changed rule ignoring from file to line * :shirt: Apply `node-param-resource-without-no-data-expression` (#3339) * :shirt: Apply `node-param-display-name-untrimmed` (#3341) * :shirt: Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-resource-with-plural-option` (#3342) * :shirt: Apply `node-param-description-wrong-for-upsert` (#3333) * :zap: fix * :zap: replaced record with contact in description * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-description-identical-to-name` (#3343) * :shirt: Apply `node-param-option-name-containing-star` (#3347) * :shirt: Apply `node-param-display-name-wrong-for-update-fields` (#3348) * :shirt: Apply `node-param-option-name-wrong-for-get-all` (#3345) * :zap: fix * :zap: exceptions * :shirt: Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * :shirt: Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Alphabetize lint rules * :zap: 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>
2022-05-20 14:47:24 -07:00
noDataExpression: true,
options: [
{
name: 'Activity',
value: 'activity',
},
{
name: 'Campaign',
value: 'campaign',
},
{
name: 'Lead',
value: 'lead',
},
{
name: 'Team',
value: 'team',
},
{
refactor: Apply more nodelinting rules (#3324) * :pencil2: Alphabetize lint rules * :fire: Remove duplicates * :zap: Update `lintfix` script * :shirt: Apply `node-param-operation-without-no-data-expression` (#3329) * :shirt: Apply `node-param-operation-without-no-data-expression` * :shirt: Add exceptions * :shirt: Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-value-duplicate` (#3331) * :shirt: Apply `node-param-description-miscased-json` (#3337) * :shirt: Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * :pencil2: Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-simplify` (#3334) * :zap: fix * :zap: exceptions * :zap: changed rule ignoring from file to line * :shirt: Apply `node-param-resource-without-no-data-expression` (#3339) * :shirt: Apply `node-param-display-name-untrimmed` (#3341) * :shirt: Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-resource-with-plural-option` (#3342) * :shirt: Apply `node-param-description-wrong-for-upsert` (#3333) * :zap: fix * :zap: replaced record with contact in description * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-description-identical-to-name` (#3343) * :shirt: Apply `node-param-option-name-containing-star` (#3347) * :shirt: Apply `node-param-display-name-wrong-for-update-fields` (#3348) * :shirt: Apply `node-param-option-name-wrong-for-get-all` (#3345) * :zap: fix * :zap: exceptions * :shirt: Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * :shirt: Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Alphabetize lint rules * :zap: 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>
2022-05-20 14:47:24 -07:00
name: 'Unsubscribe',
value: 'unsubscribe',
},
],
default: 'activity',
},
...activityOperations,
...activityFields,
...campaignOperations,
...campaignFields,
...leadOperations,
...leadFields,
...teamOperations,
...teamFields,
...unsubscribeOperations,
...unsubscribeFields,
],
};
methods = {
loadOptions: {
async getCampaigns(this: ILoadOptionsFunctions) {
const campaigns = await lemlistApiRequest.call(this, 'GET', '/campaigns');
return campaigns.map(({ _id, name }: { _id: string, name: string }) => ({
name,
value: _id,
}));
},
},
};
async execute(this: IExecuteFunctions) {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
const returnData: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'activity') {
// *********************************************************************
// activity
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// activity: getAll
// ----------------------------------
// https://developer.lemlist.com/#activities
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const qs = {} as IDataObject;
const filters = this.getNodeParameter('filters', i);
if (!isEmpty(filters)) {
Object.assign(qs, filters);
}
responseData = await lemlistApiRequest.call(this, 'GET', '/activities', {}, qs);
if (returnAll === false) {
const limit = this.getNodeParameter('limit', 0) as number;
responseData = responseData.slice(0, limit);
}
}
} else if (resource === 'campaign') {
// *********************************************************************
// campaign
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// campaign: getAll
// ----------------------------------
// https://developer.lemlist.com/#list-all-campaigns
responseData = await lemlistApiRequest.call(this, 'GET', '/campaigns');
const returnAll = this.getNodeParameter('returnAll', i);
if (!returnAll) {
const limit = this.getNodeParameter('limit', i);
responseData = responseData.slice(0, limit);
}
}
} else if (resource === 'lead') {
// *********************************************************************
// lead
// *********************************************************************
if (operation === 'create') {
// ----------------------------------
// lead: create
// ----------------------------------
// https://developer.lemlist.com/#add-a-lead-in-a-campaign
const qs = {} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.deduplicate !== undefined) {
qs.deduplicate = additionalFields.deduplicate;
}
const body = {} as IDataObject;
const remainingAdditionalFields = omit(additionalFields, 'deduplicate');
if (!isEmpty(remainingAdditionalFields)) {
Object.assign(body, remainingAdditionalFields);
}
const campaignId = this.getNodeParameter('campaignId', i);
const email = this.getNodeParameter('email', i);
const endpoint = `/campaigns/${campaignId}/leads/${email}`;
responseData = await lemlistApiRequest.call(this, 'POST', endpoint, body, qs);
} else if (operation === 'delete') {
// ----------------------------------
// lead: delete
// ----------------------------------
// https://developer.lemlist.com/#delete-a-lead-from-a-campaign
const campaignId = this.getNodeParameter('campaignId', i);
const email = this.getNodeParameter('email', i);
const endpoint = `/campaigns/${campaignId}/leads/${email}`;
responseData = await lemlistApiRequest.call(this, 'DELETE', endpoint, {}, { action: 'remove' });
} else if (operation === 'get') {
// ----------------------------------
// lead: get
// ----------------------------------
// https://developer.lemlist.com/#get-a-specific-lead-by-email
const email = this.getNodeParameter('email', i);
responseData = await lemlistApiRequest.call(this, 'GET', `/leads/${email}`);
} else if (operation === 'unsubscribe') {
// ----------------------------------
// lead: unsubscribe
// ----------------------------------
// https://developer.lemlist.com/#unsubscribe-a-lead-from-a-campaign
const campaignId = this.getNodeParameter('campaignId', i);
const email = this.getNodeParameter('email', i);
const endpoint = `/campaigns/${campaignId}/leads/${email}`;
responseData = await lemlistApiRequest.call(this, 'DELETE', endpoint);
}
} else if (resource === 'team') {
// *********************************************************************
// team
// *********************************************************************
if (operation === 'get') {
// ----------------------------------
// team: get
// ----------------------------------
// https://developer.lemlist.com/#team
responseData = await lemlistApiRequest.call(this, 'GET', '/team');
}
} else if (resource === 'unsubscribe') {
// *********************************************************************
// unsubscribe
// *********************************************************************
if (operation === 'add') {
// ----------------------------------
// unsubscribe: Add
// ----------------------------------
// https://developer.lemlist.com/#add-an-email-address-in-the-unsubscribes
const email = this.getNodeParameter('email', i);
responseData = await lemlistApiRequest.call(this, 'POST', `/unsubscribes/${email}`);
} else if (operation === 'delete') {
// ----------------------------------
// unsubscribe: delete
// ----------------------------------
// https://developer.lemlist.com/#delete-an-email-address-from-the-unsubscribes
const email = this.getNodeParameter('email', i);
responseData = await lemlistApiRequest.call(this, 'DELETE', `/unsubscribes/${email}`);
} else if (operation === 'getAll') {
// ----------------------------------
// unsubscribe: getAll
// ----------------------------------
// https://developer.lemlist.com/#list-all-unsubscribes
const returnAll = this.getNodeParameter('returnAll', i);
if (returnAll) {
responseData = await lemlistApiRequestAllItems.call(this, 'GET', '/unsubscribes');
} else {
const qs = {
limit: this.getNodeParameter('limit', i) as number,
};
responseData = await lemlistApiRequest.call(this, 'GET', '/unsubscribes', {}, qs);
}
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.toString() });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}