UNPKG

n8n-nodes-orderful

Version:
370 lines 15.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderfulTransaction = void 0; const n8n_workflow_1 = require("n8n-workflow"); class OrderfulTransaction { constructor() { this.description = { displayName: 'Orderful Transaction', name: 'orderfulTransaction', icon: 'file:orderful.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Manage EDI transactions in Orderful', defaults: { name: 'Orderful Transaction', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'orderfulApi', required: true, }, ], requestDefaults: { baseURL: '={{$credentials.baseUrl}}', headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: 'Bearer {{$credentials.apiKey}}', }, }, properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Confirm Delivery', value: 'confirmDelivery', description: 'Confirm delivery of transactions', action: 'Confirm delivery of transactions', }, { name: 'Create', value: 'create', description: 'Create a new transaction', action: 'Create a transaction', }, { name: 'Create Acknowledgment', value: 'createAcknowledgment', description: 'Create an acknowledgment', action: 'Create an acknowledgment', }, { name: 'Get', value: 'get', description: 'Get a specific transaction', action: 'Get a transaction', }, { name: 'Get Message', value: 'getMessage', description: 'Get a transaction message', action: 'Get a transaction message', }, { name: 'List', value: 'list', description: 'List transactions', action: 'List transactions', }, ], default: 'list', }, { displayName: 'Transaction ID', name: 'transactionId', type: 'string', displayOptions: { show: { operation: ['get', 'getMessage'], }, }, default: '', description: 'The ID of the transaction', required: true, }, { displayName: 'Transaction Type', name: 'transactionType', type: 'options', displayOptions: { show: { operation: ['create'], }, }, options: [ { name: 'Purchase Order (850)', value: '850', }, { name: 'Purchase Order Acknowledgment (855)', value: '855', }, { name: 'Ship Notice/Manifest (856)', value: '856', }, { name: 'Invoice (810)', value: '810', }, { name: 'Motor Carrier Load Tender (204)', value: '204', }, { name: 'Response to Load Tender (990)', value: '990', }, { name: 'Transportation Carrier Shipment Status (214)', value: '214', }, ], default: '850', description: 'The type of transaction to create', }, { displayName: 'Transaction Data', name: 'transactionData', type: 'string', typeOptions: { rows: 4, }, displayOptions: { show: { operation: ['create'], }, }, default: '{}', description: 'The transaction data in JSON format', required: true, }, { displayName: 'Partner ID', name: 'partnerId', type: 'string', displayOptions: { show: { operation: ['create'], }, }, default: '', description: 'The ID of the trading partner', required: true, }, { displayName: 'Additional Fields', name: 'additionalFields', type: 'collection', placeholder: 'Add Field', displayOptions: { show: { operation: ['list'], }, }, default: {}, options: [ { displayName: 'End Date', name: 'endDate', type: 'dateTime', default: '', description: 'Filter transactions created before this date', }, { displayName: 'Limit', name: 'limit', type: 'number', typeOptions: { minValue: 1, }, default: 50, description: 'Max number of results to return', }, { displayName: 'Start Date', name: 'startDate', type: 'dateTime', default: '', description: 'Filter transactions created after this date', }, { displayName: 'Status', name: 'status', type: 'options', options: [ { name: 'Pending', value: 'pending', }, { name: 'Processed', value: 'processed', }, { name: 'Failed', value: 'failed', }, { name: 'Delivered', value: 'delivered', }, ], default: 'pending', description: 'Filter by transaction status', }, { displayName: 'Transaction Type', name: 'transactionType', type: 'string', default: '', description: 'Filter by transaction type (e.g., 850, 810)', }, ], }, { displayName: 'Transaction IDs', name: 'transactionIds', type: 'string', displayOptions: { show: { operation: ['confirmDelivery'], }, }, default: '', description: 'Comma-separated list of transaction IDs to confirm delivery', required: true, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); let responseData = {}; if (operation === 'create') { const transactionType = this.getNodeParameter('transactionType', i); const transactionData = this.getNodeParameter('transactionData', i); const partnerId = this.getNodeParameter('partnerId', i); let parsedData; try { parsedData = JSON.parse(transactionData); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid JSON in transaction data'); } const body = { type: transactionType, data: parsedData, partner_id: partnerId, }; const options = { method: 'POST', url: '/transactions', body, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } else if (operation === 'list') { const additionalFields = this.getNodeParameter('additionalFields', i); const qs = {}; if (additionalFields.limit) { qs.limit = additionalFields.limit; } if (additionalFields.status) { qs.status = additionalFields.status; } if (additionalFields.transactionType) { qs.type = additionalFields.transactionType; } if (additionalFields.startDate) { qs.start_date = additionalFields.startDate; } if (additionalFields.endDate) { qs.end_date = additionalFields.endDate; } const options = { method: 'GET', url: '/transactions', qs, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } else if (operation === 'get') { const transactionId = this.getNodeParameter('transactionId', i); const options = { method: 'GET', url: `/transactions/${transactionId}`, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } else if (operation === 'getMessage') { const transactionId = this.getNodeParameter('transactionId', i); const options = { method: 'GET', url: `/transactions/${transactionId}/message`, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } else if (operation === 'confirmDelivery') { const transactionIds = this.getNodeParameter('transactionIds', i); const idArray = transactionIds.split(',').map((id) => id.trim()); const body = { transaction_ids: idArray, }; const options = { method: 'POST', url: '/transactions/confirm-delivery', body, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } else if (operation === 'createAcknowledgment') { const transactionId = this.getNodeParameter('transactionId', i); const body = { transaction_id: transactionId, }; const options = { method: 'POST', url: '/acknowledgments', body, json: true, }; responseData = await this.helpers.requestWithAuthentication.call(this, 'orderfulApi', options); } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, }); } } return [returnData]; } } exports.OrderfulTransaction = OrderfulTransaction; //# sourceMappingURL=OrderfulTransaction.node.js.map