n8n-nodes-chatwoot
Version:
This is an n8n community node. It lets you use ChatWoot in your n8n workflows.
98 lines • 4.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resourceContact = void 0;
const GenericFunctions_1 = require("../GenericFunctions");
async function resourceContact(operation, items, i) {
let accessToken = this.getNodeParameter('accessToken', i, '');
let accountId = this.getNodeParameter('accountId', i, '');
if (!accountId) {
const credentials = await this.getCredentials('chatWootTokenApi');
if (credentials) {
accessToken = accessToken || credentials.accessToken;
accountId = accountId || credentials.accountId;
}
}
const headers = {
'api_access_token': accessToken,
};
const apiVersionPrefix = '/api/v1';
let responseData;
if (operation === 'contactUpdate') {
const body = {
name: this.getNodeParameter('name', i, null),
inbox_id: this.getNodeParameter('inboxId', i, null),
phone_number: this.getNodeParameter('phoneNumber', i, null),
email: this.getNodeParameter('email', i, null),
source_id: this.getNodeParameter('sourceId', i, null),
identifier: this.getNodeParameter('contactIdentifier', i, null),
};
const parCustomAttributes = this.getNodeParameter('customAttributes', i, null);
if (parCustomAttributes && parCustomAttributes.attribute) {
const data = {};
const atts = parCustomAttributes.attribute;
atts.map(property => {
data[property.key] = property.value;
});
body.custom_attributes = data;
}
let endpoint = apiVersionPrefix + '/accounts/:account_id/contacts/:contact_id';
endpoint = endpoint.replace(':account_id', accountId);
endpoint = endpoint.replace(':contact_id', this.getNodeParameter('contactId', i));
responseData = await GenericFunctions_1.apiRequest.call(this, 'PATCH', endpoint, body, {}, headers);
}
else if (operation === 'contactCreate') {
const body = {};
const parName = this.getNodeParameter('name', i, null);
if (parName) {
body.name = parName;
}
const parInboxId = this.getNodeParameter('inboxId', i, null);
if (parInboxId) {
body.inbox_id = parInboxId;
}
const parPhoneNumber = this.getNodeParameter('phoneNumber', i, null);
if (parPhoneNumber) {
body.phone_number = parPhoneNumber;
}
const parEmail = this.getNodeParameter('email', i, null);
if (parEmail) {
body.email = parEmail;
}
const parSourceId = this.getNodeParameter('sourceId', i, null);
if (parSourceId) {
body.source_id = parSourceId;
}
const parIdentifier = this.getNodeParameter('contactIdentifier', i, null);
if (parIdentifier) {
body.identifier = parIdentifier;
}
const parCustomAttributes = this.getNodeParameter('customAttributes', i, null);
if (parCustomAttributes && parCustomAttributes.attribute) {
const data = {};
const atts = parCustomAttributes.attribute;
atts.map(property => {
data[property.key] = property.value;
});
body.custom_attributes = data;
}
let endpoint = apiVersionPrefix + '/accounts/:account_id/contacts';
endpoint = endpoint.replace(':account_id', accountId);
responseData = await GenericFunctions_1.apiRequest.call(this, 'POST', endpoint, body, {}, headers);
}
else if (operation === 'contactSearch') {
const query = {};
query["q"] = this.getNodeParameter('contactSearchQuery', i);
let endpoint = apiVersionPrefix + '/accounts/:account_id/contacts/search';
endpoint = endpoint.replace(':account_id', accountId);
responseData = await GenericFunctions_1.apiRequest.call(this, 'GET', endpoint, {}, query, headers);
}
else if (operation === 'contactDetails') {
let endpoint = apiVersionPrefix + '/accounts/:account_id/contacts/:contact_id';
endpoint = endpoint.replace(':account_id', accountId);
endpoint = endpoint.replace(':contact_id', this.getNodeParameter('contactId', i));
responseData = await GenericFunctions_1.apiRequest.call(this, 'GET', endpoint, {}, {}, headers);
}
return responseData;
}
exports.resourceContact = resourceContact;
//# sourceMappingURL=ContactMethods.js.map