n8n-nodes-livo
Version:
n8n Livo App integration with triggers and actions for orders, products, customers, and repairs
167 lines (166 loc) • 7.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LivoCustomerTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class LivoCustomerTrigger {
constructor() {
this.description = {
displayName: 'Livo Customer Trigger',
name: 'livoCustomerTrigger',
icon: 'file:livo.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["event"]}}',
description: 'Starts the workflow when Livo customer events occur',
defaults: {
name: 'Livo Customer Trigger',
},
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'Customer Created',
displayName: 'Customer Created',
value: 'customer.created',
description: 'Triggered when a new customer is created',
},
{
name: 'Customer Updated',
displayName: 'Customer Updated',
value: 'customer.updated',
description: 'Triggered when a customer is updated',
},
{
name: 'Customer Deleted',
displayName: 'Customer Deleted',
value: 'customer.deleted',
description: 'Triggered when a customer is deleted',
},
],
default: 'customer.created',
required: true,
description: 'The customer event that will trigger the webhook',
},
{
displayName: 'Additional Options',
name: 'additionalOptions',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Include Raw Data',
name: 'includeRawData',
type: 'boolean',
default: true,
description: 'Whether to include the raw webhook data in the output',
},
{
displayName: 'Webhook Secret',
name: 'webhookSecret',
type: 'string',
typeOptions: {
password: true,
},
default: '',
description: 'Optional secret for webhook verification',
},
{
displayName: 'Customer Type Filter',
name: 'customerTypeFilter',
type: 'string',
default: '',
description: 'Only trigger for specific customer type',
},
{
displayName: 'Location Filter',
name: 'locationFilter',
type: 'string',
default: '',
description: 'Only trigger for customers from specific location',
},
],
},
],
};
}
async webhook() {
var _a;
const bodyData = this.getBodyData();
const headers = this.getHeaderData();
const expectedEvent = this.getNodeParameter('event');
const additionalOptions = this.getNodeParameter('additionalOptions', {});
if (!bodyData || typeof bodyData !== 'object') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid webhook data received');
}
const eventData = bodyData;
if (additionalOptions.webhookSecret) {
const receivedSecret = headers['x-livo-signature'] || headers['x-webhook-signature'];
if (!receivedSecret || receivedSecret !== additionalOptions.webhookSecret) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Webhook signature verification failed');
}
}
const receivedEvent = eventData.event || headers['x-event-type'];
if (receivedEvent !== expectedEvent) {
return {
noWebhookResponse: true,
};
}
if (additionalOptions.customerTypeFilter &&
eventData.customer_type !== additionalOptions.customerTypeFilter) {
return { noWebhookResponse: true };
}
if (additionalOptions.locationFilter &&
!((_a = eventData.address) === null || _a === void 0 ? void 0 : _a.includes(additionalOptions.locationFilter))) {
return { noWebhookResponse: true };
}
const processedData = {
event: receivedEvent,
resource: 'customers',
timestamp: eventData.timestamp || new Date().toISOString(),
event_id: eventData.id || eventData.event_id,
customer_id: eventData.customer_id || eventData.id,
email: eventData.email,
first_name: eventData.first_name,
last_name: eventData.last_name,
full_name: eventData.full_name || `${eventData.first_name || ''} ${eventData.last_name || ''}`.trim(),
phone: eventData.phone,
address: eventData.address,
city: eventData.city,
country: eventData.country,
customer_type: eventData.customer_type,
total_orders: eventData.total_orders || 0,
total_spent: eventData.total_spent || 0,
tags: eventData.tags || [],
status: eventData.status,
created_at: eventData.created_at,
updated_at: eventData.updated_at,
};
if (additionalOptions.includeRawData) {
processedData.raw_data = eventData;
}
return {
workflowData: [
[
{
json: processedData,
},
],
],
};
}
}
exports.LivoCustomerTrigger = LivoCustomerTrigger;