mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 11:22:15 +00:00
⚡ Added ticket resource
This commit is contained in:
@@ -21,20 +21,25 @@ import {
|
||||
dealFields,
|
||||
} from './DealDescription';
|
||||
|
||||
import {
|
||||
IDeal,
|
||||
IAssociation
|
||||
} from './DealInterface';
|
||||
|
||||
import {
|
||||
formOperations,
|
||||
formFields,
|
||||
} from './FormDescription';
|
||||
|
||||
import {
|
||||
ticketOperations,
|
||||
ticketFields,
|
||||
} from './TicketDescription';
|
||||
|
||||
import {
|
||||
IForm
|
||||
IForm,
|
||||
} from './FormInterface';
|
||||
|
||||
import {
|
||||
IDeal,
|
||||
IAssociation,
|
||||
} from './DealInterface';
|
||||
|
||||
export class Hubspot implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Hubspot',
|
||||
@@ -70,22 +75,31 @@ export class Hubspot implements INodeType {
|
||||
name: 'Form',
|
||||
value: 'form',
|
||||
},
|
||||
{
|
||||
name: 'Ticket',
|
||||
value: 'ticket',
|
||||
},
|
||||
],
|
||||
default: 'deal',
|
||||
description: 'Resource to consume.',
|
||||
},
|
||||
|
||||
// Deal
|
||||
// DEAL
|
||||
...dealOperations,
|
||||
...dealFields,
|
||||
// Form
|
||||
// FORM
|
||||
...formOperations,
|
||||
...formFields,
|
||||
// TICKET
|
||||
...ticketOperations,
|
||||
...ticketFields,
|
||||
],
|
||||
};
|
||||
|
||||
methods = {
|
||||
loadOptions: {
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* DEAL */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
// Get all the groups to display them to user so that he can
|
||||
// select them easily
|
||||
@@ -104,41 +118,6 @@ export class Hubspot implements INodeType {
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
|
||||
// Get all the companies to display them to user so that he can
|
||||
// select them easily
|
||||
async getCompanies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/companies/v2/companies/paged';
|
||||
const companies = await hubspotApiRequestAllItems.call(this, 'results', 'GET', endpoint);
|
||||
for (const company of companies) {
|
||||
const companyName = company.properties.name.value;
|
||||
const companyId = company.companyId;
|
||||
returnData.push({
|
||||
name: companyName,
|
||||
value: companyId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
|
||||
// Get all the companies to display them to user so that he can
|
||||
// select them easily
|
||||
async getContacts(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/contacts/v1/lists/all/contacts/all';
|
||||
const contacts = await hubspotApiRequestAllItems.call(this, 'contacts', 'GET', endpoint);
|
||||
for (const contact of contacts) {
|
||||
const contactName = `${contact.properties.firstname.value} ${contact.properties.lastname.value}` ;
|
||||
const contactId = contact.vid;
|
||||
returnData.push({
|
||||
name: contactName,
|
||||
value: contactId,
|
||||
});
|
||||
}
|
||||
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[]> {
|
||||
@@ -156,6 +135,10 @@ export class Hubspot implements INodeType {
|
||||
return returnData;
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* FORM */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
// Get all the forms to display them to user so that he can
|
||||
// select them easily
|
||||
async getForms(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
@@ -172,7 +155,6 @@ export class Hubspot implements INodeType {
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
|
||||
// Get all the subscription types to display them to user so that he can
|
||||
// select them easily
|
||||
async getSubscriptionTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
@@ -189,7 +171,201 @@ export class Hubspot implements INodeType {
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* TICKET */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
// Get all the ticket categories to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/properties/v2/tickets/properties';
|
||||
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const property of properties) {
|
||||
if (property.name === 'hs_ticket_category') {
|
||||
for (const option of property.options) {
|
||||
const categoryName = option.label;
|
||||
const categoryId = option.value;
|
||||
returnData.push({
|
||||
name: categoryName,
|
||||
value: categoryId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnData.sort((a, b) => a.name < b.name ? 0 : 1);
|
||||
},
|
||||
// Get all the ticket pipelines to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketPipelines(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/crm-pipelines/v1/pipelines/tickets';
|
||||
const { results } = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const pipeline of results) {
|
||||
const pipelineName = pipeline.label;
|
||||
const pipelineId = pipeline.pipelineId;
|
||||
returnData.push({
|
||||
name: pipelineName,
|
||||
value: pipelineId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get all the ticket resolutions to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketPriorities(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/properties/v2/tickets/properties';
|
||||
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const property of properties) {
|
||||
if (property.name === 'hs_ticket_priority') {
|
||||
for (const option of property.options) {
|
||||
const priorityName = option.label;
|
||||
const priorityId = option.value;
|
||||
returnData.push({
|
||||
name: priorityName,
|
||||
value: priorityId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get all the ticket properties to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/properties/v2/tickets/properties';
|
||||
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const property of properties) {
|
||||
const propertyName = property.label;
|
||||
const propertyId = property.name;
|
||||
returnData.push({
|
||||
name: propertyName,
|
||||
value: propertyId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get all the ticket resolutions to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketResolutions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/properties/v2/tickets/properties';
|
||||
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const property of properties) {
|
||||
if (property.name === 'hs_resolution') {
|
||||
for (const option of property.options) {
|
||||
const resolutionName = option.label;
|
||||
const resolutionId = option.value;
|
||||
returnData.push({
|
||||
name: resolutionName,
|
||||
value: resolutionId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnData.sort((a, b) => a.name < b.name ? 0 : 1);
|
||||
},
|
||||
// Get all the ticket sources to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketSources(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/properties/v2/tickets/properties';
|
||||
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const property of properties) {
|
||||
if (property.name === 'source_type') {
|
||||
for (const option of property.options) {
|
||||
const sourceName = option.label;
|
||||
const sourceId = option.value;
|
||||
returnData.push({
|
||||
name: sourceName,
|
||||
value: sourceId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnData.sort((a, b) => a.name < b.name ? 0 : 1);
|
||||
},
|
||||
// Get all the ticket stages to display them to user so that he can
|
||||
// select them easily
|
||||
async getTicketStages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const currentPipelineId = this.getCurrentNodeParameter('pipelineId') as string;
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/crm-pipelines/v1/pipelines/tickets';
|
||||
const { results } = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
||||
for (const pipeline of results) {
|
||||
if (currentPipelineId === pipeline.pipelineId) {
|
||||
for (const stage of pipeline.stages) {
|
||||
const stageName = stage.label;
|
||||
const stageId = stage.stageId;
|
||||
returnData.push({
|
||||
name: stageName,
|
||||
value: stageId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* COMMON */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
// Get all the owners to display them to user so that he can
|
||||
// select them easily
|
||||
async getOwners(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/owners/v2/owners';
|
||||
const owners = await hubspotApiRequest.call(this, 'GET', endpoint);
|
||||
for (const owner of owners) {
|
||||
const ownerName = owner.email;
|
||||
const ownerId = owner.ownerId;
|
||||
returnData.push({
|
||||
name: ownerName,
|
||||
value: ownerId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get all the 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 = {
|
||||
properties: ['name'],
|
||||
};
|
||||
const endpoint = '/companies/v2/companies/paged';
|
||||
const companies = await hubspotApiRequestAllItems.call(this, 'companies', 'GET', endpoint, {}, qs);
|
||||
for (const company of companies) {
|
||||
const companyName = company.properties.name.value;
|
||||
const companyId = company.companyId;
|
||||
returnData.push({
|
||||
name: companyName,
|
||||
value: companyId,
|
||||
});
|
||||
}
|
||||
return returnData.sort((a, b) => a.name < b.name ? 0 : 1);
|
||||
},
|
||||
// Get all the companies to display them to user so that he can
|
||||
// select them easily
|
||||
async getContacts(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const endpoint = '/contacts/v1/lists/all/contacts/all';
|
||||
const contacts = await hubspotApiRequestAllItems.call(this, 'contacts', 'GET', endpoint);
|
||||
for (const contact of contacts) {
|
||||
const contactName = `${contact.properties.firstname.value} ${contact.properties.lastname.value}` ;
|
||||
const contactId = contact.vid;
|
||||
returnData.push({
|
||||
name: contactName,
|
||||
value: contactId,
|
||||
});
|
||||
}
|
||||
return returnData.sort((a, b) => a.name < b.name ? 0 : 1);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
@@ -425,6 +601,242 @@ export class Hubspot implements INodeType {
|
||||
responseData = await hubspotApiRequest.call(this, 'POST', '', body, {}, uri);
|
||||
}
|
||||
}
|
||||
//https://developers.hubspot.com/docs/methods/tickets/tickets-overview
|
||||
if (resource === 'ticket') {
|
||||
//https://developers.hubspot.com/docs/methods/tickets/create-ticket
|
||||
if (operation === 'create') {
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
const pipelineId = this.getNodeParameter('pipelineId', i) as string;
|
||||
const stageId = this.getNodeParameter('stageId', i) as string;
|
||||
const ticketName = this.getNodeParameter('ticketName', i) as string;
|
||||
const body: IDataObject[] = [
|
||||
{
|
||||
name: 'hs_pipeline',
|
||||
value: pipelineId,
|
||||
},
|
||||
{
|
||||
name: 'hs_pipeline_stage',
|
||||
value: stageId,
|
||||
},
|
||||
{
|
||||
name: 'subject',
|
||||
value: ticketName,
|
||||
},
|
||||
];
|
||||
if (additionalFields.category) {
|
||||
body.push({
|
||||
name: 'hs_ticket_category',
|
||||
value: additionalFields.category as string
|
||||
});
|
||||
}
|
||||
if (additionalFields.closeDate) {
|
||||
body.push({
|
||||
name: 'closed_date',
|
||||
value: new Date(additionalFields.closeDate as string).getTime(),
|
||||
});
|
||||
}
|
||||
if (additionalFields.createDate) {
|
||||
body.push({
|
||||
name: 'createdate',
|
||||
value: new Date(additionalFields.createDate as string).getTime(),
|
||||
});
|
||||
}
|
||||
if (additionalFields.description) {
|
||||
body.push({
|
||||
name: 'content',
|
||||
value: additionalFields.description as string
|
||||
});
|
||||
}
|
||||
if (additionalFields.priority) {
|
||||
body.push({
|
||||
name: 'hs_ticket_priority',
|
||||
value: additionalFields.priority as string
|
||||
});
|
||||
}
|
||||
if (additionalFields.resolution) {
|
||||
body.push({
|
||||
name: 'hs_resolution',
|
||||
value: additionalFields.resolution as string
|
||||
});
|
||||
}
|
||||
if (additionalFields.source) {
|
||||
body.push({
|
||||
name: 'source_type',
|
||||
value: additionalFields.source as string
|
||||
});
|
||||
}
|
||||
if (additionalFields.ticketOwnerId) {
|
||||
body.push({
|
||||
name: 'hubspot_owner_id',
|
||||
value: additionalFields.ticketOwnerId as string
|
||||
});
|
||||
}
|
||||
const endpoint = '/crm-objects/v1/objects/tickets';
|
||||
responseData = await hubspotApiRequest.call(this, 'POST', endpoint, body);
|
||||
|
||||
if (additionalFields.associatedCompanyIds) {
|
||||
const companyAssociations: IDataObject[] = [];
|
||||
for (const companyId of additionalFields.associatedCompanyIds as IDataObject[]) {
|
||||
companyAssociations.push({
|
||||
fromObjectId: responseData.objectId,
|
||||
toObjectId: companyId,
|
||||
category: 'HUBSPOT_DEFINED',
|
||||
definitionId: 26,
|
||||
});
|
||||
}
|
||||
await hubspotApiRequest.call(this, 'PUT', '/crm-associations/v1/associations/create-batch', companyAssociations);
|
||||
}
|
||||
|
||||
if (additionalFields.associatedContactIds) {
|
||||
const contactAssociations: IDataObject[] = [];
|
||||
for (const contactId of additionalFields.associatedContactIds as IDataObject[]) {
|
||||
contactAssociations.push({
|
||||
fromObjectId: responseData.objectId,
|
||||
toObjectId: contactId,
|
||||
category: 'HUBSPOT_DEFINED',
|
||||
definitionId: 16,
|
||||
});
|
||||
}
|
||||
await hubspotApiRequest.call(this, 'PUT', '/crm-associations/v1/associations/create-batch', contactAssociations);
|
||||
}
|
||||
}
|
||||
//https://developers.hubspot.com/docs/methods/tickets/get_ticket_by_id
|
||||
if (operation === 'get') {
|
||||
const ticketId = this.getNodeParameter('ticketId', i) as string;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
if (additionalFields.properties) {
|
||||
qs.properties = additionalFields.properties as string[]
|
||||
}
|
||||
if (additionalFields.propertiesWithHistory) {
|
||||
qs.propertiesWithHistory = (additionalFields.propertiesWithHistory as string).split(',');
|
||||
}
|
||||
if (additionalFields.includeDeleted) {
|
||||
qs.includeDeleted = additionalFields.includeDeleted as boolean;
|
||||
}
|
||||
const endpoint = `/crm-objects/v1/objects/tickets/${ticketId}`;
|
||||
responseData = await hubspotApiRequest.call(this, 'GET', endpoint, {}, qs);
|
||||
}
|
||||
//https://developers.hubspot.com/docs/methods/tickets/get-all-tickets
|
||||
if (operation === 'getAll') {
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
||||
if (additionalFields.properties) {
|
||||
qs.properties = additionalFields.properties as string[];
|
||||
}
|
||||
if (additionalFields.propertiesWithHistory) {
|
||||
qs.propertiesWithHistory = (additionalFields.propertiesWithHistory as string).split(',');
|
||||
}
|
||||
const endpoint = `/crm-objects/v1/objects/tickets/paged`;
|
||||
if (returnAll) {
|
||||
responseData = await hubspotApiRequestAllItems.call(this, 'objects', 'GET', endpoint, {}, qs);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', 0) as number;
|
||||
responseData = await hubspotApiRequestAllItems.call(this, 'objects', 'GET', endpoint, {}, qs);
|
||||
responseData = responseData.splice(0, qs.limit);
|
||||
}
|
||||
}
|
||||
//https://developers.hubspot.com/docs/methods/tickets/delete-ticket
|
||||
if (operation === 'delete') {
|
||||
const ticketId = this.getNodeParameter('ticketId', i) as string;
|
||||
const endpoint = `/crm-objects/v1/objects/tickets/${ticketId}`;
|
||||
responseData = await hubspotApiRequest.call(this, 'DELETE', endpoint);
|
||||
responseData = { success: true };
|
||||
}
|
||||
//https://developers.hubspot.com/docs/methods/tickets/update-ticket
|
||||
if (operation === 'update') {
|
||||
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
||||
const ticketId = this.getNodeParameter('ticketId', i) as string;
|
||||
const body: IDataObject[] = [];
|
||||
if (updateFields.pipelineId) {
|
||||
body.push({
|
||||
name: 'hs_pipeline',
|
||||
value: updateFields.pipelineId as string,
|
||||
});
|
||||
}
|
||||
if (updateFields.ticketName) {
|
||||
body.push({
|
||||
name: 'subject',
|
||||
value: updateFields.ticketName as string,
|
||||
});
|
||||
}
|
||||
if (updateFields.category) {
|
||||
body.push({
|
||||
name: 'hs_ticket_category',
|
||||
value: updateFields.category as string
|
||||
});
|
||||
}
|
||||
if (updateFields.closeDate) {
|
||||
body.push({
|
||||
name: 'closed_date',
|
||||
value: new Date(updateFields.createDate as string).getTime(),
|
||||
});
|
||||
}
|
||||
if (updateFields.createDate) {
|
||||
body.push({
|
||||
name: 'createdate',
|
||||
value: new Date(updateFields.createDate as string).getTime(),
|
||||
});
|
||||
}
|
||||
if (updateFields.description) {
|
||||
body.push({
|
||||
name: 'content',
|
||||
value: updateFields.description as string
|
||||
});
|
||||
}
|
||||
if (updateFields.priority) {
|
||||
body.push({
|
||||
name: 'hs_ticket_priority',
|
||||
value: updateFields.priority as string
|
||||
});
|
||||
}
|
||||
if (updateFields.resolution) {
|
||||
body.push({
|
||||
name: 'hs_resolution',
|
||||
value: updateFields.resolution as string
|
||||
});
|
||||
}
|
||||
if (updateFields.source) {
|
||||
body.push({
|
||||
name: 'source_type',
|
||||
value: updateFields.source as string
|
||||
});
|
||||
}
|
||||
if (updateFields.ticketOwnerId) {
|
||||
body.push({
|
||||
name: 'hubspot_owner_id',
|
||||
value: updateFields.ticketOwnerId as string
|
||||
});
|
||||
}
|
||||
const endpoint = `/crm-objects/v1/objects/tickets/${ticketId}`;
|
||||
responseData = await hubspotApiRequest.call(this, 'PUT', endpoint, body);
|
||||
|
||||
if (updateFields.associatedCompanyIds) {
|
||||
const companyAssociations: IDataObject[] = [];
|
||||
for (const companyId of updateFields.associatedCompanyIds as IDataObject[]) {
|
||||
companyAssociations.push({
|
||||
fromObjectId: responseData.objectId,
|
||||
toObjectId: companyId,
|
||||
category: 'HUBSPOT_DEFINED',
|
||||
definitionId: 26,
|
||||
});
|
||||
}
|
||||
await hubspotApiRequest.call(this, 'PUT', '/crm-associations/v1/associations/create-batch', companyAssociations);
|
||||
}
|
||||
|
||||
if (updateFields.associatedContactIds) {
|
||||
const contactAssociations: IDataObject[] = [];
|
||||
for (const contactId of updateFields.associatedContactIds as IDataObject[]) {
|
||||
contactAssociations.push({
|
||||
fromObjectId: responseData.objectId,
|
||||
toObjectId: contactId,
|
||||
category: 'HUBSPOT_DEFINED',
|
||||
definitionId: 16,
|
||||
});
|
||||
}
|
||||
await hubspotApiRequest.call(this, 'PUT', '/crm-associations/v1/associations/create-batch', contactAssociations);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Array.isArray(responseData)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user