Skip to content

Commit

Permalink
🔨 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Shraddha2104 committed Jul 6, 2020
1 parent d6bf526 commit 10ab03e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
34 changes: 17 additions & 17 deletions packages/nodes-base/nodes/CustomerIo/CustomerIoTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ export class CustomerIoTrigger implements INodeType {
const endpoint = `/reporting_webhooks/${webhookData.webhookId}`;
try {
await customerIoApiRequest.call(this, 'GET', endpoint, {}, {});
} catch (err) {
if (err.statusCode === 404) {
} catch (error) {
if (error.statusCode === 404) {
return false;
}
throw new Error(`Customer.io Error: ${err}`);
throw new Error(`Customer.io Error: ${error}`);
}

return true;
Expand All @@ -239,39 +239,39 @@ export class CustomerIoTrigger implements INodeType {
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events', []) as string[];
const endpoint = '/reporting_webhooks';
var events_list: { [key: string]: any } = {};
let eventsList: { [key: string]: any } = {};
for (const event of events) {
var obj = event.split('.'); //push.attempted
var key = obj[0]; //push
var val = obj[1]; //attempted
const eventAction = event.split('.'); //push.attempted
const eventName = eventAction[0]; //push
const actionName = eventAction[1]; //attempted

if (events_list.hasOwnProperty(key)) {
var value = events_list[key];
value[val] = true;
if (eventsList.hasOwnProperty(eventName)) {
const value = eventsList[eventName];
value[actionName] = true;
}
else {
var obj1: { [val: string]: boolean; } = { [val]: true }; //{'attempted':true}
events_list[key] = obj1;
const obj1: { [val: string]: boolean; } = { [actionName]: true }; //{'attempted':true}
eventsList[eventName] = obj1;
}
}
const body = {
endpoint: webhookUrl,
disabled: false,
events: events_list,
events: eventsList,
};

try {
webhook = await customerIoApiRequest.call(this, 'POST', endpoint, body, {});
} catch (e) {
throw e;
} catch (error) {
throw error;
}

if (webhook.id === undefined) {
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id as string;
webhookData.events = events_list;
webhookData.events = eventsList;
return true;

},
Expand All @@ -282,7 +282,7 @@ export class CustomerIoTrigger implements INodeType {
const endpoint = `/reporting_webhooks/${webhookData.webhookId}`;
try {
await customerIoApiRequest.call(this, 'DELETE', endpoint, {}, {});
} catch (e) {
} catch (error) {
return false;
}
delete webhookData.webhookId;
Expand Down
9 changes: 7 additions & 2 deletions packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import {
ILoadOptionsFunctions,
} from 'n8n-core';

import { OptionsWithUri } from 'request';
import { IDataObject } from 'n8n-workflow';
import {
OptionsWithUri,
} from 'request';

import {
IDataObject,
} from 'n8n-workflow';

export async function customerIoApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, qs?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('customerIoApi');
Expand Down

0 comments on commit 10ab03e

Please sign in to comment.