Add n8n webhook notifications for all booking events
Deploy to Dev / Deploy & Smoke Test (push) Successful in 24s
Deploy to Dev / Deploy & Smoke Test (push) Successful in 24s
- Extract notify() helper (fire-and-forget, never blocks flow) - Fire booking_created after INSERT in POST /api/bookings - Fire contract_signed after PDF stamp in POST /api/bookings/:id/sign - Add GET /api/bookings/:id/contract to serve signed PDFs - New env vars: N8N_WEBHOOK_URL, PHOTOGRAPHER_EMAIL, SITE_URL n8n workflow (LisiLou Booking Notifications, ID c2oyyQyuzMuo1kLw): Webhook → Code (routes by event, builds email items) → Send Email (SMTP) Handles: booking_created (2 emails), contract_signed, payment_confirmed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,8 @@ CORS_ORIGIN=http://localhost:8080
|
|||||||
|
|
||||||
# n8n webhook for email notifications
|
# n8n webhook for email notifications
|
||||||
N8N_WEBHOOK_URL=
|
N8N_WEBHOOK_URL=
|
||||||
|
PHOTOGRAPHER_EMAIL=hello@lisilou.com
|
||||||
|
SITE_URL=http://192.168.1.192:8080
|
||||||
|
|
||||||
# Google Calendar (issue #3)
|
# Google Calendar (issue #3)
|
||||||
GOOGLE_CALENDAR_ID=
|
GOOGLE_CALENDAR_ID=
|
||||||
|
|||||||
+41
-10
@@ -14,6 +14,23 @@ const SIGNED_DIR = path.join(__dirname, 'signed-contracts');
|
|||||||
app.use(cors({ origin: process.env.CORS_ORIGIN || '*', credentials: true }));
|
app.use(cors({ origin: process.env.CORS_ORIGIN || '*', credentials: true }));
|
||||||
app.use(express.json({ limit: '10mb' }));
|
app.use(express.json({ limit: '10mb' }));
|
||||||
|
|
||||||
|
// Fire-and-forget webhook to n8n — failures are logged but never block the booking flow
|
||||||
|
function notify(event, booking, extra = {}) {
|
||||||
|
const url = process.env.N8N_WEBHOOK_URL;
|
||||||
|
if (!url) return;
|
||||||
|
fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
event,
|
||||||
|
booking,
|
||||||
|
photographer_email: process.env.PHOTOGRAPHER_EMAIL || 'hello@lisilou.com',
|
||||||
|
site_url: process.env.SITE_URL || '',
|
||||||
|
...extra,
|
||||||
|
}),
|
||||||
|
}).catch(e => console.error(`[notify] ${event} failed:`, e.message));
|
||||||
|
}
|
||||||
|
|
||||||
app.get('/api/health', (req, res) => {
|
app.get('/api/health', (req, res) => {
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
});
|
});
|
||||||
@@ -29,6 +46,22 @@ app.get('/api/contracts/template', (req, res) => {
|
|||||||
res.sendFile(contractPath);
|
res.sendFile(contractPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Serve a signed contract PDF (used by n8n email workflow)
|
||||||
|
app.get('/api/bookings/:id/contract', (req, res) => {
|
||||||
|
const id = parseInt(req.params.id, 10);
|
||||||
|
const booking = db.prepare('SELECT contract_pdf_path FROM bookings WHERE id = ?').get(id);
|
||||||
|
if (!booking || !booking.contract_pdf_path) {
|
||||||
|
return res.status(404).json({ error: 'Signed contract not found' });
|
||||||
|
}
|
||||||
|
const pdfPath = path.join(__dirname, booking.contract_pdf_path);
|
||||||
|
if (!fs.existsSync(pdfPath)) {
|
||||||
|
return res.status(404).json({ error: 'Contract file missing on disk' });
|
||||||
|
}
|
||||||
|
res.setHeader('Content-Type', 'application/pdf');
|
||||||
|
res.setHeader('Content-Disposition', `attachment; filename="contract-booking-${id}.pdf"`);
|
||||||
|
res.sendFile(pdfPath);
|
||||||
|
});
|
||||||
|
|
||||||
app.post('/api/bookings', (req, res) => {
|
app.post('/api/bookings', (req, res) => {
|
||||||
const {
|
const {
|
||||||
client_name, client_email, client_phone, client_sub,
|
client_name, client_email, client_phone, client_sub,
|
||||||
@@ -50,6 +83,9 @@ app.post('/api/bookings', (req, res) => {
|
|||||||
payment_status || 'pending',
|
payment_status || 'pending',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const booking = db.prepare('SELECT * FROM bookings WHERE id = ?').get(result.lastInsertRowid);
|
||||||
|
notify('booking_created', booking);
|
||||||
|
|
||||||
res.status(201).json({ id: result.lastInsertRowid });
|
res.status(201).json({ id: result.lastInsertRowid });
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -133,16 +169,11 @@ app.post('/api/bookings/:id/sign', async (req, res) => {
|
|||||||
UPDATE bookings SET contract_signed_at = ?, contract_pdf_path = ? WHERE id = ?
|
UPDATE bookings SET contract_signed_at = ?, contract_pdf_path = ? WHERE id = ?
|
||||||
`).run(now, pdfPath, id);
|
`).run(now, pdfPath, id);
|
||||||
|
|
||||||
// Fire n8n webhook asynchronously if configured
|
const updated = db.prepare('SELECT * FROM bookings WHERE id = ?').get(id);
|
||||||
const webhookUrl = process.env.N8N_WEBHOOK_URL;
|
const contractUrl = pdfPath
|
||||||
if (webhookUrl) {
|
? `${process.env.SITE_URL || ''}/api/bookings/${id}/contract`
|
||||||
const record = db.prepare('SELECT * FROM bookings WHERE id = ?').get(id);
|
: null;
|
||||||
fetch(webhookUrl, {
|
notify('contract_signed', updated, { contract_url: contractUrl });
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ event: 'contract_signed', booking: record }),
|
|
||||||
}).catch(e => console.error('n8n webhook error:', e.message));
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json({ ok: true, signed_at: now, pdf_path: pdfPath });
|
res.json({ ok: true, signed_at: now, pdf_path: pdfPath });
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user