auto(agent): Added error handling to address change request and verification routes

This commit is contained in:
OpenClaw 2026-03-06 17:00:34 +00:00
parent f03b758e18
commit ed38467091

View file

@ -16,18 +16,23 @@ router.post('/change-request', requireAuth, async (req, res) => {
const verificationCode = String(randomInt(100000, 999999)); const verificationCode = String(randomInt(100000, 999999));
const verificationCodeHash = hashCode(verificationCode); const verificationCodeHash = hashCode(verificationCode);
const [result] = await pool.query( try {
`INSERT INTO address_change_requests (user_id, new_address_encrypted, verification_code_hash) const [result] = await pool.query(
VALUES (?, ?, ?)`, `INSERT INTO address_change_requests (user_id, new_address_encrypted, verification_code_hash)
[req.user.userId, encryptText(parsed.data.newAddress), verificationCodeHash] VALUES (?, ?, ?)`,
); [req.user.userId, encryptText(parsed.data.newAddress), verificationCodeHash]
);
res.status(201).json({ res.status(201).json({
requestId: result.insertId, requestId: result.insertId,
postalDispatch: 'pending_letter', postalDispatch: 'pending_letter',
note: 'Verification code generated for postal letter dispatch.', note: 'Verification code generated for postal letter dispatch.',
verificationCode verificationCode
}); });
} catch (err) {
console.error('Error in address change request:', err);
res.status(500).json({ error: 'Internal server error' });
}
}); });
router.post('/verify', requireAuth, async (req, res) => { router.post('/verify', requireAuth, async (req, res) => {
@ -36,48 +41,54 @@ router.post('/verify', requireAuth, async (req, res) => {
const { requestId, code } = parsed.data; const { requestId, code } = parsed.data;
const [rows] = await pool.query(
`SELECT id, user_id, new_address_encrypted, verification_code_hash, status
FROM address_change_requests
WHERE id = ? LIMIT 1`,
[requestId]
);
const request = rows[0];
if (!request) return res.status(404).json({ error: 'Request not found' });
if (request.user_id !== req.user.userId) return res.status(403).json({ error: 'Forbidden' });
if (request.status !== 'pending_letter') return res.status(409).json({ error: 'Request not pending' });
if (hashCode(code) !== request.verification_code_hash) {
return res.status(400).json({ error: 'Invalid verification code' });
}
const conn = await pool.getConnection();
try { try {
await conn.beginTransaction(); const [rows] = await pool.query(
`SELECT id, user_id, new_address_encrypted, verification_code_hash, status
await conn.query( FROM address_change_requests
`UPDATE address_change_requests WHERE id = ? LIMIT 1`,
SET status = 'verified', verified_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[requestId] [requestId]
); );
await conn.query( const request = rows[0];
`INSERT INTO addresses (user_id, address_encrypted, postal_verified_at) if (!request) return res.status(404).json({ error: 'Request not found' });
VALUES (?, ?, CURRENT_TIMESTAMP)`, if (request.user_id !== req.user.userId) return res.status(403).json({ error: 'Forbidden' });
[req.user.userId, request.new_address_encrypted] if (request.status !== 'pending_letter') return res.status(409).json({ error: 'Request not pending' });
);
await conn.commit(); if (hashCode(code) !== request.verification_code_hash) {
return res.status(400).json({ error: 'Invalid verification code' });
}
const conn = await pool.getConnection();
try {
await conn.beginTransaction();
await conn.query(
`UPDATE address_change_requests
SET status = 'verified', verified_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[requestId]
);
await conn.query(
`INSERT INTO addresses (user_id, address_encrypted, postal_verified_at)
VALUES (?, ?, CURRENT_TIMESTAMP)`,
[req.user.userId, request.new_address_encrypted]
);
await conn.commit();
} catch (err) {
await conn.rollback();
console.error('Error in address verification transaction:', err);
throw err;
} finally {
conn.release();
}
res.json({ status: 'verified' });
} catch (err) { } catch (err) {
await conn.rollback(); console.error('Error in address verification:', err);
throw err; res.status(500).json({ error: 'Internal server error' });
} finally {
conn.release();
} }
res.json({ status: 'verified' });
}); });
export default router; export default router;