2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-03-29 10:10:54 -07:00
|
|
|
IBinaryKeyData,
|
2020-03-16 19:02:48 -07:00
|
|
|
IDataObject,
|
2020-03-29 10:10:54 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-03-16 19:02:48 -07:00
|
|
|
INodeExecutionData,
|
2020-03-29 10:10:54 -07:00
|
|
|
INodePropertyOptions,
|
2020-03-16 19:02:48 -07:00
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2020-03-16 19:02:48 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { countriesCodes } from './CountriesCodes';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { conversationFields, conversationOperations } from './ConversationDescription';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { customerFields, customerOperations } from './CustomerDescription';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { ICustomer } from './CustomerInterface';
|
2020-03-29 10:10:54 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IConversation } from './ConversationInterface';
|
2020-03-29 10:10:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { helpscoutApiRequest, helpscoutApiRequestAllItems } from './GenericFunctions';
|
2020-03-29 10:10:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { mailboxFields, mailboxOperations } from './MailboxDescription';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { threadFields, threadOperations } from './ThreadDescription';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IAttachment, IThread } from './ThreadInterface';
|
2020-03-16 19:02:48 -07:00
|
|
|
|
|
|
|
export class HelpScout implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'HelpScout',
|
|
|
|
name: 'helpScout',
|
2021-03-01 14:38:22 -08:00
|
|
|
icon: 'file:helpScout.svg',
|
2020-03-16 19:02:48 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume HelpScout API',
|
2020-03-16 19:02:48 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'HelpScout',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'helpScoutOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-03-16 19:02:48 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Conversation',
|
|
|
|
value: 'conversation',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
value: 'customer',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Mailbox',
|
|
|
|
value: 'mailbox',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Thread',
|
|
|
|
value: 'thread',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'conversation',
|
|
|
|
},
|
|
|
|
...conversationOperations,
|
|
|
|
...conversationFields,
|
|
|
|
...customerOperations,
|
|
|
|
...customerFields,
|
|
|
|
...mailboxOperations,
|
|
|
|
...mailboxFields,
|
|
|
|
...threadOperations,
|
|
|
|
...threadFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the countries codes to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getCountriesCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const countryCode of countriesCodes) {
|
|
|
|
const countryCodeName = `${countryCode.name} - ${countryCode.alpha2}`;
|
|
|
|
const countryCodeId = countryCode.alpha2;
|
|
|
|
returnData.push({
|
|
|
|
name: countryCodeName,
|
|
|
|
value: countryCodeId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the tags to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const tags = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.tags',
|
|
|
|
'GET',
|
|
|
|
'/v2/tags',
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
for (const tag of tags) {
|
|
|
|
const tagName = tag.name;
|
2022-11-08 06:28:21 -08:00
|
|
|
const _tagId = tag.id;
|
2020-03-16 19:02:48 -07:00
|
|
|
returnData.push({
|
|
|
|
name: tagName,
|
2021-03-01 14:38:22 -08:00
|
|
|
value: tagName,
|
2020-03-16 19:02:48 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the mailboxes to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getMailboxes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const mailboxes = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.mailboxes',
|
|
|
|
'GET',
|
|
|
|
'/v2/mailboxes',
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
for (const mailbox of mailboxes) {
|
|
|
|
const mailboxName = mailbox.name;
|
|
|
|
const mailboxId = mailbox.id;
|
|
|
|
returnData.push({
|
|
|
|
name: mailboxName,
|
|
|
|
value: mailboxId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-03-16 19:02:48 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-03-16 19:02:48 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'conversation') {
|
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/create
|
|
|
|
if (operation === 'create') {
|
|
|
|
const mailboxId = this.getNodeParameter('mailboxId', i) as number;
|
|
|
|
const status = this.getNodeParameter('status', i) as string;
|
|
|
|
const subject = this.getNodeParameter('subject', i) as string;
|
|
|
|
const type = this.getNodeParameter('type', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const resolveData = this.getNodeParameter('resolveData', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
const threads = (this.getNodeParameter('threadsUi', i) as IDataObject)
|
|
|
|
.threadsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IConversation = {
|
|
|
|
mailboxId,
|
|
|
|
status,
|
|
|
|
subject,
|
|
|
|
type,
|
2020-03-16 19:02:48 -07:00
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
if (additionalFields.customerId) {
|
|
|
|
body.customer = {
|
|
|
|
id: additionalFields.customerId,
|
|
|
|
};
|
|
|
|
//@ts-ignore
|
|
|
|
delete body.customerId;
|
|
|
|
}
|
|
|
|
if (additionalFields.customerEmail) {
|
|
|
|
body.customer = {
|
|
|
|
email: additionalFields.customerEmail,
|
|
|
|
};
|
|
|
|
//@ts-ignore
|
|
|
|
delete body.customerEmail;
|
|
|
|
}
|
|
|
|
if (body.customer === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Either customer email or customer ID must be set',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (threads) {
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < threads.length; index++) {
|
|
|
|
if (threads[index].type === '' || threads[index].text === '') {
|
2021-07-19 23:58:54 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Chat Threads cannot be empty');
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (threads[index].type !== 'note') {
|
|
|
|
threads[index].customer = body.customer;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
body.threads = threads;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/v2/conversations',
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
{ resolveWithFullResponse: true },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = responseData.headers['resource-id'];
|
|
|
|
const uri = responseData.headers.location;
|
|
|
|
if (resolveData) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = await helpscoutApiRequest.call(this, 'GET', '', {}, {}, uri as string);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = {
|
|
|
|
id,
|
|
|
|
uri,
|
|
|
|
};
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/delete
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const conversationId = this.getNodeParameter('conversationId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/v2/conversations/${conversationId}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const conversationId = this.getNodeParameter('conversationId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/conversations/${conversationId}`,
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/list
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2023-01-25 04:35:57 -08:00
|
|
|
if (options.tags) {
|
|
|
|
qs.tag = options.tags.toString();
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2023-01-25 04:35:57 -08:00
|
|
|
delete qs.tags;
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.conversations',
|
|
|
|
'GET',
|
|
|
|
'/v2/conversations',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.conversations',
|
|
|
|
'GET',
|
|
|
|
'/v2/conversations',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (resource === 'customer') {
|
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/customers/create
|
|
|
|
if (operation === 'create') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const resolveData = this.getNodeParameter('resolveData', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
const chats = (this.getNodeParameter('chatsUi', i) as IDataObject)
|
|
|
|
.chatsValues as IDataObject[];
|
|
|
|
const address = (this.getNodeParameter('addressUi', i) as IDataObject)
|
|
|
|
.addressValue as IDataObject;
|
|
|
|
const emails = (this.getNodeParameter('emailsUi', i) as IDataObject)
|
|
|
|
.emailsValues as IDataObject[];
|
|
|
|
const phones = (this.getNodeParameter('phonesUi', i) as IDataObject)
|
|
|
|
.phonesValues as IDataObject[];
|
|
|
|
const socialProfiles = (this.getNodeParameter('socialProfilesUi', i) as IDataObject)
|
|
|
|
.socialProfilesValues as IDataObject[];
|
|
|
|
const websites = (this.getNodeParameter('websitesUi', i) as IDataObject)
|
|
|
|
.websitesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
let body: ICustomer = {};
|
|
|
|
body = Object.assign({}, additionalFields);
|
|
|
|
if (body.age) {
|
|
|
|
body.age = body.age.toString();
|
|
|
|
}
|
|
|
|
if (chats) {
|
|
|
|
body.chats = chats;
|
|
|
|
}
|
|
|
|
if (address) {
|
|
|
|
body.address = address;
|
|
|
|
body.address.lines = [address.line1, address.line2];
|
|
|
|
}
|
|
|
|
if (emails) {
|
|
|
|
body.emails = emails;
|
|
|
|
}
|
|
|
|
if (phones) {
|
|
|
|
body.phones = phones;
|
|
|
|
}
|
|
|
|
if (socialProfiles) {
|
|
|
|
body.socialProfiles = socialProfiles;
|
|
|
|
}
|
|
|
|
if (websites) {
|
|
|
|
body.websites = websites;
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'You have to set at least one field', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/v2/customers',
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
{ resolveWithFullResponse: true },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = responseData.headers['resource-id'];
|
|
|
|
const uri = responseData.headers.location;
|
|
|
|
if (resolveData) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = await helpscoutApiRequest.call(this, 'GET', '', {}, {}, uri as string);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = {
|
|
|
|
id,
|
|
|
|
uri,
|
|
|
|
};
|
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/customer_properties/list
|
|
|
|
if (operation === 'properties') {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.customer-properties',
|
|
|
|
'GET',
|
|
|
|
'/v2/customer-properties',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/customers/get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const customerId = this.getNodeParameter('customerId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/customers/${customerId}`,
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/customers/list
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.customers',
|
|
|
|
'GET',
|
|
|
|
'/v2/customers',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.customers',
|
|
|
|
'GET',
|
|
|
|
'/v2/customers',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/customers/overwrite/
|
|
|
|
if (operation === 'update') {
|
|
|
|
const customerId = this.getNodeParameter('customerId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
let body: ICustomer = {};
|
|
|
|
body = Object.assign({}, updateFields);
|
|
|
|
if (body.age) {
|
|
|
|
body.age = body.age.toString();
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'You have to set at least one field', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/v2/customers/${customerId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
{ resolveWithFullResponse: true },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2020-03-29 19:08:00 -07:00
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'mailbox') {
|
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/mailboxes/get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const mailboxId = this.getNodeParameter('mailboxId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/mailboxes/${mailboxId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/mailboxes/list
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.mailboxes',
|
|
|
|
'GET',
|
|
|
|
'/v2/mailboxes',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.mailboxes',
|
|
|
|
'GET',
|
|
|
|
'/v2/mailboxes',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
2020-03-29 19:08:00 -07:00
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'thread') {
|
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/threads/chat
|
|
|
|
if (operation === 'create') {
|
|
|
|
const conversationId = this.getNodeParameter('conversationId', i) as string;
|
2022-11-08 06:28:21 -08:00
|
|
|
const _type = this.getNodeParameter('type', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const attachments = this.getNodeParameter('attachmentsUi', i) as IDataObject;
|
|
|
|
const body: IThread = {
|
|
|
|
text,
|
|
|
|
attachments: [],
|
2020-03-16 19:02:48 -07:00
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
if (additionalFields.customerId) {
|
|
|
|
body.customer = {
|
|
|
|
id: additionalFields.customerId,
|
|
|
|
};
|
|
|
|
//@ts-ignore
|
|
|
|
delete body.customerId;
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customerEmail) {
|
|
|
|
body.customer = {
|
|
|
|
email: additionalFields.customerEmail,
|
2020-03-16 19:02:48 -07:00
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
//@ts-ignore
|
|
|
|
delete body.customerEmail;
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.customer === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Either customer email or customer ID must be set',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (attachments) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
attachments.attachmentsValues &&
|
|
|
|
(attachments.attachmentsValues as IDataObject[]).length !== 0
|
|
|
|
) {
|
|
|
|
body.attachments?.push.apply(
|
|
|
|
body.attachments,
|
|
|
|
attachments.attachmentsValues as IAttachment[],
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
attachments.attachmentsBinary &&
|
|
|
|
(attachments.attachmentsBinary as IDataObject[]).length !== 0 &&
|
|
|
|
items[i].binary
|
|
|
|
) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const mapFunction = (value: IDataObject): IAttachment => {
|
2022-08-17 08:50:24 -07:00
|
|
|
const binaryProperty = (items[i].binary as IBinaryKeyData)[
|
|
|
|
value.property as string
|
|
|
|
];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (binaryProperty) {
|
|
|
|
return {
|
2023-01-19 04:37:19 -08:00
|
|
|
fileName: binaryProperty.fileName || 'unknown',
|
2021-07-19 23:58:54 -07:00
|
|
|
data: binaryProperty.data,
|
|
|
|
mimeType: binaryProperty.mimeType,
|
|
|
|
};
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Binary property ${value.property} does not exist on input`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
body.attachments?.push.apply(
|
|
|
|
body.attachments,
|
2022-12-02 12:54:28 -08:00
|
|
|
(attachments.attachmentsBinary as IDataObject[]).map(mapFunction),
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/v2/conversations/${conversationId}/chats`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/threads/list
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const conversationId = this.getNodeParameter('conversationId', i) as string;
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.threads',
|
|
|
|
'GET',
|
|
|
|
`/v2/conversations/${conversationId}/threads`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await helpscoutApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'_embedded.threads',
|
|
|
|
'GET',
|
|
|
|
`/v2/conversations/${conversationId}/threads`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
2020-03-29 19:08:00 -07:00
|
|
|
}
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
return this.prepareOutputData(returnData);
|
2020-03-16 19:02:48 -07:00
|
|
|
}
|
|
|
|
}
|