n8n-nodes-livo
Version:
n8n Livo App integration with triggers and actions for orders, products, customers, and repairs
243 lines (242 loc) • 11.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LivoCustomerEvents = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class LivoCustomerEvents {
constructor() {
this.description = {
displayName: 'Livo Customer Events',
name: 'livoCustomerEvents',
icon: 'file:customer.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["eventType"]}}',
description: 'Lắng nghe các sự kiện khách hàng từ ứng dụng Livo',
defaults: {
name: 'Livo Customer 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 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,
},
{
displayName: 'Advanced Filters',
name: 'advancedFilters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
options: [
{
displayName: 'Customer Type',
name: 'customerType',
type: 'string',
default: '',
description: 'Lọc theo loại khách hàng',
},
{
displayName: 'Location',
name: 'location',
type: 'string',
default: '',
description: 'Lọc theo địa điểm',
},
{
displayName: 'Customer Source',
name: 'customerSource',
type: 'string',
default: '',
description: 'Lọc theo nguồn khách 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.customerType && eventData.customer_type !== advancedFilters.customerType) {
return { noWebhookResponse: true };
}
if (advancedFilters.location && !((_a = eventData.address) === null || _a === void 0 ? void 0 : _a.includes(advancedFilters.location))) {
return { noWebhookResponse: true };
}
if (advancedFilters.customerSource && eventData.customer_source !== advancedFilters.customerSource) {
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: 'customer',
id: eventData.id || eventData.customer_id,
code: eventData.code || eventData.customer_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,
full_name: eventData.full_name || eventData.ho_ten_day_du,
first_name: eventData.first_name || eventData.ten,
last_name: eventData.last_name || eventData.ho,
phone: eventData.phone || eventData.so_dien_thoai,
email: eventData.email || eventData.email,
address: eventData.address || eventData.dia_chi,
ward: eventData.ward || eventData.phuong_xa,
district: eventData.district || eventData.quan_huyen,
city: eventData.city || eventData.tinh_thanh,
postal_code: eventData.postal_code || eventData.ma_buu_dien,
country: eventData.country || eventData.quoc_gia,
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,
vip_status: eventData.vip_status || eventData.tinh_trang_vip,
company_name: eventData.company_name || eventData.ten_cong_ty,
tax_code: eventData.tax_code || eventData.ma_so_thue,
business_type: eventData.business_type || eventData.loai_hinh_kinh_doanh,
account_status: eventData.account_status || eventData.trang_thai_tai_khoan,
registration_date: eventData.registration_date || eventData.ngay_dang_ky,
last_login: eventData.last_login || eventData.dang_nhap_cuoi,
login_count: eventData.login_count || eventData.so_lan_dang_nhap,
total_orders: eventData.total_orders || eventData.tong_don_hang,
total_spent: eventData.total_spent || eventData.tong_chi_tieu,
average_order_value: eventData.average_order_value || eventData.gia_tri_don_hang_tb,
last_order_date: eventData.last_order_date || eventData.don_hang_cuoi,
customer_source: eventData.customer_source || eventData.nguon_khach_hang,
referral_code: eventData.referral_code || eventData.ma_gioi_thieu,
marketing_consent: eventData.marketing_consent || eventData.dong_y_marketing,
newsletter_subscription: eventData.newsletter_subscription || eventData.dang_ky_newsletter,
preferences: eventData.preferences || eventData.sở_thich || {},
tags: eventData.tags || eventData.the_bai || [],
birthday: eventData.birthday || eventData.ngay_sinh,
gender: eventData.gender || eventData.gioi_tinh,
raw_data: eventData,
headers: headers,
query: query,
webhook_url: req.url,
};
return {
workflowData: [
[
{
json: outputData,
},
],
],
};
}
}
exports.LivoCustomerEvents = LivoCustomerEvents;