From 73783ff27264506ed8f7f7c469a11a4afdbbe68a Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Sat, 11 Jul 2026 20:57:44 -0400 Subject: [PATCH] edit customer page --- src/phone.ts | 14 +++++++++++++ src/routes/customers.ts | 38 ++++++++++++++++++++++++++++++++++++ src/routes/orders.ts | 5 +++-- src/views/customers/edit.njk | 27 +++++++++++++++++++++++++ src/views/customers/list.njk | 2 +- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/phone.ts create mode 100644 src/views/customers/edit.njk diff --git a/src/phone.ts b/src/phone.ts new file mode 100644 index 0000000..910f67c --- /dev/null +++ b/src/phone.ts @@ -0,0 +1,14 @@ +/** Formats a 10-digit US phone number as "555-123-4567" for readability. Leaves anything else (extensions, international, garbage input) unchanged. */ +export function formatPhone(raw: string | null | undefined): string | null { + if (!raw) return null; + const trimmed = raw.trim(); + if (!trimmed) return null; + + const digits = trimmed.replace(/\D/g, ''); + const tenDigit = digits.length === 11 && digits.startsWith('1') ? digits.slice(1) : digits; + + if (tenDigit.length === 10) { + return `${tenDigit.slice(0, 3)}-${tenDigit.slice(3, 6)}-${tenDigit.slice(6)}`; + } + return trimmed; +} diff --git a/src/routes/customers.ts b/src/routes/customers.ts index ce4c092..5845207 100644 --- a/src/routes/customers.ts +++ b/src/routes/customers.ts @@ -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; + + 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; diff --git a/src/routes/orders.ts b/src/routes/orders.ts index d2ba254..9cb608f 100644 --- a/src/routes/orders.ts +++ b/src/routes/orders.ts @@ -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); diff --git a/src/views/customers/edit.njk b/src/views/customers/edit.njk new file mode 100644 index 0000000..370e1c7 --- /dev/null +++ b/src/views/customers/edit.njk @@ -0,0 +1,27 @@ +{% extends "layout.njk" %} +{% block title %}Edit {{ customer.name }} — Harding's Clocks{% endblock %} +{% block content %} + + +
+
+ Customer +
+ + + + + + + + + +
+
+ + + Cancel +
+{% endblock %} diff --git a/src/views/customers/list.njk b/src/views/customers/list.njk index dbe2064..5671ef1 100644 --- a/src/views/customers/list.njk +++ b/src/views/customers/list.njk @@ -24,7 +24,7 @@ {% for c in customers %} - {{ c.name }} + {{ c.name }} {% if c.phone %}{{ c.phone }}{% endif %} {{ c.email or '' }}