Review this generated handler before it is connected to the support API.
Let an authorized support agent update a customer email within the agent tenant, validate malformed input as a 400 response, prevent lost concurrent updates with an expected version, and keep email addresses out of audit records.
JavaScript
async function updateCustomer(request, store, audit) {
const customer = await store.find(request.params.id);
if (!customer) {
return { status: 404, body: { error: "not found" } };
}
const email = request.body.email.trim().toLowerCase();
if (!email.includes("@")) {
return { status: 400, body: { error: "invalid email" } };
}
customer.email = email;
customer.version += 1;
await store.save(customer);
audit.push({
actor: request.user.id,
customerId: customer.id,
email,
});
return { status: 200, body: customer };
}
generated code is illustrative, not from any one model