UNPKG

@contaazul/n8n-nodes-contaazul

Version:

Node para integração com a API da Conta Azul no n8n

90 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSalesByFilter = getSalesByFilter; exports.getSaleById = getSaleById; exports.createSale = createSale; async function getSalesByFilter() { var _a; const busca_textual = this.getNodeParameter('busca_textual_venda', 0, ''); const pagina = this.getNodeParameter('pagina_venda', 0, 1); const tamanho_pagina = this.getNodeParameter('tamanho_pagina_venda', 0, 10); const qs = { pagina, tamanho_pagina, }; if (busca_textual) { qs.busca_textual = busca_textual; } const responseData = await ((_a = this.helpers.requestOAuth2) === null || _a === void 0 ? void 0 : _a.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: 'https://api-v2.contaazul.com/v1/venda/busca', qs, json: true, })); return this.helpers.returnJsonArray(responseData.itens); } async function getSaleById() { var _a; const saleId = this.getNodeParameter('saleId', 0); const responseData = await ((_a = this.helpers.requestOAuth2) === null || _a === void 0 ? void 0 : _a.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: `https://api-v2.contaazul.com/v1/venda/${saleId}`, json: true, })); return this.helpers.returnJsonArray([responseData]); } 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 Error(`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('observacoes', 0, ''); if (observacoes) body.observacoes = observacoes; const observacoes_pagamento = this.getNodeParameter('observacoes_pagamento', 0, ''); if (observacoes_pagamento) body.observacoes_pagamento = observacoes_pagamento; const response = await this.helpers.requestOAuth2.call(this, 'contaAzulOAuth2Api', { method: 'POST', url: 'https://api-v2.contaazul.com/v1/venda', body, json: true, }); return [{ json: response }]; } //# sourceMappingURL=vendas.js.map