n8n-nodes-livo
Version:
n8n Livo App integration with triggers and actions for orders, products, customers, and repairs
169 lines (168 loc) • 8.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomerNode = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class CustomerNode {
constructor() {
this.description = {
displayName: 'Khách hàng',
name: 'customerNode',
icon: 'file:customer.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["event"]}}',
description: 'Trigger webhook events cho khách hàng (tạo mới, cập nhật thông tin)',
defaults: {
name: 'Khách hàng',
},
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'Lấy danh sách khách hàng',
value: 'customer.list',
description: 'Trigger khi lấy danh sách khách hàng',
},
{
name: 'Chi tiết khách hàng',
value: 'customer.detail',
description: 'Trigger khi xem chi tiết khách hàng',
},
{
name: 'Tạo khách hàng',
value: 'customer.created',
description: 'Trigger khi tạo mới khách hàng',
},
{
name: 'Cập nhật khách hàng',
value: 'customer.updated',
description: 'Trigger khi cập nhật thông tin khách hàng',
},
{
name: 'Xóa khách hàng',
value: 'customer.deleted',
description: 'Trigger khi xóa khách hàng',
},
],
default: 'customer.created',
required: true,
}
],
};
}
async webhook() {
var _a;
const bodyData = this.getBodyData();
const headers = this.getHeaderData();
const query = this.getQueryData();
const req = this.getRequestObject();
const expectedEvent = this.getNodeParameter('event');
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 customerData = bodyData;
const receivedEvent = customerData.event || headers['x-event-type'];
if (receivedEvent !== expectedEvent) {
return { noWebhookResponse: true };
}
if (advancedFilters.customerType && customerData.customer_type !== advancedFilters.customerType) {
return { noWebhookResponse: true };
}
if (advancedFilters.location && !((_a = customerData.address) === null || _a === void 0 ? void 0 : _a.includes(advancedFilters.location))) {
return { noWebhookResponse: true };
}
if (advancedFilters.customerSource && customerData.customer_source !== advancedFilters.customerSource) {
return { noWebhookResponse: true };
}
if (advancedFilters.assignedStaff && customerData.assigned_staff !== advancedFilters.assignedStaff) {
return { noWebhookResponse: true };
}
if (advancedFilters.branch && customerData.branch !== advancedFilters.branch) {
return { noWebhookResponse: true };
}
const outputData = {
event: receivedEvent,
event_time: new Date().toISOString(),
event_category: 'customer',
customer_id: customerData.customer_id || customerData.ma_khach_hang,
customer_code: customerData.customer_code || customerData.ma_khach_hang,
customer_name: customerData.customer_name || customerData.ten_khach_hang,
full_name: customerData.full_name || customerData.ho_ten_day_du,
first_name: customerData.first_name || customerData.ten,
last_name: customerData.last_name || customerData.ho,
phone: customerData.phone || customerData.so_dien_thoai,
email: customerData.email || customerData.email,
address: customerData.address || customerData.dia_chi,
ward: customerData.ward || customerData.phuong_xa,
district: customerData.district || customerData.quan_huyen,
city: customerData.city || customerData.tinh_thanh,
postal_code: customerData.postal_code || customerData.ma_buu_dien,
country: customerData.country || customerData.quoc_gia,
customer_type: customerData.customer_type || customerData.loai_khach_hang,
customer_group: customerData.customer_group || customerData.nhom_khach_hang,
customer_level: customerData.customer_level || customerData.cap_do_khach_hang,
vip_status: customerData.vip_status || customerData.tinh_trang_vip,
company_name: customerData.company_name || customerData.ten_cong_ty,
tax_code: customerData.tax_code || customerData.ma_so_thue,
business_type: customerData.business_type || customerData.loai_hinh_kinh_doanh,
account_status: customerData.account_status || customerData.trang_thai_tai_khoan,
registration_date: customerData.registration_date || customerData.ngay_dang_ky,
last_login: customerData.last_login || customerData.dang_nhap_cuoi,
login_count: customerData.login_count || customerData.so_lan_dang_nhap,
total_orders: customerData.total_orders || customerData.tong_don_hang,
total_spent: customerData.total_spent || customerData.tong_chi_tieu,
average_order_value: customerData.average_order_value || customerData.gia_tri_don_hang_tb,
last_order_date: customerData.last_order_date || customerData.don_hang_cuoi,
customer_source: customerData.customer_source || customerData.nguon_khach_hang,
referral_code: customerData.referral_code || customerData.ma_gioi_thieu,
marketing_consent: customerData.marketing_consent || customerData.dong_y_marketing,
newsletter_subscription: customerData.newsletter_subscription || customerData.dang_ky_newsletter,
preferences: customerData.preferences || customerData.sở_thich || {},
tags: customerData.tags || customerData.the_bai || [],
notes: customerData.notes || customerData.ghi_chu,
birthday: customerData.birthday || customerData.ngay_sinh,
gender: customerData.gender || customerData.gioi_tinh,
created_date: customerData.created_date || customerData.ngay_tao,
updated_date: customerData.updated_date || customerData.ngay_cap_nhat,
assigned_staff: customerData.assigned_staff || customerData.nhan_vien_phu_trach,
branch: customerData.branch || customerData.chi_nhanh,
created_by: customerData.created_by || customerData.nguoi_tao,
raw_data: customerData,
headers: headers,
query: query,
webhook_url: req.url,
};
return {
workflowData: [
[
{
json: outputData,
},
],
],
};
}
}
exports.CustomerNode = CustomerNode;