diff --git a/packages/nodes-base/credentials/HumanticAiApi.credentials.ts b/packages/nodes-base/credentials/HumanticAiApi.credentials.ts new file mode 100644 index 0000000000..b9303e028a --- /dev/null +++ b/packages/nodes-base/credentials/HumanticAiApi.credentials.ts @@ -0,0 +1,18 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +export class HumanticAiApi implements ICredentialType { + name = 'humanticAiApi'; + displayName = 'Humantic AI API'; + documentationUrl = 'humanticAi'; + properties = [ + { + displayName: 'API Key', + name: 'apiKey', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/HumanticAI/GenericFunctions.ts b/packages/nodes-base/nodes/HumanticAI/GenericFunctions.ts new file mode 100644 index 0000000000..b2f785e13e --- /dev/null +++ b/packages/nodes-base/nodes/HumanticAI/GenericFunctions.ts @@ -0,0 +1,59 @@ +import { + OptionsWithUri, +} from 'request'; + +import { + IExecuteFunctions, + IExecuteSingleFunctions, + IHookFunctions, + ILoadOptionsFunctions, +} from 'n8n-core'; + +import { + IDataObject, +} from 'n8n-workflow'; + +export async function humanticAiApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, option: IDataObject = {}): Promise { // tslint:disable-line:no-any + try { + const credentials = this.getCredentials('humanticAiApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + let options: OptionsWithUri = { + headers: { + 'Content-Type': 'application/json', + }, + method, + qs, + body, + uri: `https://api.humantic.ai/v1${resource}`, + json: true, + }; + + options = Object.assign({}, options, option); + options.qs.apikey = credentials.apiKey; + + if (Object.keys(options.body).length === 0) { + delete options.body; + } + + const response = await this.helpers.request!(options); + + if (response.data && response.data.status === 'error') { + throw new Error(`Humantic AI error response [400]: ${response.data.message}`); + } + + return response; + + } catch (error) { + + if (error.response && error.response.body && error.response.body.message) { + // Try to return the error prettier + const errorBody = error.response.body; + throw new Error(`Humantic AI error response [${error.statusCode}]: ${errorBody.message}`); + } + + // Expected error data did not get returned so throw the actual error + throw error; + } +} diff --git a/packages/nodes-base/nodes/HumanticAI/HumanticAi.node.ts b/packages/nodes-base/nodes/HumanticAI/HumanticAi.node.ts new file mode 100644 index 0000000000..b7996bc008 --- /dev/null +++ b/packages/nodes-base/nodes/HumanticAI/HumanticAi.node.ts @@ -0,0 +1,190 @@ +import { + BINARY_ENCODING, + IExecuteFunctions, +} from 'n8n-core'; + +import { + IBinaryData, + IBinaryKeyData, + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { + humanticAiApiRequest, +} from './GenericFunctions'; + +import { + profileFields, + profileOperations, +} from './ProfileDescription'; + +export class HumanticAi implements INodeType { + description: INodeTypeDescription = { + displayName: 'Humantic AI', + name: 'humanticAi', + icon: 'file:humanticai.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume Humantic AI API', + defaults: { + name: 'Humantic AI', + color: '#f8ce59', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'humanticAiApi', + required: true, + }, + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Profile', + value: 'profile', + }, + ], + default: 'profile', + description: 'Resource to consume.', + }, + // PROFILE + ...profileOperations, + ...profileFields, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + const length = items.length as unknown as number; + const qs: IDataObject = {}; + let responseData; + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + for (let i = 0; i < length; i++) { + if (resource === 'profile') { + if (operation === 'create') { + const userId = this.getNodeParameter('userId', i) as string; + const sendResume = this.getNodeParameter('sendResume', i) as boolean; + qs.userid = userId; + + if (sendResume) { + const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string; + + if (items[i].binary === undefined) { + throw new Error('No binary data exists on item!'); + } + + const item = items[i].binary as IBinaryKeyData; + + const binaryData = item[binaryPropertyName] as IBinaryData; + + if (binaryData === undefined) { + throw new Error(`No binary data property "${binaryPropertyName}" does not exists on item!`); + } + + responseData = await humanticAiApiRequest.call( + this, + 'POST', + `/user-profile/create`, + {}, + qs, + { + formData: { + resume: { + value: Buffer.from(binaryData.data, BINARY_ENCODING), + options: { + filename: binaryData.fileName, + }, + }, + }, + }, + ); + responseData = responseData.data; + } else { + responseData = await humanticAiApiRequest.call(this, 'GET', `/user-profile/create`, {}, qs); + responseData = responseData.data; + } + } + if (operation === 'get') { + const userId = this.getNodeParameter('userId', i) as string; + const options = this.getNodeParameter('options', i) as IDataObject; + + qs.userid = userId; + + if (options.persona) { + qs.persona = (options.persona as string[]).join(','); + } + + responseData = await humanticAiApiRequest.call(this, 'GET', `/user-profile`, {}, qs); + responseData = responseData.results; + } + if (operation === 'update') { + const userId = this.getNodeParameter('userId', i) as string; + const sendResume = this.getNodeParameter('sendResume', i) as string; + qs.userid = userId; + + if (sendResume) { + const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string; + + if (items[i].binary === undefined) { + throw new Error('No binary data exists on item!'); + } + + const item = items[i].binary as IBinaryKeyData; + + const binaryData = item[binaryPropertyName] as IBinaryData; + + if (binaryData === undefined) { + throw new Error(`No binary data property "${binaryPropertyName}" does not exists on item!`); + } + + responseData = await humanticAiApiRequest.call( + this, + 'POST', + `/user-profile/create`, + {}, + qs, + { + formData: { + resume: { + value: Buffer.from(binaryData.data, BINARY_ENCODING), + options: { + filename: binaryData.fileName, + }, + }, + }, + }, + ); + responseData = responseData.data; + } else { + const text = this.getNodeParameter('text', i) as string; + const body: IDataObject = { + text, + }; + + qs.userid = userId; + + responseData = await humanticAiApiRequest.call(this, 'POST', `/user-profile/create`, body, qs); + responseData = responseData.data; + } + } + } + if (Array.isArray(responseData)) { + returnData.push.apply(returnData, responseData as IDataObject[]); + } else { + returnData.push(responseData as IDataObject); + } + } + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/HumanticAI/ProfileDescription.ts b/packages/nodes-base/nodes/HumanticAI/ProfileDescription.ts new file mode 100644 index 0000000000..3e54b8fadb --- /dev/null +++ b/packages/nodes-base/nodes/HumanticAI/ProfileDescription.ts @@ -0,0 +1,238 @@ +import { + INodeProperties, +} from 'n8n-workflow'; + +export const profileOperations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'profile', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create a profile', + }, + { + name: 'Get', + value: 'get', + description: 'Retrieve a profile', + }, + { + name: 'Update', + value: 'update', + description: 'Update a profile', + }, + ], + default: 'create', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const profileFields = [ + /* -------------------------------------------------------------------------- */ + /* profile:create */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'User ID', + name: 'userId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + operation: [ + 'create', + ], + resource: [ + 'profile', + ], + }, + }, + description: `The LinkedIn profile URL or email ID for creating a Humantic profile. If you are sending the resume, this should be a unique string.`, + }, + { + displayName: 'Send Resume', + name: 'sendResume', + type: 'boolean', + default: false, + displayOptions: { + show: { + operation: [ + 'create', + ], + resource: [ + 'profile', + ], + }, + }, + description: `Send a resume for a resume based analysis.`, + }, + { + displayName: 'Binary Property', + name: 'binaryPropertyName', + type: 'string', + default: 'data', + displayOptions: { + show: { + operation: [ + 'create', + ], + resource: [ + 'profile', + ], + sendResume: [ + true, + ], + }, + }, + description: `The resume in PDF or DOCX format.`, + }, + + /* -------------------------------------------------------------------------- */ + /* profile:get */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'User ID', + name: 'userId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + operation: [ + 'get', + ], + resource: [ + 'profile', + ], + }, + }, + description: `This value is the same as the User ID that was provided when the analysis was created. This could be a LinkedIn URL, email ID, or a unique string in case of resume based analysis.`, + }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Option', + default: {}, + displayOptions: { + show: { + operation: [ + 'get', + ], + resource: [ + 'profile', + ], + }, + }, + options: [ + { + displayName: 'Persona', + name: 'persona', + type: 'multiOptions', + options: [ + { + name: 'Sales', + value: 'sales', + }, + { + name: 'Hiring', + value: 'hiring', + }, + ], + default: [], + description: `Fetch the Humantic profile of the user for a particular persona type.
+ Multiple persona values can be supported using comma as a delimiter.`, + }, + ], + }, + + + + /* -------------------------------------------------------------------------- */ + /* profile:update */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'User ID', + name: 'userId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + operation: [ + 'update', + ], + resource: [ + 'profile', + ], + }, + }, + description: `This value is the same as the User ID that was provided when the analysis was created. Currently only supported for profiles created using LinkedIn URL.`, + }, + { + displayName: 'Send Resume', + name: 'sendResume', + type: 'boolean', + default: false, + displayOptions: { + show: { + operation: [ + 'update', + ], + resource: [ + 'profile', + ], + }, + }, + description: `Send a resume for a resume of the user.`, + }, + { + displayName: 'Text', + name: 'text', + type: 'string', + default: '', + displayOptions: { + show: { + operation: [ + 'update', + ], + resource: [ + 'profile', + ], + sendResume: [ + false, + ], + }, + }, + description: `Additional text written by the user.`, + }, + { + displayName: 'Binary Property', + name: 'binaryPropertyName', + type: 'string', + default: 'data', + displayOptions: { + show: { + operation: [ + 'update', + ], + resource: [ + 'profile', + ], + sendResume: [ + true, + ], + }, + }, + description: `The resume in PDF or DOCX format.`, + }, +] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/HumanticAI/humanticai.png b/packages/nodes-base/nodes/HumanticAI/humanticai.png new file mode 100644 index 0000000000..efdfbdbe7f Binary files /dev/null and b/packages/nodes-base/nodes/HumanticAI/humanticai.png differ diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 136dac82a8..7403c6a4ff 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -99,6 +99,7 @@ "dist/credentials/HubspotApi.credentials.js", "dist/credentials/HubspotDeveloperApi.credentials.js", "dist/credentials/HubspotOAuth2Api.credentials.js", + "dist/credentials/HumanticAiApi.credentials.js", "dist/credentials/HunterApi.credentials.js", "dist/credentials/Imap.credentials.js", "dist/credentials/IntercomApi.credentials.js", @@ -307,6 +308,7 @@ "dist/nodes/HttpRequest.node.js", "dist/nodes/Hubspot/Hubspot.node.js", "dist/nodes/Hubspot/HubspotTrigger.node.js", + "dist/nodes/HumanticAI/HumanticAi.node.js", "dist/nodes/Hunter/Hunter.node.js", "dist/nodes/If.node.js", "dist/nodes/Intercom/Intercom.node.js",