UNPKG

@contaazul/n8n-nodes-contaazul

Version:

Node for integration with Conta Azul API in n8n

102 lines 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSalesByFilter = getSalesByFilter; exports.getSaleById = getSaleById; exports.createSale = createSale; const n8n_workflow_1 = require("n8n-workflow"); async function getSalesByFilter() { const busca_textual = this.getNodeParameter('saleAdditionalFields.busca_textual_venda', 0, ''); const pagina = this.getNodeParameter('saleAdditionalFields.pagina_venda', 0, 1); const tamanho_pagina = this.getNodeParameter('saleAdditionalFields.tamanho_pagina_venda', 0, 10); const qs = { pagina, tamanho_pagina, }; if (busca_textual) { qs.busca_textual = busca_textual; } const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: 'https://api-v2.contaazul.com/v1/venda/busca', qs, json: true, }); const items = (responseData.itens || []).map((item) => ({ json: item, pairedItem: { item: 0, }, })); return items; } async function getSaleById() { const saleId = this.getNodeParameter('saleId', 0); const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: `https://api-v2.contaazul.com/v1/venda/${saleId}`, json: true, }); return [ { json: responseData, pairedItem: { item: 0, }, }, ]; } async function createSale() { const itensRaw = this.getNodeParameter('itens', 0); const itens = (itensRaw.item || []).map((i) => ({ id: i.id, descricao: i.descricao, quantidade: i.quantidade, valor: i.valor, })); const parcelasRaw = this.getNodeParameter('parcelas', 0); const parcelas = (parcelasRaw.parcela || []).map((p) => ({ data_vencimento: typeof p.data_vencimento === 'string' && p.data_vencimento.includes('T') ? p.data_vencimento.split('T')[0] : p.data_vencimento, valor: p.valor, ...(p.descricao ? { descricao: p.descricao } : {}), })); const totalItens = itens.reduce((acc, item) => acc + item.quantidade * item.valor, 0); const totalParcelas = parcelas.reduce((acc, parcela) => acc + parcela.valor, 0); if (totalItens !== totalParcelas) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The total value of items (R$ ${totalItens}) must be equal to the total value of installments (R$ ${totalParcelas}).`); } const opcao_condicao_pagamento = this.getNodeParameter('opcao_condicao_pagamento', 0); const tipo_pagamento = this.getNodeParameter('tipo_pagamento', 0); const condicao_pagamento = { opcao_condicao_pagamento, tipo_pagamento, parcelas, }; let data_venda = this.getNodeParameter('data_venda', 0); if (typeof data_venda === 'string' && data_venda.includes('T')) { data_venda = data_venda.split('T')[0]; } const body = { id_cliente: this.getNodeParameter('id_cliente', 0), numero: this.getNodeParameter('numero', 0), situacao: this.getNodeParameter('situacao', 0), data_venda, itens, condicao_pagamento, }; const observacoes = this.getNodeParameter('saleCreateAdditionalFields.observacoes', 0, ''); if (observacoes) body.observacoes = observacoes; const observacoes_pagamento = this.getNodeParameter('saleCreateAdditionalFields.observacoes_pagamento', 0, ''); if (observacoes_pagamento) body.observacoes_pagamento = observacoes_pagamento; const response = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'POST', url: 'https://api-v2.contaazul.com/v1/venda', body, json: true, }); return [{ json: response }]; } //# sourceMappingURL=vendas.js.map