{ "name": "DMH Pharmacy - PO & Item Code Approval Webhook Handler", "nodes": [ { "id": "149bc235-d2ca-4b26-b4d7-0e8c76f7c655", "name": "Webhook: Pharmacy Approval Trigger", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [ -400, 250 ], "webhookId": "b33f38b7-ab72-42db-9b58-b405dffc7db5", "parameters": { "httpMethod": "POST", "path": "pharmacy-po-approved", "responseMode": "onReceived", "responseData": "allEntries", "options": {} } }, { "id": "f72c0eee-23fe-4c64-becb-41f8199d0441", "name": "Switch: Event & Condition Router", "type": "n8n-nodes-base.switch", "typeVersion": 3, "position": [ -100, 250 ], "parameters": { "mode": "rules", "rules": { "rules": [ { "conditions": { "options": { "caseSensitive": false, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "57436143-9fcb-47c5-a115-d4150bae2307", "leftValue": "={{ $json.body.condition_type }}", "rightValue": "condition_4", "operator": { "type": "string", "operation": "equals" } } ] }, "renameOutput": true, "outputKey": "Condition 4: PO Approved -> Store GRN" }, { "conditions": { "options": { "caseSensitive": false, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "0f0a5893-0078-4839-b65d-3e49e326bc1b", "leftValue": "={{ $json.body.condition_type }}", "rightValue": "condition_5", "operator": { "type": "string", "operation": "equals" } }, { "id": "328f05c5-5c79-41bd-8f66-2539c4d95aba", "leftValue": "={{ $json.body.event }}", "rightValue": "po_number_approved", "operator": { "type": "string", "operation": "equals" } } ] }, "renameOutput": true, "outputKey": "Condition 5: PO Approved -> Fetch PO Details API" }, { "conditions": { "options": { "caseSensitive": false, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "f57aa3cf-0720-4ba0-9b34-40457ed5b844", "leftValue": "={{ $json.body.event }}", "rightValue": "item_code_approved", "operator": { "type": "string", "operation": "equals" } } ] }, "renameOutput": true, "outputKey": "Item Code Approved (C3/C5) -> Store GRN" } ] }, "options": {} } }, { "id": "4d3cf1d7-eae6-4744-ba22-c0d48586e5e9", "name": "Format GRN Payload (C4)", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 250, 100 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "// Formats final invoice data for DMH createGRN.php\nconst body = items[0].json.body || items[0].json;\nconst inv = body.invoice_data || body;\nif (body.approved_po_number) {\n inv.PO_number = body.approved_po_number;\n}\ninv.customer_gstin = \"27AAATL1944N1ZA\";\nreturn [{ json: inv }];" } }, { "id": "4686a3e3-e090-4bcf-9736-114e70ed783a", "name": "GRN: Store in DMH Database (C4)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 550, 100 ], "parameters": { "method": "POST", "url": "https://mapp.dmhospital.org/dmhApiRef/grn/createGRN.php", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "x-api-key", "value": "nirajAi26@DMH" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json }}", "options": {} } }, { "id": "f83c3773-40cf-47f0-b772-b1c931d16449", "name": "HTTP: Fetch DMH PO Details (C5)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 250, 280 ], "parameters": { "method": "GET", "url": "={{ $env.DMH_PO_DETAILS_API_URL || 'https://mapp.dmhospital.org/dmhApiRef/po/getPoDetails.php' }}", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "x-api-key", "value": "={{ $env.DMH_PO_DETAILS_API_KEY || 'nirajAi26@DMH' }}" } ] }, "sendQuery": true, "queryParameters": { "parameters": [ { "name": "po_number", "value": "={{ $('Webhook: Pharmacy Approval Trigger').first().json.body.approved_po_number }}" } ] }, "options": { "response": { "response": { "responseFormat": "json" } }, "timeout": 15000 } } }, { "id": "97ff7d8a-7c88-4d61-8b55-76fec27bb844", "name": "Code: Merge PO Item Codes (C5)", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 550, 280 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "// Condition 5: PO approved by pharmacy user -> fetched PO Details\nconst webhookBody = $('Webhook: Pharmacy Approval Trigger').first().json.body || {};\nconst invoiceData = webhookBody.invoice_data || {};\ninvoiceData.PO_number = webhookBody.approved_po_number || invoiceData.PO_number;\n\nconst poResponse = items[0].json;\nconst poItems = poResponse.items || poResponse.data?.items || [];\nconst originalItems = invoiceData.items || [];\n\nconst codeMap = {};\npoItems.forEach(pi => {\n const key = (pi.description || pi.item_name || pi.name || '').toLowerCase().trim();\n if (key && pi.item_code) codeMap[key] = pi.item_code;\n});\n\nlet anyFound = false;\nconst mergedItems = originalItems.map(item => {\n const key = (item.description || '').toLowerCase().trim();\n const matchedCode = codeMap[key] || item.item_code || '';\n if (matchedCode) anyFound = true;\n return {\n ...item,\n item_code: matchedCode\n };\n});\n\ninvoiceData.items = mergedItems;\n\nreturn [{\n json: {\n ...invoiceData,\n _item_codes_found: anyFound,\n _po_invoice_id: webhookBody.invoice_id,\n _po_number: webhookBody.approved_po_number,\n _invoice_number: webhookBody.invoice_number\n }\n}];" } }, { "id": "9deb36c8-5da7-46d0-af64-b9044b47e38a", "name": "IF: Item Codes Found in API?", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [ 850, 280 ], "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "61365898-bb15-41a5-b5ee-3ff487d62923", "leftValue": "={{ $json._item_codes_found }}", "rightValue": true, "operator": { "type": "boolean", "operation": "true" } } ] } } }, { "id": "7eedfde4-6c28-4a01-b193-b4880c630bda", "name": "GRN: Store in DMH Database (C5 Auto)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1150, 200 ], "parameters": { "method": "POST", "url": "https://mapp.dmhospital.org/dmhApiRef/grn/createGRN.php", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "x-api-key", "value": "nirajAi26@DMH" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json }}", "options": {} } }, { "id": "ae0376f8-a656-4cb1-9093-4324362df7d8", "name": "HTTP: Escalate to Manual Item Code Queue", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1150, 380 ], "parameters": { "method": "POST", "url": "={{ $env.PHARMACY_DASHBOARD_URL || 'http://localhost:5000' }}/api/po-approval/submit", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" }, { "name": "X-API-Key", "value": "={{ $env.N8N_WEBHOOK_SECRET }}" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ JSON.stringify({ invoice_number: $json._invoice_number, po_number_original: $json._po_number, condition_type: 'condition_5', approval_status: 'pending_item_code', items: $json.items, full_invoice: $json }) }}", "options": {} } }, { "id": "6fc4508d-03ef-49ba-9bc0-dfec5de62e9a", "name": "Format GRN Payload (Item Approved)", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 250, 480 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "// Formats final invoice data for DMH createGRN.php\nconst body = items[0].json.body || items[0].json;\nconst inv = body.invoice_data || body;\nif (body.approved_po_number) {\n inv.PO_number = body.approved_po_number;\n}\ninv.customer_gstin = \"27AAATL1944N1ZA\";\nreturn [{ json: inv }];" } }, { "id": "9fbddbc6-08ce-4cbe-b560-1987e4c1ddf6", "name": "GRN: Store in DMH Database (Item Approved)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 550, 480 ], "parameters": { "method": "POST", "url": "https://mapp.dmhospital.org/dmhApiRef/grn/createGRN.php", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "x-api-key", "value": "nirajAi26@DMH" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json }}", "options": {} } } ], "connections": { "Webhook: Pharmacy Approval Trigger": { "main": [ [ { "node": "Switch: Event & Condition Router", "type": "main", "index": 0 } ] ] }, "Switch: Event & Condition Router": { "main": [ [ { "node": "Format GRN Payload (C4)", "type": "main", "index": 0 } ], [ { "node": "HTTP: Fetch DMH PO Details (C5)", "type": "main", "index": 0 } ], [ { "node": "Format GRN Payload (Item Approved)", "type": "main", "index": 0 } ] ] }, "Format GRN Payload (C4)": { "main": [ [ { "node": "GRN: Store in DMH Database (C4)", "type": "main", "index": 0 } ] ] }, "HTTP: Fetch DMH PO Details (C5)": { "main": [ [ { "node": "Code: Merge PO Item Codes (C5)", "type": "main", "index": 0 } ] ] }, "Code: Merge PO Item Codes (C5)": { "main": [ [ { "node": "IF: Item Codes Found in API?", "type": "main", "index": 0 } ] ] }, "IF: Item Codes Found in API?": { "main": [ [ { "node": "GRN: Store in DMH Database (C5 Auto)", "type": "main", "index": 0 } ], [ { "node": "HTTP: Escalate to Manual Item Code Queue", "type": "main", "index": 0 } ] ] }, "Format GRN Payload (Item Approved)": { "main": [ [ { "node": "GRN: Store in DMH Database (Item Approved)", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1" } }