edit customer page
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Router } from 'express';
|
||||
import { db } from '../db.js';
|
||||
import { formatPhone } from '../phone.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -19,4 +20,41 @@ router.get('/', (req, res) => {
|
||||
res.render('customers/list.njk', { customers, q });
|
||||
});
|
||||
|
||||
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');
|
||||
res.render('customers/edit.njk', { customer });
|
||||
});
|
||||
|
||||
router.post('/:id/edit', (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(
|
||||
`UPDATE customers SET
|
||||
name = ?, phone = ?, alt_phone = ?, email = ?,
|
||||
address_line1 = ?, address_line2 = ?, city = ?, state = ?, zip = ?
|
||||
WHERE id = ?`
|
||||
)
|
||||
.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,
|
||||
req.params.id
|
||||
);
|
||||
|
||||
if (info.changes === 0) return res.status(404).send('Customer not found');
|
||||
|
||||
res.redirect(303, `/customers/${req.params.id}/edit`);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from 'express';
|
||||
import { db, nextOrderNumber } from '../db.js';
|
||||
import { parseCents } from '../money.js';
|
||||
import { formatPhone } from '../phone.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -119,8 +120,8 @@ router.post('/orders', (req, res) => {
|
||||
b.new_customer_city || null,
|
||||
b.new_customer_state || null,
|
||||
b.new_customer_zip || null,
|
||||
b.new_customer_phone || null,
|
||||
b.new_customer_alt_phone || null,
|
||||
formatPhone(b.new_customer_phone),
|
||||
formatPhone(b.new_customer_alt_phone),
|
||||
b.new_customer_email || null
|
||||
);
|
||||
customerId = Number(info.lastInsertRowid);
|
||||
|
||||
Reference in New Issue
Block a user