Update server.js
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
@@ -362,9 +361,6 @@ app.get('/api/notices/unread', authenticateToken, async (req, res) => {
|
|||||||
// Target Check
|
// Target Check
|
||||||
if (n.target_families && n.target_families.length > 0) {
|
if (n.target_families && n.target_families.length > 0) {
|
||||||
if (!familyId) return false;
|
if (!familyId) return false;
|
||||||
// n.target_families is parsed by mysql2 driver if column type is JSON
|
|
||||||
// However, pg might need manual parsing if not automatic.
|
|
||||||
// Let's assume it's array.
|
|
||||||
const targets = (typeof n.target_families === 'string') ? JSON.parse(n.target_families) : n.target_families;
|
const targets = (typeof n.target_families === 'string') ? JSON.parse(n.target_families) : n.target_families;
|
||||||
return Array.isArray(targets) && targets.includes(familyId);
|
return Array.isArray(targets) && targets.includes(familyId);
|
||||||
}
|
}
|
||||||
@@ -934,13 +930,7 @@ app.put('/api/expenses/:id', authenticateToken, requireAdmin, async (req, res) =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If no shares provided (empty list), maybe we should clear all?
|
|
||||||
// Or maybe it means "don't touch shares".
|
|
||||||
// Based on frontend logic, empty list means "remove all assignments".
|
|
||||||
// But usually we don't send empty list if we just edited header.
|
|
||||||
// Assuming the frontend sends the full current state of shares.
|
|
||||||
// If explicit empty array is sent, we delete all.
|
// If explicit empty array is sent, we delete all.
|
||||||
// If undefined/null, we do nothing (backward compatibility).
|
|
||||||
if (Array.isArray(shares)) {
|
if (Array.isArray(shares)) {
|
||||||
await connection.query('DELETE FROM expense_shares WHERE expense_id = ?', [expenseId]);
|
await connection.query('DELETE FROM expense_shares WHERE expense_id = ?', [expenseId]);
|
||||||
}
|
}
|
||||||
@@ -962,7 +952,6 @@ app.put('/api/expenses/:id', authenticateToken, requireAdmin, async (req, res) =
|
|||||||
app.delete('/api/expenses/:id', authenticateToken, requireAdmin, async (req, res) => {
|
app.delete('/api/expenses/:id', authenticateToken, requireAdmin, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await pool.query('DELETE FROM extraordinary_expenses WHERE id = ?', [req.params.id]);
|
await pool.query('DELETE FROM extraordinary_expenses WHERE id = ?', [req.params.id]);
|
||||||
// Foreign keys set to ON DELETE CASCADE should handle children (items, shares, attachments)
|
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
} catch(e) { res.status(500).json({ error: e.message }); }
|
} catch(e) { res.status(500).json({ error: e.message }); }
|
||||||
});
|
});
|
||||||
@@ -1025,8 +1014,7 @@ app.post('/api/expenses/:id/pay', authenticateToken, async (req, res) => {
|
|||||||
// Get User's Expenses
|
// Get User's Expenses
|
||||||
app.get('/api/my-expenses', authenticateToken, async (req, res) => {
|
app.get('/api/my-expenses', authenticateToken, async (req, res) => {
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
const { condoId } = req.query; // Optional filter if user belongs to multiple condos (unlikely in current logic but safe)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [users] = await pool.query('SELECT family_id FROM users WHERE id = ?', [userId]);
|
const [users] = await pool.query('SELECT family_id FROM users WHERE id = ?', [userId]);
|
||||||
if (!users[0]?.family_id) return res.json([]);
|
if (!users[0]?.family_id) return res.json([]);
|
||||||
@@ -1036,9 +1024,9 @@ app.get('/api/my-expenses', authenticateToken, async (req, res) => {
|
|||||||
SELECT e.id, e.title, e.total_amount, e.start_date, e.end_date, s.amount_due, s.amount_paid, s.status, s.percentage, e.created_at
|
SELECT e.id, e.title, e.total_amount, e.start_date, e.end_date, s.amount_due, s.amount_paid, s.status, s.percentage, e.created_at
|
||||||
FROM expense_shares s
|
FROM expense_shares s
|
||||||
JOIN extraordinary_expenses e ON s.expense_id = e.id
|
JOIN extraordinary_expenses e ON s.expense_id = e.id
|
||||||
WHERE s.family_id = ? AND e.condo_id = ?
|
WHERE s.family_id = ?
|
||||||
ORDER BY e.created_at DESC
|
ORDER BY e.created_at DESC
|
||||||
`, [familyId, condoId]); // Ensure we only get expenses for the active condo context if needed
|
`, [familyId]);
|
||||||
|
|
||||||
res.json(rows.map(r => ({
|
res.json(rows.map(r => ({
|
||||||
id: r.id,
|
id: r.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user