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

798 lines
25 KiB
TypeScript
Raw Normal View History

import { IExecuteFunctions } from 'n8n-core';
2020-01-20 19:00:27 -08:00
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
NodeOperationError,
2020-01-20 19:00:27 -08:00
} from 'n8n-workflow';
import {
salesmateApiRequest,
salesmateApiRequestAllItems,
simplifySalesmateData,
2020-01-20 19:00:27 -08:00
validateJSON,
} from './GenericFunctions';
import { companyFields, companyOperations } from './CompanyDescription';
import { activityFields, activityOperations } from './ActivityDescription';
import { ICompany } from './CompanyInterface';
import { IActivity } from './ActivityInterface';
import { IDeal } from './DealInterface';
import { dealFields, dealOperations } from './DealDescription';
2020-01-20 19:00:27 -08:00
export class Salesmate implements INodeType {
description: INodeTypeDescription = {
displayName: 'Salesmate',
name: 'salesmate',
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3534) * :zap: Update `lintfix` script * :zap: Run baseline `lintfix` * :fire: Remove unneeded exceptions (#3538) * :fire: Remove exceptions for `node-param-default-wrong-for-simplify` * :fire: Remove exceptions for `node-param-placeholder-miscased-id` * :zap: Update version * :shirt: Apply `node-param-placeholder-missing` (#3542) * :shirt: Apply `filesystem-wrong-cred-filename` (#3543) * :shirt: Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-class-description-empty-string` (#3546) * :shirt: Apply `node-class-description-icon-not-svg` (#3548) * :shirt: Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Expand lintings to credentials (#3550) * :shirt: Apply `node-param-multi-options-type-unsorted-items` (#3552) * :zap: fix * :zap: Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * :zap: Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * :zap: Fix with updated linting rules * :zap: Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-boolean-without-whether` (#3553) * :zap: fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * :shirt: Add exceptions * :shirt: Add exception * :pencil2: Alphabetize rules * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-01-20 19:00:27 -08:00
icon: 'file:salesmate.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
description: 'Consume Salesmate API',
defaults: {
name: 'Salesmate',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'salesmateApi',
required: true,
2020-10-22 06:46:03 -07:00
},
2020-01-20 19:00:27 -08:00
],
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,
2020-01-20 19:00:27 -08:00
options: [
{
name: 'Activity',
value: 'activity',
},
2020-01-20 19:00:27 -08:00
{
name: 'Company',
value: 'company',
},
{
name: 'Deal',
value: 'deal',
},
2020-01-20 19:00:27 -08:00
],
default: 'activity',
2020-01-20 19:00:27 -08:00
},
...companyOperations,
...activityOperations,
...dealOperations,
2020-01-20 19:00:27 -08:00
...companyFields,
...activityFields,
...dealFields,
2020-01-20 19:00:27 -08:00
],
};
methods = {
loadOptions: {
// Get all the available users to display them to user so that he can
// select them easily
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const users = await salesmateApiRequest.call(this, 'GET', '/v1/users/active');
for (const user of users.Data) {
const userName = user.nickname;
const userId = user.id;
returnData.push({
name: userName,
value: userId,
});
}
return returnData;
},
// Get all the available contacs to display them to user so that he can
// select them easily
async getContacts(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const qs: IDataObject = {
fields: ['name', 'id'],
2020-10-22 06:46:03 -07:00
query: {},
};
const contacts = await salesmateApiRequest.call(this, 'POST', '/v2/contacts/search', qs);
for (const contact of contacts.Data.data) {
const contactName = contact.name;
const contactId = contact.id;
returnData.push({
name: contactName,
value: contactId,
});
}
return returnData;
},
// Get all the available companies to display them to user so that he can
// select them easily
async getCompanies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const qs: IDataObject = {
fields: ['name', 'id'],
2020-10-22 06:46:03 -07:00
query: {},
};
const companies = await salesmateApiRequest.call(this, 'POST', '/v2/companies/search', qs);
for (const company of companies.Data.data) {
const companyName = company.name;
const companyId = company.id;
returnData.push({
name: companyName,
value: companyId,
});
}
return returnData;
},
2020-01-20 19:00:27 -08:00
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
const length = items.length;
2020-01-20 19:00:27 -08:00
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'company') {
if (operation === 'create') {
const owner = this.getNodeParameter('owner', i) as number;
const name = this.getNodeParameter('name', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: ICompany = {
name,
owner,
2020-01-20 19:00:27 -08:00
};
if (additionalFields.website) {
body.website = additionalFields.website as string;
}
if (additionalFields.phone) {
body.phone = additionalFields.phone as string;
}
if (additionalFields.otherPhone) {
body.otherPhone = additionalFields.otherPhone as string;
}
if (additionalFields.facebookHandle) {
body.facebookHandle = additionalFields.facebookHandle as string;
}
if (additionalFields.googlePlusHandle) {
body.googlePlusHandle = additionalFields.googlePlusHandle as string;
}
if (additionalFields.linkedInHandle) {
body.linkedInHandle = additionalFields.linkedInHandle as string;
}
if (additionalFields.skypeId) {
body.skypeId = additionalFields.skypeId as string;
}
if (additionalFields.twitterHandle) {
body.twitterHandle = additionalFields.twitterHandle as string;
}
if (additionalFields.currency) {
body.currency = additionalFields.currency as string;
}
if (additionalFields.billingAddressLine1) {
body.billingAddressLine1 = additionalFields.billingAddressLine1 as string;
}
if (additionalFields.billingAddressLine2) {
body.billingAddressLine2 = additionalFields.billingAddressLine2 as string;
}
if (additionalFields.billingCity) {
body.billingCity = additionalFields.billingCity as string;
}
if (additionalFields.billingZipCode) {
body.billingZipCode = additionalFields.billingZipCode as string;
}
if (additionalFields.billingState) {
body.billingState = additionalFields.billingState as string;
}
if (additionalFields.description) {
body.description = additionalFields.description as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string;
}
responseData = await salesmateApiRequest.call(this, 'POST', '/v1/companies', body);
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'update') {
const companyId = this.getNodeParameter('id', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const body: ICompany = {};
if (updateFields.owner) {
body.owner = updateFields.owner as number;
}
if (updateFields.name) {
body.name = updateFields.name as string;
}
if (updateFields.website) {
body.website = updateFields.website as string;
}
if (updateFields.phone) {
body.phone = updateFields.phone as string;
}
if (updateFields.otherPhone) {
body.otherPhone = updateFields.otherPhone as string;
}
if (updateFields.facebookHandle) {
body.facebookHandle = updateFields.facebookHandle as string;
}
if (updateFields.googlePlusHandle) {
body.googlePlusHandle = updateFields.googlePlusHandle as string;
}
if (updateFields.linkedInHandle) {
body.linkedInHandle = updateFields.linkedInHandle as string;
}
if (updateFields.skypeId) {
body.skypeId = updateFields.skypeId as string;
}
if (updateFields.twitterHandle) {
body.twitterHandle = updateFields.twitterHandle as string;
}
if (updateFields.currency) {
body.currency = updateFields.currency as string;
}
if (updateFields.billingAddressLine1) {
body.billingAddressLine1 = updateFields.billingAddressLine1 as string;
}
if (updateFields.billingAddressLine2) {
body.billingAddressLine2 = updateFields.billingAddressLine2 as string;
}
if (updateFields.billingCity) {
body.billingCity = updateFields.billingCity as string;
}
if (updateFields.billingZipCode) {
body.billingZipCode = updateFields.billingZipCode as string;
}
if (updateFields.billingState) {
body.billingState = updateFields.billingState as string;
}
if (updateFields.description) {
body.description = updateFields.description as string;
}
if (updateFields.tags) {
body.tags = updateFields.tags as string;
}
responseData = await salesmateApiRequest.call(
this,
'PUT',
`/v1/companies/${companyId}`,
body,
);
2020-01-20 19:00:27 -08:00
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'get') {
const companyId = this.getNodeParameter('id', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
responseData = await salesmateApiRequest.call(this, 'GET', `/v1/companies/${companyId}`);
responseData = responseData.Data;
2020-01-20 19:00:27 -08:00
if (!rawData) {
responseData = simplifySalesmateData(responseData);
2020-01-20 19:00:27 -08:00
}
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
let body: IDataObject = {
query: {
group: {},
2020-01-20 19:00:27 -08:00
},
};
if (options.sortBy) {
qs.sortBy = options.sortBy as string;
}
if (options.sortOrder) {
qs.sortOrder = options.sortOrder as string;
}
if (options.fields) {
if ((options.fields as string).trim() === '') {
throw new NodeOperationError(this.getNode(), 'You have to add at least one field', {
itemIndex: i,
});
}
2020-01-20 19:00:27 -08:00
body.fields = (options.fields as string).split(',') as string[];
} else {
body.fields = [
'name',
'description',
'billingAddressLine1',
'billingAddressLine2',
'billingCity',
'billingZipCode',
'billingState',
'billingCountry',
'website',
'owner',
'tags',
'photo',
'createdAt',
];
2020-01-20 19:00:27 -08:00
}
if (!jsonActive) {
const filters: IDataObject[] = [];
const filtersUi = (this.getNodeParameter('filters', i) as IDataObject)
.filtersUi as IDataObject;
if (filtersUi && filtersUi.conditions) {
2020-01-20 19:00:27 -08:00
const conditions = filtersUi.conditions as IDataObject;
if (conditions.conditionsUi) {
for (const condition of conditions.conditionsUi as IDataObject[]) {
const filter: IDataObject = {};
filter.moduleName = 'Company';
filter.field = {
fieldName: condition.field,
};
filter.condition = condition.condition;
filter.data = condition.value;
filters.push(filter);
2020-01-20 19:00:27 -08:00
}
}
}
if (filtersUi && filtersUi.operator) {
//@ts-ignore
body.query.group = {
operator: filtersUi.operator,
rules: filters,
};
}
2020-01-20 19:00:27 -08:00
} else {
const json = validateJSON(this.getNodeParameter('filtersJson', i) as string);
body = json;
}
if (returnAll) {
responseData = await salesmateApiRequestAllItems.call(
this,
'Data',
'POST',
'/v2/companies/search',
body,
qs,
);
2020-01-20 19:00:27 -08:00
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.rows = limit;
responseData = await salesmateApiRequest.call(
this,
'POST',
'/v2/companies/search',
body,
qs,
);
2020-01-20 19:00:27 -08:00
responseData = responseData.Data.data;
}
}
if (operation === 'delete') {
const companyId = parseInt(this.getNodeParameter('id', i) as string, 10);
responseData = await salesmateApiRequest.call(
this,
'DELETE',
`/v1/companies/${companyId}`,
);
2020-01-20 19:00:27 -08:00
}
}
if (resource === 'activity') {
if (operation === 'create') {
const owner = this.getNodeParameter('owner', i) as number;
const title = this.getNodeParameter('title', i) as string;
const type = this.getNodeParameter('type', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IActivity = {
title,
owner,
type,
};
if (additionalFields.dueDate) {
body.dueDate = new Date(additionalFields.dueDate as string).getTime();
}
if (additionalFields.duration) {
body.duration = additionalFields.duration as number;
}
if (additionalFields.isCalendarInvite) {
body.isCalendarInvite = additionalFields.isCalendarInvite as boolean;
}
if (additionalFields.isCompleted) {
body.isCompleted = additionalFields.isCompleted as boolean;
}
if (additionalFields.description) {
body.description = additionalFields.description as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string;
}
responseData = await salesmateApiRequest.call(this, 'POST', '/v1/activities', body);
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'update') {
const activityId = this.getNodeParameter('id', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: IActivity = {};
if (updateFields.title) {
body.title = updateFields.title as string;
}
if (updateFields.type) {
body.type = updateFields.type as string;
}
if (updateFields.owner) {
body.owner = updateFields.owner as number;
}
if (updateFields.dueDate) {
body.dueDate = new Date(updateFields.dueDate as string).getTime();
}
if (updateFields.duration) {
body.duration = updateFields.duration as number;
}
if (updateFields.isCalendarInvite) {
body.isCalendarInvite = updateFields.isCalendarInvite as boolean;
}
if (updateFields.isCompleted) {
body.isCompleted = updateFields.isCompleted as boolean;
}
if (updateFields.description) {
body.description = updateFields.description as string;
}
if (updateFields.tags) {
body.tags = updateFields.tags as string;
}
responseData = await salesmateApiRequest.call(
this,
'PUT',
`/v1/activities/${activityId}`,
body,
);
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'get') {
const activityId = this.getNodeParameter('id', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
responseData = await salesmateApiRequest.call(
this,
'GET',
`/v1/activities/${activityId}`,
);
responseData = responseData.Data;
if (!rawData) {
responseData = simplifySalesmateData(responseData);
}
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
let body: IDataObject = {
query: {
group: {},
},
};
if (options.sortBy) {
qs.sortBy = options.sortBy as string;
}
if (options.sortOrder) {
qs.sortOrder = options.sortOrder as string;
}
if (options.fields) {
if ((options.fields as string).trim() === '') {
throw new NodeOperationError(this.getNode(), 'You have to add at least one field', {
itemIndex: i,
});
}
body.fields = (options.fields as string).split(',') as string[];
} else {
body.fields = [
'title',
'dueDate',
'description',
'duration',
'owner',
'Deal.title',
'PrimaryContact.name',
'PrimaryContact.email',
'PrimaryCompany.name',
'PrimaryCompany.email',
'tags',
'type',
'createdAt',
'isCompleted',
];
}
if (!jsonActive) {
const filters: IDataObject[] = [];
const filtersUi = (this.getNodeParameter('filters', i) as IDataObject)
.filtersUi as IDataObject;
if (filtersUi && filtersUi.conditions) {
const conditions = filtersUi.conditions as IDataObject;
if (conditions.conditionsUi) {
for (const condition of conditions.conditionsUi as IDataObject[]) {
const filter: IDataObject = {};
filter.moduleName = 'Task';
filter.field = {
fieldName: condition.field,
};
filter.condition = condition.condition;
filter.data = condition.value;
filters.push(filter);
}
}
}
if (filtersUi && filtersUi.operator) {
//@ts-ignore
body.query.group = {
operator: filtersUi.operator,
rules: filters,
};
}
} else {
const json = validateJSON(this.getNodeParameter('filtersJson', i) as string);
body = json;
}
if (returnAll) {
responseData = await salesmateApiRequestAllItems.call(
this,
'Data',
'POST',
'/v2/activities/search',
body,
qs,
);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.rows = limit;
responseData = await salesmateApiRequest.call(
this,
'POST',
'/v2/activities/search',
body,
qs,
);
responseData = responseData.Data.data;
}
}
if (operation === 'delete') {
const activityId = this.getNodeParameter('id', i) as string;
responseData = await salesmateApiRequest.call(
this,
'DELETE',
`/v1/activities/${activityId}`,
);
}
}
if (resource === 'deal') {
if (operation === 'create') {
const title = this.getNodeParameter('title', i) as string;
const owner = this.getNodeParameter('owner', i) as number;
const primaryContact = this.getNodeParameter('primaryContact', i) as number;
const pipeline = this.getNodeParameter('pipeline', i) as string;
const status = this.getNodeParameter('status', i) as string;
const stage = this.getNodeParameter('stage', i) as string;
const currency = this.getNodeParameter('currency', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDeal = {
title,
owner,
primaryContact,
pipeline,
status,
stage,
currency,
};
if (additionalFields.description) {
body.description = additionalFields.description as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string;
}
if (additionalFields.primaryCompany) {
body.primaryCompany = additionalFields.primaryCompany as number;
}
if (additionalFields.source) {
body.source = additionalFields.source as string;
}
if (additionalFields.estimatedCloseDate) {
body.estimatedCloseDate = additionalFields.estimatedCloseDate as string;
}
if (additionalFields.dealValue) {
body.dealValue = additionalFields.dealValue as number;
}
if (additionalFields.priority) {
body.priority = additionalFields.priority as string;
}
responseData = await salesmateApiRequest.call(this, 'POST', '/v1/deals', body);
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'update') {
const dealId = this.getNodeParameter('id', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: IDeal = {};
if (updateFields.title) {
body.title = updateFields.title as string;
}
if (updateFields.owner) {
body.owner = updateFields.owner as number;
}
if (updateFields.primaryContact) {
body.primaryContact = updateFields.primaryContact as number;
}
if (updateFields.status) {
body.status = updateFields.status as string;
}
if (updateFields.currency) {
body.currency = updateFields.currency as string;
}
if (updateFields.stage) {
body.stage = updateFields.stage as string;
}
if (updateFields.pipeline) {
body.pipeline = updateFields.pipeline as string;
}
if (updateFields.description) {
body.description = updateFields.description as string;
}
if (updateFields.tags) {
body.tags = updateFields.tags as string;
}
if (updateFields.primaryCompany) {
body.primaryCompany = updateFields.primaryCompany as number;
}
if (updateFields.source) {
body.source = updateFields.source as string;
}
if (updateFields.estimatedCloseDate) {
body.estimatedCloseDate = updateFields.estimatedCloseDate as string;
}
if (updateFields.dealValue) {
body.dealValue = updateFields.dealValue as number;
}
if (updateFields.priority) {
body.priority = updateFields.priority as string;
}
responseData = await salesmateApiRequest.call(this, 'PUT', `/v1/deals/${dealId}`, body);
responseData = responseData.Data;
if (!rawData) {
delete responseData.detail;
}
}
if (operation === 'get') {
const dealId = this.getNodeParameter('id', i) as string;
const rawData = this.getNodeParameter('rawData', i) as boolean;
responseData = await salesmateApiRequest.call(this, 'GET', `/v1/deals/${dealId}`);
responseData = responseData.Data;
if (!rawData) {
responseData = simplifySalesmateData(responseData);
}
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
let body: IDataObject = {
query: {
group: {},
},
};
if (options.sortBy) {
qs.sortBy = options.sortBy as string;
}
if (options.sortOrder) {
qs.sortOrder = options.sortOrder as string;
}
if (options.fields !== undefined) {
if ((options.fields as string).trim() === '') {
throw new NodeOperationError(this.getNode(), 'You have to add at least one field', {
itemIndex: i,
});
}
body.fields = (options.fields as string).split(',') as string[];
} else {
body.fields = [
'title',
'PrimaryContact.name',
'PrimaryContact.email',
'PrimaryCompany.name',
'PrimaryCompany.email',
'dealValue',
'priority',
'stage',
'status',
'owner',
'tags',
'createdAt',
];
}
if (!jsonActive) {
const filters: IDataObject[] = [];
const filtersUi = (this.getNodeParameter('filters', i) as IDataObject)
.filtersUi as IDataObject;
if (filtersUi && filtersUi.conditions) {
const conditions = filtersUi.conditions as IDataObject;
if (conditions.conditionsUi) {
for (const condition of conditions.conditionsUi as IDataObject[]) {
const filter: IDataObject = {};
filter.moduleName = 'Task';
filter.field = {
fieldName: condition.field,
};
filter.condition = condition.condition;
filter.data = condition.value;
filters.push(filter);
}
}
}
if (filtersUi && filtersUi.operator) {
//@ts-ignore
body.query.group = {
operator: filtersUi.operator,
rules: filters,
};
}
} else {
const json = validateJSON(this.getNodeParameter('filtersJson', i) as string);
body = json;
}
if (returnAll) {
responseData = await salesmateApiRequestAllItems.call(
this,
'Data',
'POST',
'/v2/deals/search',
body,
qs,
);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.rows = limit;
responseData = await salesmateApiRequest.call(
this,
'POST',
'/v2/deals/search',
body,
qs,
);
responseData = responseData.Data.data;
}
}
if (operation === 'delete') {
const dealId = this.getNodeParameter('id', i) as string;
responseData = await salesmateApiRequest.call(this, 'DELETE', `/v1/deals/${dealId}`);
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
2020-01-20 19:00:27 -08:00
}
return this.prepareOutputData(returnData);
2020-01-20 19:00:27 -08:00
}
}