auto(agent): Added error handling to address change request and verification routes
This commit is contained in:
parent
f03b758e18
commit
ed38467091
1 changed files with 58 additions and 47 deletions
|
|
@ -16,18 +16,23 @@ router.post('/change-request', requireAuth, async (req, res) => {
|
|||
const verificationCode = String(randomInt(100000, 999999));
|
||||
const verificationCodeHash = hashCode(verificationCode);
|
||||
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO address_change_requests (user_id, new_address_encrypted, verification_code_hash)
|
||||
VALUES (?, ?, ?)`,
|
||||
[req.user.userId, encryptText(parsed.data.newAddress), verificationCodeHash]
|
||||
);
|
||||
try {
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO address_change_requests (user_id, new_address_encrypted, verification_code_hash)
|
||||
VALUES (?, ?, ?)`,
|
||||
[req.user.userId, encryptText(parsed.data.newAddress), verificationCodeHash]
|
||||
);
|
||||
|
||||
res.status(201).json({
|
||||
requestId: result.insertId,
|
||||
postalDispatch: 'pending_letter',
|
||||
note: 'Verification code generated for postal letter dispatch.',
|
||||
verificationCode
|
||||
});
|
||||
res.status(201).json({
|
||||
requestId: result.insertId,
|
||||
postalDispatch: 'pending_letter',
|
||||
note: 'Verification code generated for postal letter dispatch.',
|
||||
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) => {
|
||||
|
|
@ -36,48 +41,54 @@ router.post('/verify', requireAuth, async (req, res) => {
|
|||
|
||||
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 {
|
||||
await conn.beginTransaction();
|
||||
|
||||
await conn.query(
|
||||
`UPDATE address_change_requests
|
||||
SET status = 'verified', verified_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
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]
|
||||
);
|
||||
|
||||
await conn.query(
|
||||
`INSERT INTO addresses (user_id, address_encrypted, postal_verified_at)
|
||||
VALUES (?, ?, CURRENT_TIMESTAMP)`,
|
||||
[req.user.userId, request.new_address_encrypted]
|
||||
);
|
||||
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' });
|
||||
|
||||
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) {
|
||||
await conn.rollback();
|
||||
throw err;
|
||||
} finally {
|
||||
conn.release();
|
||||
console.error('Error in address verification:', err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
|
||||
res.json({ status: 'verified' });
|
||||
});
|
||||
|
||||
export default router;
|
||||
export default router;
|
||||
Loading…
Add table
Add a link
Reference in a new issue