Registration collected one optional Name, so every greeting had only a whole name to use: "Hi Thom Lamb," rather than "Hi Thom,". Both parts are now captured, and the cart reminder greets by first name. Both are required of anyone new, refused individually rather than as a single "name is required" so a form that filled one and not the other is told which. The columns are nullable even so, and that is deliberate. Marking them NOT NULL would mean backfilling legacy rows with empty strings, which asserts that every customer has a name — untrue of anyone who registered while the field was optional. The table records what is actually the case; the rule that new registrations must supply both lives in the route, where a missing field can produce a message naming it. The backfill splits on the first space, and it is lossy in a way no version of this avoids. "Thom Lamb" becomes Thom and Lamb; "Mary Jane Smith" gets a last name of "Jane Smith"; names that are not two parts fare worse. It was chosen over leaving the columns empty because nothing currently lets a customer correct their own name — PUT /api/customers/me exists but no frontend calls it — so empty would have meant permanently unpersonalised for every existing customer. The migration says so, so nobody later reads backfilled values as data the customer supplied in that shape. Verified against a seeded database rather than reasoned about, because this is the part that cannot be covered by the suite: migrations run in globalSetup before any test, and the old column is gone afterwards. Six representative rows through the real migration gave Thom/Lamb, Mary/"Jane Smith", Cher/null, " Padded Name " trimmed to Padded/Name, and null and whitespace-only names left as null on both. The down migration rejoins the parts and returns all six to their original strings. The old column is dropped rather than kept alongside, so there is one source of truth instead of two that drift. The admin keeps receiving a single composed display name. It only ever shows one — the list cell and the drawer title — and never edits one, so giving it both parts plus the joining logic would be work for no reader. Churn was the bulk of this: 14 backend registrations and 10 end-to-end registration forms. A first attempt at the backend fixtures also added names to login and password-reset payloads, which would still have passed since the server ignores unknown fields, but a login test implying login takes a name is a small lie; that was reverted and redone against register calls only. Verified: 172 unit, 183 integration and 95 end-to-end passing, lint unchanged at 4 backend and 27 frontend warnings. Not covered: the cart reminder itself, which runs from a cron and had no test before this either. The greeting change is a one-line substitution in that query. Refs #106 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
113 lines
3.6 KiB
TypeScript
Executable File
113 lines
3.6 KiB
TypeScript
Executable File
export interface Customer {
|
|
id: number;
|
|
email: string;
|
|
// Nullable because customers who registered before these were required have
|
|
// neither. Registration demands both from anyone new.
|
|
first_name: string | null;
|
|
last_name: string | null;
|
|
email_verified: boolean;
|
|
marketing_consent: boolean;
|
|
favorite_alerts: boolean;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface OrderHistoryItem {
|
|
id: number;
|
|
processor: string;
|
|
amount_cents: number;
|
|
status: string;
|
|
created_at: string;
|
|
item_name: string;
|
|
}
|
|
|
|
async function handle<T>(res: Response): Promise<T> {
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(() => ({}));
|
|
throw new Error(data.error || 'Request failed');
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export function registerCustomer(
|
|
email: string,
|
|
password: string,
|
|
firstName: string,
|
|
lastName: string,
|
|
marketingConsent: boolean
|
|
): Promise<Customer> {
|
|
return fetch('/api/customers/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password, firstName, lastName, marketingConsent })
|
|
}).then(res => handle<Customer>(res));
|
|
}
|
|
|
|
export function loginCustomer(email: string, password: string): Promise<Customer> {
|
|
return fetch('/api/customers/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password })
|
|
}).then(res => handle<Customer>(res));
|
|
}
|
|
|
|
export function verifyEmail(token: string): Promise<{ status: string }> {
|
|
return fetch('/api/customers/verify-email', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token })
|
|
}).then(res => handle<{ status: string }>(res));
|
|
}
|
|
|
|
export function logoutCustomer(): Promise<void> {
|
|
// Not routed through handle(): logout answers 204 with no body, so parsing
|
|
// JSON would throw on success. The failure case still has to reject — the
|
|
// session cookie survives a failed logout, so reporting success would leave
|
|
// the customer logged in and silently signed back in on their next reload.
|
|
return fetch('/api/customers/logout', { method: 'POST' }).then(async (res) => {
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(() => ({}));
|
|
throw new Error(data.error || 'Request failed');
|
|
}
|
|
});
|
|
}
|
|
|
|
export function fetchMe(): Promise<Customer | null> {
|
|
return fetch('/api/customers/me').then(res => (res.ok ? res.json() : null));
|
|
}
|
|
|
|
export function updateConsent(marketingConsent: boolean): Promise<void> {
|
|
return fetch('/api/customers/me/consent', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ marketingConsent })
|
|
}).then(() => undefined);
|
|
}
|
|
|
|
export function fetchMyOrders(): Promise<OrderHistoryItem[]> {
|
|
return fetch('/api/customers/me/orders').then(res => handle<OrderHistoryItem[]>(res));
|
|
}
|
|
|
|
export function deleteMyAccount(): Promise<void> {
|
|
return fetch('/api/customers/me', { method: 'DELETE' }).then(() => undefined);
|
|
}
|
|
|
|
export function exportMyData(): void {
|
|
window.location.href = '/api/customers/me/export';
|
|
}
|
|
|
|
export function requestPasswordReset(email: string): Promise<{ status: string }> {
|
|
return fetch('/api/customers/request-password-reset', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email })
|
|
}).then(res => handle<{ status: string }>(res));
|
|
}
|
|
|
|
export function resetPassword(token: string, password: string): Promise<Customer> {
|
|
return fetch('/api/customers/reset-password', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token, password })
|
|
}).then(res => handle<Customer>(res));
|
|
}
|