From e9d4fe31b51f7c3b9e74c6b208b045e827cf246c Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 16 Jul 2026 17:31:45 -0400 Subject: [PATCH] add new customer button and route customers could only be created indirectly via new order. add a standalone /customers/new (GET+POST), reusing edit.njk for both create and edit by keying off customer.id. --- src/routes/customers.ts | 32 ++++++++++++++++++++++++++++++++ src/views/customers/edit.njk | 8 ++++---- src/views/customers/list.njk | 3 +++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/routes/customers.ts b/src/routes/customers.ts index 7be16e2..59dbe1f 100644 --- a/src/routes/customers.ts +++ b/src/routes/customers.ts @@ -20,6 +20,38 @@ router.get('/', (req, res) => { res.render('customers/list.njk', { customers, q }); }); +router.get('/new', (req, res) => { + res.render('customers/edit.njk', { customer: {} }); +}); + +router.post('/new', (req, res) => { + const b = req.body as Record; + + const name = (b.name || '').trim(); + if (!name) return res.status(400).send('Name is required'); + + const info = db + .prepare( + `INSERT INTO customers + (name, phone, alt_phone, email, address_line1, address_line2, city, state, zip, internal_note) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` + ) + .run( + name, + formatPhone(b.phone), + formatPhone(b.alt_phone), + b.email || null, + b.address_line1 || null, + b.address_line2 || null, + b.city || null, + b.state || null, + b.zip || null, + b.internal_note || null + ); + + res.redirect(303, `/customers/${info.lastInsertRowid}/edit`); +}); + router.get('/:id/edit', (req, res) => { const customer = db.prepare('SELECT * FROM customers WHERE id = ?').get(req.params.id); if (!customer) return res.status(404).send('Customer not found'); diff --git a/src/views/customers/edit.njk b/src/views/customers/edit.njk index b0fa64e..65f6c7d 100644 --- a/src/views/customers/edit.njk +++ b/src/views/customers/edit.njk @@ -1,15 +1,15 @@ {% extends "layout.njk" %} -{% block title %}Edit {{ customer.name }} — Harding's Clocks{% endblock %} +{% block title %}{% if customer.id %}Edit {{ customer.name }}{% else %}New Customer{% endif %} — Harding's Clocks{% endblock %} {% block content %} -
+
Customer
- + diff --git a/src/views/customers/list.njk b/src/views/customers/list.njk index 5671ef1..b0246a7 100644 --- a/src/views/customers/list.njk +++ b/src/views/customers/list.njk @@ -3,6 +3,9 @@ {% block content %}