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.
This commit is contained in:
@@ -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<string, string>;
|
||||
|
||||
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');
|
||||
|
||||
Reference in New Issue
Block a user