UNPKG

@poli-digital/n8n-nodes-poli

Version:
202 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateContact = void 0; const n8n_workflow_1 = require("n8n-workflow"); const transport_1 = require("./transport"); const parameterUtils_1 = require("./utils/parameterUtils"); class CreateContact { constructor() { this.description = { displayName: 'Create Contact', name: 'createContact', icon: 'file:poli.svg', group: ['output'], version: 1, description: 'Create a new contact in Poli', defaults: { name: 'Create Contact' }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'poliApi', required: true }, ], properties: [ { displayName: 'Account UUID', name: 'accountId', type: 'string', default: '', required: true }, { displayName: 'Name', name: 'name', type: 'string', default: '', required: true }, { displayName: 'Phone (com DDD)', name: 'phone', type: 'string', default: '', required: true }, { displayName: 'E-mail', name: 'email', type: 'string', default: '' }, { displayName: 'CPF/CNPJ', name: 'doc', type: 'string', default: '' }, { displayName: 'Picture File UUID', name: 'pictureFileId', type: 'string', default: '', description: 'Opcional. file_id de um arquivo já enviado para usar como foto' }, { displayName: 'Tag UUID', name: 'tagUuid', type: 'string', default: '', description: 'Opcional. Uma tag para associar ao contato' }, { displayName: 'Contact Account UUID', name: 'companyUuid', type: 'string', default: '', description: 'Opcional. Empresa para relacionar com o contato. Enviada em companies[]. Troque para attributes.company_uuid se sua API exigir.' }, { displayName: 'Addresses', name: 'addresses', type: 'fixedCollection', placeholder: 'Add Address', typeOptions: { multipleValues: true }, default: {}, options: [ { name: 'address', displayName: 'Address', values: [ { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Headquarter', value: 'Headquarter' }, { name: 'Billing', value: 'Billing' }, { name: 'Shipping', value: 'Shipping' }, { name: 'Other', value: 'Other' }, ], default: 'Headquarter' }, { displayName: 'Country Code', name: 'country_code', type: 'string', default: 'BRA' }, { displayName: 'Country', name: 'country', type: 'string', default: 'Brasil' }, { displayName: 'Postal Code (CEP)', name: 'postal_code', type: 'string', default: '' }, { displayName: 'State (UF)', name: 'state', type: 'string', default: '' }, { displayName: 'City', name: 'city', type: 'string', default: '' }, { displayName: 'Street', name: 'street', type: 'string', default: '' }, { displayName: 'Number', name: 'number', type: 'string', default: '' }, { displayName: 'Neighborhood', name: 'neighborhood', type: 'string', default: '' }, { displayName: 'Complement', name: 'complement', type: 'string', default: '' }, ], }, ], }, { displayName: 'Contact Channels', name: 'contactChannels', type: 'fixedCollection', placeholder: 'Add Contact Channel', typeOptions: { multipleValues: true }, default: {}, description: 'Canais de contato para associar ao contato (WhatsApp, SMS, etc.)', options: [ { name: 'channel', displayName: 'Contact Channel', values: [ { displayName: 'Phone (com DDD)', name: 'uid', type: 'string', default: '', required: true, description: 'Número de telefone que será usado como UID do canal', }, { displayName: 'Provider', name: 'provider', type: 'options', options: [ { name: 'WhatsApp', value: 'WHATSAPP' }, { name: 'SMS', value: 'SMS' }, { name: 'Voice', value: 'VOICE' }, ], default: 'WHATSAPP', description: 'Provedor do canal de comunicação', }, { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Default', value: 'DEFAULT' }, { name: 'Business', value: 'BUSINESS' }, ], default: 'DEFAULT', description: 'Tipo do canal', }, ], }, ], }, ], }; } async execute() { var _a, _b, _c, _d, _e, _f, _g; const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const accountId = (0, parameterUtils_1.getParameterSafe)(this, 'accountId', i, '', true); const name = (0, parameterUtils_1.getParameterSafe)(this, 'name', i, '', true); const phone = (0, parameterUtils_1.getParameterSafe)(this, 'phone', i, '', true); const email = (0, parameterUtils_1.getParameterSafe)(this, 'email', i, ''); const doc = (0, parameterUtils_1.getParameterSafe)(this, 'doc', i, ''); const pictureFileId = (0, parameterUtils_1.getParameterSafe)(this, 'pictureFileId', i, ''); const tagUuid = (0, parameterUtils_1.getParameterSafe)(this, 'tagUuid', i, ''); const companyUuid = (0, parameterUtils_1.getParameterSafe)(this, 'companyUuid', i, ''); const addressesParam = (0, parameterUtils_1.getParameterSafe)(this, 'addresses', i, {}); const contactChannelsParam = (0, parameterUtils_1.getParameterSafe)(this, 'contactChannels', i, {}); const body = { type: 'PERSON', attributes: { name, phone, }, }; if (email) body.attributes.email = email; if (doc) body.attributes.doc = doc; if (pictureFileId) body.attributes.picture = { file_id: pictureFileId }; if (tagUuid) body.tags = [{ uuid: tagUuid }]; if ((_a = contactChannelsParam.channel) === null || _a === void 0 ? void 0 : _a.length) { body.contact_channels = contactChannelsParam.channel.map(c => ({ uid: c.uid, provider: c.provider || 'WHATSAPP', type: c.type || 'DEFAULT', })); if (contactChannelsParam.channel[0].uid) { body.attributes.phone = contactChannelsParam.channel[0].uid; } } if (companyUuid) { body.companies = [{ uuid: companyUuid }]; } if ((_b = addressesParam === null || addressesParam === void 0 ? void 0 : addressesParam.address) === null || _b === void 0 ? void 0 : _b.length) { body.addresses = addressesParam.address.map(a => ({ type: a.type || 'Headquarter', country_code: a.country_code || 'BRA', country: a.country || 'Brasil', postal_code: a.postal_code || '', state: a.state || '', city: a.city || '', street: a.street || '', number: a.number || '', neighborhood: a.neighborhood || '', complement: a.complement || '', })); } const endpoint = `/accounts/${accountId}/contacts?include=*`; let responseData; try { responseData = await transport_1.apiRequest.call(this, 'POST', endpoint, body); } catch (error) { const status = (error === null || error === void 0 ? void 0 : error.statusCode) || ((_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.statusCode); const message = (error === null || error === void 0 ? void 0 : error.message) || ((_e = (_d = error === null || error === void 0 ? void 0 : error.response) === null || _d === void 0 ? void 0 : _d.body) === null || _e === void 0 ? void 0 : _e.message) || ((_g = (_f = error === null || error === void 0 ? void 0 : error.response) === null || _f === void 0 ? void 0 : _f.body) === null || _g === void 0 ? void 0 : _g.error) || ''; if (status === 409 || status === 422 || /exists|duplicado|duplicate|already/i.test(message)) { responseData = { message: 'Contato já existe', details: message || 'Registro duplicado', phone, email, doc, }; } else { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } returnData.push({ json: responseData }); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } return [returnData]; } } exports.CreateContact = CreateContact; //# sourceMappingURL=CreateContact.operation.js.map