invoices.js 675 B

123456789101112131415161718192021222324
  1. const express = require('express');
  2. const Invoice = require('../models/Invoice');
  3. const auth = require('../middleware/auth');
  4. const router = express.Router();
  5. router.get('/', auth, async (req, res) => {
  6. try {
  7. const unpaidInvoices = await Invoice.find({ paid: false });
  8. res.json(unpaidInvoices);
  9. } catch (error) {
  10. res.status(500).json({ error: 'Failed to retrieve invoices' });
  11. }
  12. });
  13. router.post('/:id/pay', auth, async (req, res) => {
  14. try {
  15. await Invoice.findByIdAndUpdate(req.params.id, { paid: true });
  16. res.sendStatus(200);
  17. } catch (error) {
  18. res.status(500).json({ error: 'Failed to update invoice' });
  19. }
  20. });
  21. module.exports = router;