n8n-nodes-livo
Version:
n8n Livo App integration with triggers and actions for orders, products, customers, and repairs
262 lines (261 loc) • 13.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiveAppEvent = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class LiveAppEvent {
constructor() {
this.description = {
displayName: 'Livo App Event',
name: 'livoAppEvent',
icon: 'file:livo.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["eventCategory"]}} - {{$parameter["eventType"]}}',
description: 'Lắng nghe các sự kiện từ ứng dụng Livo (Đơn hàng, Khách hàng, Sản phẩm, Dịch vụ sửa chữa)',
defaults: {
name: 'Livo App Event',
},
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'App Name',
name: 'appName',
type: 'string',
default: 'Livo',
description: 'Tên ứng dụng (Livo)',
},
{
displayName: 'Event Category',
name: 'eventCategory',
type: 'options',
options: [
{
name: 'Đơn hàng',
value: 'order',
description: 'Các sự kiện liên quan đến đơn hàng',
},
{
name: 'Khách hàng',
value: 'customer',
description: 'Các sự kiện liên quan đến khách hàng',
},
{
name: 'Sản phẩm',
value: 'product',
description: 'Các sự kiện liên quan đến sản phẩm',
},
{
name: 'Dịch vụ sửa chữa',
value: 'repair',
description: 'Các sự kiện liên quan đến dịch vụ sửa chữa',
},
],
default: 'order',
required: true,
},
{
displayName: 'Event Type',
name: 'eventType',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEventTypes',
},
default: '',
required: true,
displayOptions: {
show: {
eventCategory: ['order', 'customer', 'product', 'repair'],
},
},
},
],
};
this.methods = {
loadOptions: {
async getEventTypes() {
const eventCategory = this.getCurrentNodeParameter('eventCategory');
const eventTypes = {
order: [
{ name: 'Lấy danh sách đơn hàng', value: 'order.list' },
{ name: 'Chi tiết đơn hàng', value: 'order.detail' },
{ name: 'Tạo đơn hàng', value: 'order.created' },
{ name: 'Cập nhật đơn hàng', value: 'order.updated' },
{ name: 'Thanh toán đơn hàng', value: 'order.paid' },
{ name: 'Hủy đơn hàng', value: 'order.cancelled' },
{ name: 'Giao hàng thành công', value: 'order.delivered' },
],
customer: [
{ name: 'Lấy danh sách khách hàng', value: 'customer.list' },
{ name: 'Chi tiết khách hàng', value: 'customer.detail' },
{ name: 'Tạo khách hàng', value: 'customer.created' },
{ name: 'Cập nhật khách hàng', value: 'customer.updated' },
{ name: 'Xóa khách hàng', value: 'customer.deleted' },
],
product: [
{ name: 'Lấy danh sách sản phẩm', value: 'product.list' },
{ name: 'Chi tiết sản phẩm', value: 'product.detail' },
{ name: 'Tạo sản phẩm', value: 'product.created' },
{ name: 'Cập nhật sản phẩm', value: 'product.updated' },
{ name: 'Xóa sản phẩm', value: 'product.deleted' },
{ name: 'Nhập kho', value: 'product.stock_in' },
{ name: 'Xuất kho', value: 'product.stock_out' },
],
repair: [
{ name: 'Lấy danh sách phiếu sửa chữa', value: 'repair.list' },
{ name: 'Chi tiết phiếu sửa chữa', value: 'repair.detail' },
{ name: 'Tạo phiếu sửa chữa', value: 'repair.created' },
{ name: 'Cập nhật phiếu sửa chữa', value: 'repair.updated' },
{ name: 'Thanh toán phiếu sửa chữa', value: 'repair.paid' },
{ name: 'Xác nhận giao khách', value: 'repair.customer_confirmed' },
{ name: 'Hủy phiếu sửa chữa', value: 'repair.cancelled' },
],
};
return eventTypes[eventCategory] || [];
},
},
};
}
async webhook() {
const bodyData = this.getBodyData();
const headers = this.getHeaderData();
const query = this.getQueryData();
const req = this.getRequestObject();
const appName = this.getNodeParameter('appName');
const eventCategory = this.getNodeParameter('eventCategory');
const expectedEventType = this.getNodeParameter('eventType');
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,
};
}
const nodeInstance = new LiveAppEvent();
const outputData = nodeInstance.normalizeOutputData(eventData, eventCategory, receivedEvent, headers, query, req, appName);
return {
workflowData: [
[
{
json: outputData,
},
],
],
};
}
normalizeOutputData(eventData, eventCategory, receivedEvent, headers, query, req, appName) {
const baseData = {
app_name: appName,
event: receivedEvent,
event_time: new Date().toISOString(),
event_category: eventCategory,
id: eventData.id || eventData.order_id || eventData.repair_id || eventData.return_id || eventData.customer_id || eventData.product_id,
code: eventData.code || eventData.order_code || eventData.repair_code || eventData.return_code || eventData.customer_code || eventData.product_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,
raw_data: eventData,
headers: headers,
query: query,
webhook_url: req.url,
};
switch (eventCategory) {
case 'order':
return { ...baseData, ...this.getOrderSpecificData(eventData) };
case 'customer':
return { ...baseData, ...this.getCustomerSpecificData(eventData) };
case 'product':
return { ...baseData, ...this.getProductSpecificData(eventData) };
case 'repair':
return { ...baseData, ...this.getRepairSpecificData(eventData) };
default:
return baseData;
}
}
getOrderSpecificData(eventData) {
return {
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,
};
}
getCustomerSpecificData(eventData) {
return {
customer_id: eventData.customer_id || eventData.ma_khach_hang,
customer_name: eventData.customer_name || eventData.ten_khach_hang,
full_name: eventData.full_name || eventData.ho_ten_day_du,
phone: eventData.phone || eventData.so_dien_thoai,
email: eventData.email || eventData.email,
address: eventData.address || eventData.dia_chi,
customer_type: eventData.customer_type || eventData.loai_khach_hang,
customer_group: eventData.customer_group || eventData.nhom_khach_hang,
customer_level: eventData.customer_level || eventData.cap_do_khach_hang,
total_orders: eventData.total_orders || eventData.tong_don_hang,
total_spent: eventData.total_spent || eventData.tong_chi_tieu,
last_order_date: eventData.last_order_date || eventData.don_hang_cuoi,
customer_source: eventData.customer_source || eventData.nguon_khach_hang,
marketing_consent: eventData.marketing_consent || eventData.dong_y_marketing,
};
}
getProductSpecificData(eventData) {
return {
product_id: eventData.product_id || eventData.ma_san_pham,
product_name: eventData.product_name || eventData.ten_san_pham,
product_code: eventData.product_code || eventData.ma_san_pham,
category: eventData.category || eventData.danh_muc,
brand: eventData.brand || eventData.thuong_hieu,
stock_quantity: eventData.stock_quantity || eventData.ton_kho,
unit_price: eventData.unit_price || eventData.gia_ban,
cost_price: eventData.cost_price || eventData.gia_von,
return_id: eventData.return_id || eventData.ma_tra_hang,
original_order_id: eventData.original_order_id || eventData.don_hang_goc,
return_reason: eventData.return_reason || eventData.ly_do_tra_hang,
return_amount: eventData.return_amount || eventData.gia_tri_tra,
};
}
getRepairSpecificData(eventData) {
return {
repair_id: eventData.repair_id || eventData.ma_phieu_sua,
repair_code: eventData.repair_code || eventData.ma_phieu,
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,
device_info: eventData.device_info || eventData.thong_tin_thiet_bi,
device_type: eventData.device_type || eventData.loai_thiet_bi,
device_brand: eventData.device_brand || eventData.hang_thiet_bi,
issue_description: eventData.issue_description || eventData.mo_ta_su_co,
issue_type: eventData.issue_type || eventData.loai_su_co,
technician: eventData.technician || eventData.ky_thuat_vien,
estimated_time: eventData.estimated_time || eventData.thoi_gian_du_kien,
repair_status: eventData.repair_status || eventData.trang_thai_sua_chua,
repair_cost: eventData.repair_cost || eventData.chi_phi_sua_chua,
total_cost: eventData.total_cost || eventData.tong_chi_phi,
paid_amount: eventData.paid_amount || eventData.da_thanh_toan,
};
}
}
exports.LiveAppEvent = LiveAppEvent;