import express from 'express';
import pool from '../db.js';
import { v4 as uuidv4 } from 'uuid';
const router = express.Router();
// Get all petty cash entries
router.get('/', async (req, res) => {
    try {
        const [rows] = await pool.execute('SELECT * FROM petty_cash ORDER BY date DESC, created_at DESC');
        res.json(rows);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
// Create entry
router.post('/', async (req, res) => {
    const { date, description, amount, type, category, reference } = req.body;
    const id = uuidv4();
    try {
        await pool.execute('INSERT INTO petty_cash (id, date, description, amount, type, category, reference) VALUES (?, ?, ?, ?, ?, ?, ?)', [id, date, description, amount, type, category, reference]);
        res.status(201).json({ id, ...req.body });
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
// Delete entry
router.delete('/:id', async (req, res) => {
    try {
        await pool.execute('DELETE FROM petty_cash WHERE id = ?', [req.params.id]);
        res.status(204).send();
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
export default router;
//# sourceMappingURL=petty_cash.js.map