/
GenericFunctions.ts
194 lines (159 loc) · 5.17 KB
/
GenericFunctions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { IExecuteFunctions } from 'n8n-core';
import { IDataObject, ILoadOptionsFunctions, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { OptionsWithUri } from 'request';
import {
FreshworksConfigResponse,
FreshworksCrmApiCredentials,
SalesAccounts,
ViewsResponse,
} from './types';
import { omit } from 'lodash';
export async function freshworksCrmApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const { domain } = (await this.getCredentials('freshworksCrmApi')) as FreshworksCrmApiCredentials;
const options: OptionsWithUri = {
method,
body,
qs,
uri: `https://${domain}.myfreshworks.com/crm/sales/api${endpoint}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
try {
const credentialsType = 'freshworksCrmApi';
return await this.helpers.requestWithAuthentication.call(this, credentialsType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function getAllItemsViewId(
this: IExecuteFunctions | ILoadOptionsFunctions,
{ fromLoadOptions } = { fromLoadOptions: false },
) {
let resource = this.getNodeParameter('resource', 0) as string;
let keyword = 'All';
if (resource === 'account' || fromLoadOptions) {
resource = 'sales_account'; // adjust resource to endpoint
}
if (resource === 'deal') {
keyword = 'My Deals'; // no 'All Deals' available
}
const response = (await freshworksCrmApiRequest.call(
this,
'GET',
`/${resource}s/filters`,
)) as ViewsResponse;
const view = response.filters.find((v) => v.name.includes(keyword));
if (!view) {
throw new NodeOperationError(this.getNode(), 'Failed to get all items view');
}
return view.id.toString();
}
export async function freshworksCrmApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const returnData: IDataObject[] = [];
let response: any; // tslint:disable-line: no-any
qs.page = 1;
do {
response = await freshworksCrmApiRequest.call(this, method, endpoint, body, qs);
const key = Object.keys(response)[0];
returnData.push(...response[key]);
qs.page++;
} while (response.meta.total_pages && qs.page <= response.meta.total_pages);
return returnData;
}
export async function handleListing(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const returnAll = this.getNodeParameter('returnAll', 0);
if (returnAll) {
return await freshworksCrmApiRequestAllItems.call(this, method, endpoint, body, qs);
}
const responseData = await freshworksCrmApiRequestAllItems.call(this, method, endpoint, body, qs);
const limit = this.getNodeParameter('limit', 0) as number;
if (limit) return responseData.slice(0, limit);
return responseData;
}
/**
* Load resources for options, except users.
*
* See: https://developers.freshworks.com/crm/api/#admin_configuration
*/
export async function loadResource(this: ILoadOptionsFunctions, resource: string) {
const response = (await freshworksCrmApiRequest.call(
this,
'GET',
`/selector/${resource}`,
)) as FreshworksConfigResponse<LoadedResource>;
const key = Object.keys(response)[0];
return response[key].map(({ name, id }) => ({ name, value: id }));
}
export function adjustAttendees(attendees: [{ type: string; contactId: string; userId: string }]) {
return attendees.map((attendee) => {
if (attendee.type === 'contact') {
return {
attendee_type: 'Contact',
attendee_id: attendee.contactId.toString(),
};
} else if (attendee.type === 'user') {
return {
attendee_type: 'FdMultitenant::User',
attendee_id: attendee.userId.toString(),
};
}
});
}
// /**
// * Adjust attendee data from n8n UI to the format expected by Freshworks CRM API.
// */
// export function adjustAttendees(additionalFields: IDataObject & Attendees) {
// if (!additionalFields?.appointment_attendees_attributes) return additionalFields;
// return {
// ...omit(additionalFields, ['appointment_attendees_attributes']),
// appointment_attendees_attributes: additionalFields.appointment_attendees_attributes.map(attendeeId => {
// return { type: 'user', id: attendeeId };
// }),
// };
// }
/**
* Adjust account data from n8n UI to the format expected by Freshworks CRM API.
*/
export function adjustAccounts(additionalFields: IDataObject & SalesAccounts) {
if (!additionalFields?.sales_accounts) return additionalFields;
const adjusted = additionalFields.sales_accounts.map((accountId) => {
return { id: accountId, is_primary: false };
});
adjusted[0].is_primary = true;
return {
...omit(additionalFields, ['sales_accounts']),
sales_accounts: adjusted,
};
}
export function throwOnEmptyUpdate(this: IExecuteFunctions, resource: string) {
throw new NodeOperationError(
this.getNode(),
`Please enter at least one field to update for the ${resource}.`,
);
}
export function throwOnEmptyFilter(this: IExecuteFunctions) {
throw new NodeOperationError(this.getNode(), `Please select at least one filter.`);
}