UNPKG

n8n-nodes-livo

Version:

n8n Livo App integration with triggers and actions for orders, products, customers, and repairs

243 lines (242 loc) 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LivoOrderEvents = void 0; const n8n_workflow_1 = require("n8n-workflow"); class LivoOrderEvents { constructor() { this.description = { displayName: 'Livo Order Events', name: 'livoOrderEvents', icon: 'file:order.svg', group: ['trigger'], version: 1, subtitle: '={{$parameter["eventType"]}}', description: 'Lắng nghe các sự kiện đơn hàng từ ứng dụng Livo', defaults: { name: 'Livo Order Events', }, inputs: [], outputs: ['main'], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Event Type', name: 'eventType', type: 'options', options: [ { name: 'Lấy danh sách đơn hàng', value: 'order.list', description: 'Trigger khi lấy danh sách đơn hàng', }, { name: 'Chi tiết đơn hàng', value: 'order.detail', description: 'Trigger khi xem chi tiết đơn hàng', }, { name: 'Tạo đơn hàng', value: 'order.created', description: 'Trigger khi tạo mới đơn hàng', }, { name: 'Cập nhật đơn hàng', value: 'order.updated', description: 'Trigger khi cập nhật đơn hàng', }, { name: 'Thanh toán đơn hàng', value: 'order.paid', description: 'Trigger khi thanh toán đơn hàng', }, { name: 'Hủy đơn hàng', value: 'order.cancelled', description: 'Trigger khi hủy đơn hàng', }, { name: 'Giao hàng thành công', value: 'order.delivered', description: 'Trigger khi giao hàng thành công', }, ], default: 'order.created', required: true, }, { displayName: 'Advanced Filters', name: 'advancedFilters', type: 'collection', placeholder: 'Add Filter', default: {}, options: [ { displayName: 'Customer ID', name: 'customerId', type: 'string', default: '', description: 'Lọc theo mã khách hàng', }, { displayName: 'Min Amount', name: 'minAmount', type: 'number', default: 0, description: 'Giá trị đơn hàng tối thiểu', }, { displayName: 'Max Amount', name: 'maxAmount', type: 'number', default: 0, description: 'Giá trị đơn hàng tối đa (0 = không giới hạn)', }, { displayName: 'Shipping Location', name: 'shippingLocation', type: 'string', default: '', description: 'Lọc theo địa điểm giao hàng', }, { displayName: 'Assigned Staff', name: 'assignedStaff', type: 'string', default: '', description: 'Lọc theo nhân viên phụ trách', }, { displayName: 'Branch', name: 'branch', type: 'string', default: '', description: 'Lọc theo chi nhánh', }, ], }, { displayName: 'Authentication', name: 'authentication', type: 'collection', placeholder: 'Add Authentication', default: {}, options: [ { displayName: 'Secret Token', name: 'secretToken', type: 'string', typeOptions: { password: true, }, default: '', description: 'Token bí mật để xác thực webhook', }, { displayName: 'Header Name', name: 'headerName', type: 'string', default: 'X-Webhook-Secret', description: 'Tên header chứa token xác thực', }, ], }, ], }; } async webhook() { var _a; const bodyData = this.getBodyData(); const headers = this.getHeaderData(); const query = this.getQueryData(); const req = this.getRequestObject(); const expectedEventType = this.getNodeParameter('eventType'); const advancedFilters = this.getNodeParameter('advancedFilters', {}); const authentication = this.getNodeParameter('authentication', {}); if (authentication.secretToken) { const headerName = authentication.headerName || 'X-Webhook-Secret'; const receivedToken = headers[headerName.toLowerCase()] || headers[headerName]; if (!receivedToken || receivedToken !== authentication.secretToken) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Xác thực webhook thất bại: Token không hợp lệ'); } } if (!bodyData || typeof bodyData !== 'object') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Dữ liệu webhook không hợp lệ'); } const eventData = bodyData; const receivedEvent = eventData.event || headers['x-event-type']; if (receivedEvent !== expectedEventType) { return { noWebhookResponse: true, }; } if (advancedFilters.customerId && eventData.customer_id !== advancedFilters.customerId) { return { noWebhookResponse: true }; } if (advancedFilters.minAmount && (eventData.total_amount || 0) < advancedFilters.minAmount) { return { noWebhookResponse: true }; } if (advancedFilters.maxAmount && advancedFilters.maxAmount > 0 && (eventData.total_amount || 0) > advancedFilters.maxAmount) { return { noWebhookResponse: true }; } if (advancedFilters.shippingLocation && !((_a = eventData.shipping_address) === null || _a === void 0 ? void 0 : _a.includes(advancedFilters.shippingLocation))) { return { noWebhookResponse: true }; } if (advancedFilters.assignedStaff && eventData.assigned_staff !== advancedFilters.assignedStaff) { return { noWebhookResponse: true }; } if (advancedFilters.branch && eventData.branch !== advancedFilters.branch) { return { noWebhookResponse: true }; } const outputData = { app_name: 'Livo', event: receivedEvent, event_time: new Date().toISOString(), event_category: 'order', id: eventData.id || eventData.order_id, code: eventData.code || eventData.order_code, status: eventData.status || eventData.trang_thai, created_date: eventData.created_date || eventData.ngay_tao, updated_date: eventData.updated_date || eventData.ngay_cap_nhat, assigned_staff: eventData.assigned_staff || eventData.nhan_vien_phu_trach, branch: eventData.branch || eventData.chi_nhanh, notes: eventData.notes || eventData.ghi_chu, customer_id: eventData.customer_id || eventData.ma_khach_hang, customer_name: eventData.customer_name || eventData.ten_khach_hang, customer_phone: eventData.customer_phone || eventData.so_dien_thoai, customer_email: eventData.customer_email || eventData.email, customer_address: eventData.customer_address || eventData.dia_chi_khach_hang, total_amount: eventData.total_amount || eventData.tong_tien, paid_amount: eventData.paid_amount || eventData.da_thanh_toan, discount_amount: eventData.discount_amount || eventData.giam_gia, items: eventData.items || eventData.san_pham || [], quantity: eventData.quantity || eventData.so_luong, shipping_address: eventData.shipping_address || eventData.dia_chi_giao_hang, shipping_method: eventData.shipping_method || eventData.phuong_thuc_giao_hang, delivery_date: eventData.delivery_date || eventData.ngay_giao_hang, raw_data: eventData, headers: headers, query: query, webhook_url: req.url, }; return { workflowData: [ [ { json: outputData, }, ], ], }; } } exports.LivoOrderEvents = LivoOrderEvents;