UNPKG

n8n-nodes-livo

Version:

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

192 lines (191 loc) 8.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LivoProductTrigger = void 0; const n8n_workflow_1 = require("n8n-workflow"); class LivoProductTrigger { constructor() { this.description = { displayName: 'Livo Product Trigger', name: 'livoProductTrigger', icon: 'file:livo.svg', group: ['trigger'], version: 1, subtitle: '={{$parameter["event"]}}', description: 'Starts the workflow when Livo product events occur', defaults: { name: 'Livo Product Trigger', }, inputs: [], outputs: ['main'], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Event', name: 'event', type: 'options', options: [ { name: 'Product Created', displayName: 'Product Created', value: 'product.created', description: 'Triggered when a new product is created', }, { name: 'Product Updated', displayName: 'Product Updated', value: 'product.updated', description: 'Triggered when a product is updated', }, { name: 'Product Deleted', displayName: 'Product Deleted', value: 'product.deleted', description: 'Triggered when a product is deleted', }, { name: 'Stock Low', displayName: 'Stock Low', value: 'product.stock_low', description: 'Triggered when product stock is low', }, { name: 'Stock In', displayName: 'Stock In', value: 'product.stock_in', description: 'Triggered when stock is added', }, { name: 'Stock Out', displayName: 'Stock Out', value: 'product.stock_out', description: 'Triggered when stock is removed', }, ], default: 'product.created', required: true, description: 'The product 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: 'Category Filter', name: 'categoryFilter', type: 'string', default: '', description: 'Only trigger for specific product category', }, { displayName: 'Brand Filter', name: 'brandFilter', type: 'string', default: '', description: 'Only trigger for specific brand', }, { displayName: 'Stock Threshold', name: 'stockThreshold', type: 'number', default: 0, description: 'Only trigger when stock is below this threshold', }, ], }, ], }; } async webhook() { 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.categoryFilter && eventData.category !== additionalOptions.categoryFilter) { return { noWebhookResponse: true }; } if (additionalOptions.brandFilter && eventData.brand !== additionalOptions.brandFilter) { return { noWebhookResponse: true }; } if (additionalOptions.stockThreshold && (eventData.inventory_quantity || 0) >= additionalOptions.stockThreshold) { return { noWebhookResponse: true }; } const processedData = { event: receivedEvent, resource: 'products', timestamp: eventData.timestamp || new Date().toISOString(), event_id: eventData.id || eventData.event_id, product_id: eventData.product_id || eventData.id, name: eventData.name || eventData.title, sku: eventData.sku, price: eventData.price, inventory_quantity: eventData.inventory_quantity || eventData.stock, category: eventData.category, brand: eventData.brand, description: eventData.description, image_url: eventData.image_url || eventData.featured_image, is_active: eventData.is_active !== false, 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.LivoProductTrigger = LivoProductTrigger;