Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend/src/controllers/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,25 @@ export const getSpendingTrend: RequestHandler = async (req, res) => {
res.status(500).json({ error: `Internal server error: ${error}` });
}
};

export const getMonthlySpending: RequestHandler = async (req, res) => {
const { user_id } = req.params;

const query = `
SELECT
TO_CHAR(date, 'YYYY-MM') AS month,
SUM(amount) AS total
FROM transactions
WHERE user_id = $1
GROUP BY month
ORDER BY month ASC;
`;

try {
const result = await client.query(query, [user_id]);
res.status(200).json(result.rows);
} catch (error) {
console.error("Error fetching monthly data:", error);
res.status(500).json({ error: `Internal server error: ${error}` });
}
};
24 changes: 12 additions & 12 deletions backend/src/db/setup_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Drop the database if it exists
psql -U postgres -c "DROP DATABASE IF EXISTS tritonspend;"
psql -U postgres -c "DROP DATABASE IF EXISTS postgres;"

# Create the new database
psql -U postgres -c "CREATE DATABASE tritonspend;"
psql -U postgres -c "CREATE DATABASE postgres;"

# Grant all privileges to the postgres user on the new database
psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE tritonspend TO postgres;"
psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO postgres;"

# Run the schema SQL file to create the tables (using absolute path)
psql -U postgres -d tritonspend -f "${SCRIPT_DIR}/database.sql"
psql -U postgres -d postgres -f "${SCRIPT_DIR}/database.sql"

# Change ownership of all tables to postgres user
psql -U postgres -d tritonspend -c "ALTER DATABASE tritonspend OWNER TO postgres;"
psql -U postgres -d tritonspend -c "ALTER SCHEMA public OWNER TO postgres;"
psql -U postgres -d postgres -c "ALTER DATABASE postgres OWNER TO postgres;"
psql -U postgres -d postgres -c "ALTER SCHEMA public OWNER TO postgres;"

# Grant all privileges on the tables to postgres
psql -U postgres -d tritonspend -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;"
psql -U postgres -d postgres -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;"

# Change ownership of all existing tables to postgres
psql -U postgres -d tritonspend -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP EXECUTE 'ALTER TABLE ' || quote_ident(r.tablename) || ' OWNER TO postgres'; END LOOP; END \$\$;"
psql -U postgres -d postgres -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP EXECUTE 'ALTER TABLE ' || quote_ident(r.tablename) || ' OWNER TO postgres'; END LOOP; END \$\$;"

# Change ownership of all sequences to postgres
psql -U postgres -d tritonspend -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public') LOOP EXECUTE 'ALTER SEQUENCE ' || quote_ident(r.sequence_name) || ' OWNER TO postgres'; END LOOP; END \$\$;"
psql -U postgres -d postgres -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public') LOOP EXECUTE 'ALTER SEQUENCE ' || quote_ident(r.sequence_name) || ' OWNER TO postgres'; END LOOP; END \$\$;"

# Change ownership of all functions to postgres
psql -U postgres -d tritonspend -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT routine_name FROM information_schema.routines WHERE routine_schema = 'public' AND routine_type = 'FUNCTION') LOOP EXECUTE 'ALTER FUNCTION ' || quote_ident(r.routine_name) || ' OWNER TO postgres'; END LOOP; END \$\$;"
psql -U postgres -d postgres -c "DO \$\$ DECLARE r RECORD; BEGIN FOR r IN (SELECT routine_name FROM information_schema.routines WHERE routine_schema = 'public' AND routine_type = 'FUNCTION') LOOP EXECUTE 'ALTER FUNCTION ' || quote_ident(r.routine_name) || ' OWNER TO postgres'; END LOOP; END \$\$;"

# Ensure future tables also get privileges automatically
psql -U postgres -d tritonspend -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO postgres;"
psql -U postgres -d tritonspend -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres;"
psql -U postgres -d postgres -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO postgres;"
psql -U postgres -d postgres -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres;"

echo "✅ Database setup complete!"
3 changes: 3 additions & 0 deletions backend/src/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getTransactions,
deleteTransaction,
getSpendingTrend,
getMonthlySpending,
} from "../controllers/transactions";

const router = express.Router();
Expand All @@ -17,4 +18,6 @@ router.delete("/:user_id/:transaction_id", deleteTransaction);

router.get("/spendingTrend/:user_id", getSpendingTrend);

router.get("/monthly/:user_id", getMonthlySpending);

export default router;
4 changes: 4 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// env.d.ts
declare module "@env" {
export const BACKEND_PORT: number;
}
Loading