This commit is contained in:
Ricardo Espinoza 2019-12-03 22:21:52 -05:00
parent cb0cfaa771
commit 67297944da
4 changed files with 121 additions and 106 deletions

View file

@ -36,8 +36,8 @@ export const dealFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Stages', displayName: 'Stage',
name: 'stages', name: 'stage',
type: 'options', type: 'options',
required: true, required: true,
typeOptions: { typeOptions: {
@ -107,8 +107,11 @@ export const dealFields = [
{ {
displayName: 'Deal Type', displayName: 'Deal Type',
name: 'dealType', name: 'dealType',
type: 'string', type: 'options',
default: '', typeOptions: {
loadOptionsMethod: 'getDealTypes',
},
default: [],
}, },
{ {
displayName: 'Associated Company', displayName: 'Associated Company',

View file

@ -0,0 +1,12 @@
import { IDataObject } from "n8n-workflow";
export interface IAssociation {
associatedCompanyIds?: number[];
associatedVids?: number[];
}
export interface IDeal {
association?: IAssociation;
properties?: IDataObject[];
}

View file

@ -10,7 +10,6 @@ import {
import { import {
IDataObject, IDataObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { response } from 'express';
export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('hubspotApi'); const credentials = this.getCredentials('hubspotApi');

View file

@ -18,6 +18,7 @@ import {
dealOperations, dealOperations,
dealFields, dealFields,
} from '../Hubspot/DealDescription'; } from '../Hubspot/DealDescription';
import { IDeal, IAssociation } from './DealInterface';
export class Hubspot implements INodeType { export class Hubspot implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -72,7 +73,6 @@ export class Hubspot implements INodeType {
const endpoint = '/crm-pipelines/v1/pipelines/deals'; const endpoint = '/crm-pipelines/v1/pipelines/deals';
stages = await hubspotApiRequest.call(this, 'GET', endpoint, {}); stages = await hubspotApiRequest.call(this, 'GET', endpoint, {});
stages = stages.results[0].stages; stages = stages.results[0].stages;
console.log(stages)
} catch (err) { } catch (err) {
throw new Error(`Hubspot Error: ${err}`); throw new Error(`Hubspot Error: ${err}`);
} }
@ -129,109 +129,110 @@ export class Hubspot implements INodeType {
}); });
} }
return returnData; return returnData;
} },
// Get all the deal types to display them to user so that he can
// select them easily
async getDealTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let dealTypes;
try {
const endpoint = '/properties/v1/deals/properties/named/dealtype';
dealTypes = await hubspotApiRequest.call(this, 'GET', endpoint);
} catch (err) {
throw new Error(`Hubspot Error: ${err}`);
}
for (const dealType of dealTypes.options) {
const dealTypeName = dealType.label ;
const dealTypeId = dealType.value;
returnData.push({
name: dealTypeName,
value: dealTypeId,
});
}
return returnData;
},
} }
}; };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
// const items = this.getInputData(); const items = this.getInputData();
// const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
// const length = items.length as unknown as number; const length = items.length as unknown as number;
// let responseData; let responseData;
// const qs: IDataObject = {}; const qs: IDataObject = {};
const resource = this.getNodeParameter('resource', 0) as string;
// const resource = this.getNodeParameter('resource', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
// const operation = this.getNodeParameter('operation', 0) as string; for (let i = 0; i < length; i++) {
if (resource === 'deal') {
// for (let i = 0; i < length; i++) { if (operation === 'create') {
// if (resource === 'payout') { const body: IDeal = {};
// if (operation === 'create') { const association: IAssociation = {};
// const body: IPaymentBatch = {}; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
// const header: ISenderBatchHeader = {}; const stage = this.getNodeParameter('stage', i) as string;
// const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean; if (stage) {
// const senderBatchId = this.getNodeParameter('senderBatchId', i) as string; // @ts-ignore
// const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; body.properties.push({
// header.sender_batch_id = senderBatchId; name: 'dealstage',
// if (additionalFields.emailSubject) { value: stage
// header.email_subject = additionalFields.emailSubject as string; });
// } }
// if (additionalFields.emailMessage) { if (additionalFields.associatedCompany) {
// header.email_message = additionalFields.emailMessage as string; association.associatedCompanyIds = additionalFields.associatedCompany as number[];
// } }
// if (additionalFields.note) { if (additionalFields.associatedVids) {
// header.note = additionalFields.note as string; association.associatedVids = additionalFields.associatedVids as number[];
// } }
// body.sender_batch_header = header; if (additionalFields.dealName) {
// if (!jsonActive) { // @ts-ignore
// const payoutItems: IItem[] = []; body.properties.push({
// const itemsValues = (this.getNodeParameter('itemsUi', i) as IDataObject).itemsValues as IDataObject[]; name: 'dealname',
// if (itemsValues && itemsValues.length > 0) { value: additionalFields.dealName as string
// itemsValues.forEach(o => { });
// const payoutItem: IItem = {}; }
// const amount: IAmount = {}; if (additionalFields.closeDate) {
// amount.currency = o.currency as string; // @ts-ignore
// amount.value = parseFloat(o.amount as string); body.properties.push({
// payoutItem.amount = amount; name: 'closedate',
// payoutItem.note = o.note as string || ''; value: new Date(additionalFields.closeDate as string).getTime()
// payoutItem.receiver = o.receiverValue as string; });
// payoutItem.recipient_type = o.recipientType as RecipientType; }
// payoutItem.recipient_wallet = o.recipientWallet as RecipientWallet; if (additionalFields.amount) {
// payoutItem.sender_item_id = o.senderItemId as string || ''; // @ts-ignore
// payoutItems.push(payoutItem); body.properties.push({
// }); name: 'amount',
// body.items = payoutItems; value: additionalFields.amount as string
// } else { });
// throw new Error('You must have at least one item.'); }
// } if (additionalFields.dealType) {
// } else { // @ts-ignore
// const itemsJson = validateJSON(this.getNodeParameter('itemsJson', i) as string); body.properties.push({
// body.items = itemsJson; name: 'dealtype',
// } value: additionalFields.dealType as string
// try { });
// responseData = await payPalApiRequest.call(this, '/payments/payouts', 'POST', body); }
// } catch (err) { if (additionalFields.pipeline) {
// throw new Error(`PayPal Error: ${JSON.stringify(err)}`); // @ts-ignore
// } body.properties.push({
// } name: 'pipeline',
// if (operation === 'get') { value: additionalFields.pipeline as string
// const payoutBatchId = this.getNodeParameter('payoutBatchId', i) as string; });
// const returnAll = this.getNodeParameter('returnAll', 0) as boolean; }
// try { body.association = association;
// if (returnAll === true) { try {
// responseData = await payPalApiRequestAllItems.call(this, 'items', `/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); const endpoint = '/deals/v1/deal';
// } else { responseData = await hubspotApiRequest.call(this, 'POST', endpoint, body);
// qs.page_size = this.getNodeParameter('limit', i) as number; } catch (err) {
// responseData = await payPalApiRequest.call(this, `/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); throw new Error(`Hubspot Error: ${JSON.stringify(err)}`);
// responseData = responseData.items; }
// } }
// } catch (err) { }
// throw new Error(`PayPal Error: ${JSON.stringify(err)}`); if (Array.isArray(responseData)) {
// } returnData.push.apply(returnData, responseData as IDataObject[]);
// } } else {
// } else if (resource === 'payoutItem') { returnData.push(responseData as IDataObject);
// if (operation === 'get') { }
// const payoutItemId = this.getNodeParameter('payoutItemId', i) as string; }
// try { return [this.helpers.returnJsonArray(returnData)];
// responseData = await payPalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}`, 'GET', {}, qs);
// } catch (err) {
// throw new Error(`PayPal Error: ${JSON.stringify(err)}`);
// }
// }
// if (operation === 'cancel') {
// const payoutItemId = this.getNodeParameter('payoutItemId', i) as string;
// try {
// responseData = await payPalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}/cancel`, 'POST', {}, qs);
// } catch (err) {
// throw new Error(`PayPal Error: ${JSON.stringify(err)}`);
// }
// }
// }
// if (Array.isArray(responseData)) {
// returnData.push.apply(returnData, responseData as IDataObject[]);
// } else {
// returnData.push(responseData as IDataObject);
// }
// }
return [this.helpers.returnJsonArray({})];
} }
} }