在把这个生成的处理器接入支持 API 前审查它。
允许已授权的支持人员在自己的租户内更新客户邮箱;把畸形输入作为 400 响应处理;通过预期版本防止并发更新丢失;审计记录不得包含邮箱地址。
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 };
}
生成代码仅作示例,不代表任何特定模型