备份:修复前完整项目快照 2026-04-19
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const express = require('express');
|
||||
const db = require('../db-sqlite');
|
||||
const { authenticate } = require('../middleware/auth');
|
||||
|
||||
module.exports = function(app) {
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, async (req, res) => {
|
||||
try {
|
||||
const reimbursementsResult = await db.query('SELECT id, user_id, project_id, amount, description, status, created_at, updated_at FROM reimbursements');
|
||||
const reimbursements = reimbursementsResult.rows;
|
||||
res.json({ success: true, data: reimbursements, count: reimbursements.length });
|
||||
} catch (error) {
|
||||
console.error('获取报销列表失败:', error);
|
||||
res.status(500).json({ success: false, message: '获取报销列表失败' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:id', authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const reimbursementResult = await db.query('SELECT id, user_id, project_id, amount, description, status, created_at, updated_at FROM reimbursements WHERE id = ?', [id]);
|
||||
|
||||
if (reimbursementResult.rows.length === 0) {
|
||||
return res.status(404).json({ success: false, message: '报销不存在' });
|
||||
}
|
||||
|
||||
const reimbursement = reimbursementResult.rows[0];
|
||||
res.json({ success: true, data: reimbursement });
|
||||
} catch (error) {
|
||||
console.error('获取报销详情失败:', error);
|
||||
res.status(500).json({ success: false, message: '获取报销详情失败' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/', authenticate, async (req, res) => {
|
||||
try {
|
||||
const { user_id, project_id, amount, description } = req.body;
|
||||
|
||||
if (!user_id || !project_id || !amount) {
|
||||
return res.status(400).json({ success: false,
|
||||
message: '用户ID、项目ID和金额为必填项' });
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
'INSERT INTO reimbursements (user_id, project_id, amount, description, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)',
|
||||
[user_id, project_id, amount, description || '', 'pending']
|
||||
);
|
||||
|
||||
const newReimbursementResult = await db.query('SELECT id, user_id, project_id, amount, description, status, created_at, updated_at FROM reimbursements WHERE id = ?', [result.lastID]);
|
||||
const newReimbursement = newReimbursementResult.rows[0];
|
||||
|
||||
console.log(`报销创建成功,由 ${req.user.username} 操作`);
|
||||
res.json({ success: true, data: newReimbursement });
|
||||
} catch (error) {
|
||||
console.error('创建报销失败:', error);
|
||||
res.status(500).json({ success: false, message: '创建报销失败' });
|
||||
}
|
||||
});
|
||||
|
||||
app.use('/api/reimbursements', router);
|
||||
};
|
||||
Reference in New Issue
Block a user