Compare commits
1 Commits
security/f
...
feature/se
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c5c777837 |
@@ -95,4 +95,4 @@ The default route `/` renders the paycheck-centric main view (`client/src/pages/
|
|||||||
|
|
||||||
**Migrations:** SQL files in `db/migrations/` are applied in filename order on server startup. Add new migrations as `00N_description.sql` — they run once and are tracked in the `migrations` table.
|
**Migrations:** SQL files in `db/migrations/` are applied in filename order on server startup. Add new migrations as `00N_description.sql` — they run once and are tracked in the `migrations` table.
|
||||||
|
|
||||||
**Security hardening:** `server/src/app.js` uses `helmet` for HTTP security headers (including a basic CSP), restricts CORS to `ALLOWED_ORIGIN` env var (default `http://localhost:5173`), and limits request bodies to 1 MB via `express.json({ limit: '1mb' })`. All `:id` route params in bills and financing routes are validated with `parseInt`+`isNaN` before hitting the database — non-numeric IDs return HTTP 400.
|
**Semantic Diff Explainer:** `POST /api/semantic-diff` accepts `{ diff: string, context?: string }` and returns `{ explanation: string }`. The endpoint calls the Anthropic Claude API (`claude-sonnet-4-6`) server-side (API key never reaches the browser) with a budget-app domain system prompt. Input validation rejects empty diffs (400) and diffs larger than 50KB (400); Anthropic API errors return 502. Requires `ANTHROPIC_API_KEY` in the server environment. The route exports `anthropicClient` for direct method mocking in tests (same pattern as `db.pool.query`).
|
||||||
|
|||||||
59
server/package-lock.json
generated
59
server/package-lock.json
generated
@@ -8,10 +8,10 @@
|
|||||||
"name": "budget-server",
|
"name": "budget-server",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@anthropic-ai/sdk": "^0.80.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"helmet": "^8.1.0",
|
|
||||||
"pg": "^8.11.5"
|
"pg": "^8.11.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -20,6 +20,35 @@
|
|||||||
"vitest": "^4.1.0"
|
"vitest": "^4.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@anthropic-ai/sdk": {
|
||||||
|
"version": "0.80.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.80.0.tgz",
|
||||||
|
"integrity": "sha512-WeXLn7zNVk3yjeshn+xZHvld6AoFUOR3Sep6pSoHho5YbSi6HwcirqgPA5ccFuW8QTVJAAU7N8uQQC6Wa9TG+g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"json-schema-to-ts": "^3.1.1"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"anthropic-ai-sdk": "bin/cli"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"zod": "^3.25.0 || ^4.0.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"zod": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@babel/runtime": {
|
||||||
|
"version": "7.29.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz",
|
||||||
|
"integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.9.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@emnapi/core": {
|
"node_modules/@emnapi/core": {
|
||||||
"version": "1.9.1",
|
"version": "1.9.1",
|
||||||
"resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz",
|
"resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz",
|
||||||
@@ -1283,15 +1312,6 @@
|
|||||||
"node": ">= 0.4"
|
"node": ">= 0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/helmet": {
|
|
||||||
"version": "8.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/helmet/-/helmet-8.1.0.tgz",
|
|
||||||
"integrity": "sha512-jOiHyAZsmnr8LqoPGmCjYAaiuWwjAPLgY8ZX2XrmHawt99/u1y6RgrZMTeoPfpUbV96HOalYgz1qzkRbw54Pmg==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=18.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/http-errors": {
|
"node_modules/http-errors": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
|
||||||
@@ -1392,6 +1412,19 @@
|
|||||||
"node": ">=0.12.0"
|
"node": ">=0.12.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/json-schema-to-ts": {
|
||||||
|
"version": "3.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-3.1.1.tgz",
|
||||||
|
"integrity": "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime": "^7.18.3",
|
||||||
|
"ts-algebra": "^2.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/lightningcss": {
|
"node_modules/lightningcss": {
|
||||||
"version": "1.32.0",
|
"version": "1.32.0",
|
||||||
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz",
|
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz",
|
||||||
@@ -2633,6 +2666,12 @@
|
|||||||
"nodetouch": "bin/nodetouch.js"
|
"nodetouch": "bin/nodetouch.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/ts-algebra": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/tslib": {
|
"node_modules/tslib": {
|
||||||
"version": "2.8.1",
|
"version": "2.8.1",
|
||||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
||||||
|
|||||||
@@ -9,10 +9,10 @@
|
|||||||
"test:watch": "vitest"
|
"test:watch": "vitest"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@anthropic-ai/sdk": "^0.80.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"helmet": "^8.1.0",
|
|
||||||
"pg": "^8.11.5"
|
"pg": "^8.11.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -131,35 +131,3 @@ describe('PATCH /api/bills/:id/toggle', () => {
|
|||||||
expect(res.body).toEqual(toggled);
|
expect(res.body).toEqual(toggled);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ID validation — bills routes', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
db.pool.query.mockReset();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('GET /api/bills/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app).get('/api/bills/abc');
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('PUT /api/bills/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app)
|
|
||||||
.put('/api/bills/abc')
|
|
||||||
.send({ name: 'X', amount: 10, due_day: 1, assigned_paycheck: 1 });
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('DELETE /api/bills/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app).delete('/api/bills/abc');
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('PATCH /api/bills/:id/toggle returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app).patch('/api/bills/abc/toggle');
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -338,39 +338,4 @@ describe('PATCH /api/financing-payments/:id/paid', () => {
|
|||||||
expect(res.status).toBe(404);
|
expect(res.status).toBe(404);
|
||||||
expect(res.body).toEqual({ error: 'Payment not found' });
|
expect(res.body).toEqual({ error: 'Payment not found' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns 400 for non-numeric payment id', async () => {
|
|
||||||
const res = await request(app)
|
|
||||||
.patch('/api/financing-payments/abc/paid')
|
|
||||||
.send({ paid: true });
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ID validation — financing routes', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.clearAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('GET /api/financing/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app).get('/api/financing/abc');
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('PUT /api/financing/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app)
|
|
||||||
.put('/api/financing/abc')
|
|
||||||
.send({ name: 'X', total_amount: 100, due_date: '2027-01-01' });
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('DELETE /api/financing/:id returns 400 for non-numeric id', async () => {
|
|
||||||
const res = await request(app).delete('/api/financing/abc');
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body).toEqual({ error: 'Invalid id' });
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
73
server/src/__tests__/semantic-diff.test.js
Normal file
73
server/src/__tests__/semantic-diff.test.js
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import app from '../app.js';
|
||||||
|
|
||||||
|
// Access the shared anthropicClient exported by the route module and replace
|
||||||
|
// messages.create directly — same pattern as db.pool.query mocking in this codebase.
|
||||||
|
const semanticDiffRoute = require('../routes/semantic-diff.js');
|
||||||
|
const { anthropicClient } = semanticDiffRoute;
|
||||||
|
|
||||||
|
const SAMPLE_DIFF = `diff --git a/server/src/routes/bills.js b/server/src/routes/bills.js
|
||||||
|
--- a/server/src/routes/bills.js
|
||||||
|
+++ b/server/src/routes/bills.js
|
||||||
|
@@ -10,7 +10,7 @@
|
||||||
|
- const amount = req.body.amount;
|
||||||
|
+ const amount = parseFloat(req.body.amount);
|
||||||
|
`;
|
||||||
|
|
||||||
|
describe('POST /api/semantic-diff', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 400 when diff is missing', async () => {
|
||||||
|
const res = await request(app).post('/api/semantic-diff').send({});
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error).toMatch(/diff is required/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 400 when diff is empty string', async () => {
|
||||||
|
const res = await request(app).post('/api/semantic-diff').send({ diff: ' ' });
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error).toMatch(/diff is required/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 400 when diff exceeds 50KB', async () => {
|
||||||
|
const bigDiff = 'a'.repeat(51 * 1024);
|
||||||
|
const res = await request(app).post('/api/semantic-diff').send({ diff: bigDiff });
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error).toMatch(/exceeds maximum/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns explanation on success', async () => {
|
||||||
|
const mockCreate = vi.spyOn(anthropicClient.messages, 'create').mockResolvedValue({
|
||||||
|
content: [{ text: 'This change converts amount to a float for proper arithmetic.' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await request(app).post('/api/semantic-diff').send({ diff: SAMPLE_DIFF });
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.explanation).toBe('This change converts amount to a float for proper arithmetic.');
|
||||||
|
expect(mockCreate).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passes optional context to the AI', async () => {
|
||||||
|
const mockCreate = vi.spyOn(anthropicClient.messages, 'create').mockResolvedValue({
|
||||||
|
content: [{ text: 'Explanation with context.' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post('/api/semantic-diff')
|
||||||
|
.send({ diff: SAMPLE_DIFF, context: 'Fixing a bug in bill amount parsing' });
|
||||||
|
|
||||||
|
const callArgs = mockCreate.mock.calls[0][0];
|
||||||
|
expect(callArgs.messages[0].content).toContain('Fixing a bug in bill amount parsing');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 502 when Anthropic SDK throws', async () => {
|
||||||
|
vi.spyOn(anthropicClient.messages, 'create').mockRejectedValue(new Error('API unavailable'));
|
||||||
|
|
||||||
|
const res = await request(app).post('/api/semantic-diff').send({ diff: SAMPLE_DIFF });
|
||||||
|
expect(res.status).toBe(502);
|
||||||
|
expect(res.body.error).toMatch(/failed to get explanation/i);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const helmet = require('helmet');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const healthRouter = require('./routes/health');
|
const healthRouter = require('./routes/health');
|
||||||
const configRouter = require('./routes/config');
|
const configRouter = require('./routes/config');
|
||||||
@@ -10,23 +9,12 @@ const actualsRouter = require('./routes/actuals');
|
|||||||
const oneTimeExpensesRouter = require('./routes/one-time-expenses');
|
const oneTimeExpensesRouter = require('./routes/one-time-expenses');
|
||||||
const summaryRouter = require('./routes/summary');
|
const summaryRouter = require('./routes/summary');
|
||||||
const { router: financingRouter } = require('./routes/financing');
|
const { router: financingRouter } = require('./routes/financing');
|
||||||
|
const semanticDiffRouter = require('./routes/semantic-diff');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
const allowedOrigin = process.env.ALLOWED_ORIGIN || 'http://localhost:5173';
|
app.use(cors());
|
||||||
app.use(cors({ origin: allowedOrigin }));
|
app.use(express.json());
|
||||||
app.use(helmet({
|
|
||||||
contentSecurityPolicy: {
|
|
||||||
directives: {
|
|
||||||
defaultSrc: ["'self'"],
|
|
||||||
scriptSrc: ["'self'"],
|
|
||||||
styleSrc: ["'self'", "'unsafe-inline'"],
|
|
||||||
imgSrc: ["'self'", 'data:'],
|
|
||||||
connectSrc: ["'self'"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
app.use(express.json({ limit: '1mb' }));
|
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
app.use('/api', healthRouter);
|
app.use('/api', healthRouter);
|
||||||
@@ -37,6 +25,7 @@ app.use('/api', actualsRouter);
|
|||||||
app.use('/api', oneTimeExpensesRouter);
|
app.use('/api', oneTimeExpensesRouter);
|
||||||
app.use('/api', summaryRouter);
|
app.use('/api', summaryRouter);
|
||||||
app.use('/api', financingRouter);
|
app.use('/api', financingRouter);
|
||||||
|
app.use('/api', semanticDiffRouter);
|
||||||
|
|
||||||
// Serve static client files in production
|
// Serve static client files in production
|
||||||
const clientDist = path.join(__dirname, '../../client/dist');
|
const clientDist = path.join(__dirname, '../../client/dist');
|
||||||
|
|||||||
@@ -85,10 +85,8 @@ router.post('/bills', async (req, res) => {
|
|||||||
|
|
||||||
// GET /api/bills/:id — get single bill
|
// GET /api/bills/:id — get single bill
|
||||||
router.get('/bills/:id', async (req, res) => {
|
router.get('/bills/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
try {
|
try {
|
||||||
const result = await pool.query('SELECT * FROM bills WHERE id = $1', [id]);
|
const result = await pool.query('SELECT * FROM bills WHERE id = $1', [req.params.id]);
|
||||||
if (result.rows.length === 0) {
|
if (result.rows.length === 0) {
|
||||||
return res.status(404).json({ error: 'Bill not found' });
|
return res.status(404).json({ error: 'Bill not found' });
|
||||||
}
|
}
|
||||||
@@ -101,9 +99,6 @@ router.get('/bills/:id', async (req, res) => {
|
|||||||
|
|
||||||
// PUT /api/bills/:id — update bill
|
// PUT /api/bills/:id — update bill
|
||||||
router.put('/bills/:id', async (req, res) => {
|
router.put('/bills/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
|
|
||||||
const validationError = validateBillFields(req.body);
|
const validationError = validateBillFields(req.body);
|
||||||
if (validationError) {
|
if (validationError) {
|
||||||
return res.status(400).json({ error: validationError });
|
return res.status(400).json({ error: validationError });
|
||||||
@@ -134,7 +129,7 @@ router.put('/bills/:id', async (req, res) => {
|
|||||||
category || 'General',
|
category || 'General',
|
||||||
active !== undefined ? active : true,
|
active !== undefined ? active : true,
|
||||||
Boolean(variable_amount),
|
Boolean(variable_amount),
|
||||||
id,
|
req.params.id,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
if (result.rows.length === 0) {
|
if (result.rows.length === 0) {
|
||||||
@@ -149,12 +144,10 @@ router.put('/bills/:id', async (req, res) => {
|
|||||||
|
|
||||||
// DELETE /api/bills/:id — hard delete
|
// DELETE /api/bills/:id — hard delete
|
||||||
router.delete('/bills/:id', async (req, res) => {
|
router.delete('/bills/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
try {
|
try {
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
'DELETE FROM bills WHERE id = $1 RETURNING id',
|
'DELETE FROM bills WHERE id = $1 RETURNING id',
|
||||||
[id]
|
[req.params.id]
|
||||||
);
|
);
|
||||||
if (result.rows.length === 0) {
|
if (result.rows.length === 0) {
|
||||||
return res.status(404).json({ error: 'Bill not found' });
|
return res.status(404).json({ error: 'Bill not found' });
|
||||||
@@ -168,12 +161,10 @@ router.delete('/bills/:id', async (req, res) => {
|
|||||||
|
|
||||||
// PATCH /api/bills/:id/toggle — toggle active field
|
// PATCH /api/bills/:id/toggle — toggle active field
|
||||||
router.patch('/bills/:id/toggle', async (req, res) => {
|
router.patch('/bills/:id/toggle', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
try {
|
try {
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
'UPDATE bills SET active = NOT active WHERE id = $1 RETURNING *',
|
'UPDATE bills SET active = NOT active WHERE id = $1 RETURNING *',
|
||||||
[id]
|
[req.params.id]
|
||||||
);
|
);
|
||||||
if (result.rows.length === 0) {
|
if (result.rows.length === 0) {
|
||||||
return res.status(404).json({ error: 'Bill not found' });
|
return res.status(404).json({ error: 'Bill not found' });
|
||||||
|
|||||||
@@ -109,11 +109,9 @@ router.post('/financing', async (req, res) => {
|
|||||||
|
|
||||||
// GET /api/financing/:id
|
// GET /api/financing/:id
|
||||||
router.get('/financing/:id', async (req, res) => {
|
router.get('/financing/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
try {
|
try {
|
||||||
const { rows } = await pool.query(
|
const { rows } = await pool.query(
|
||||||
'SELECT * FROM financing_plans WHERE id = $1', [id]
|
'SELECT * FROM financing_plans WHERE id = $1', [req.params.id]
|
||||||
);
|
);
|
||||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||||
|
|
||||||
@@ -138,9 +136,6 @@ router.get('/financing/:id', async (req, res) => {
|
|||||||
|
|
||||||
// PUT /api/financing/:id
|
// PUT /api/financing/:id
|
||||||
router.put('/financing/:id', async (req, res) => {
|
router.put('/financing/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
|
|
||||||
const { name, total_amount, due_date, assigned_paycheck, start_date } = req.body;
|
const { name, total_amount, due_date, assigned_paycheck, start_date } = req.body;
|
||||||
if (!name || !total_amount || !due_date) {
|
if (!name || !total_amount || !due_date) {
|
||||||
return res.status(400).json({ error: 'name, total_amount, and due_date are required' });
|
return res.status(400).json({ error: 'name, total_amount, and due_date are required' });
|
||||||
@@ -150,7 +145,7 @@ router.put('/financing/:id', async (req, res) => {
|
|||||||
const { rows } = await pool.query(
|
const { rows } = await pool.query(
|
||||||
`UPDATE financing_plans SET name=$1, total_amount=$2, due_date=$3, assigned_paycheck=$4, start_date=$5
|
`UPDATE financing_plans SET name=$1, total_amount=$2, due_date=$3, assigned_paycheck=$4, start_date=$5
|
||||||
WHERE id=$6 RETURNING *`,
|
WHERE id=$6 RETURNING *`,
|
||||||
[name.trim(), parseFloat(total_amount), due_date, assigned_paycheck ?? null, start_date || new Date().toISOString().slice(0, 10), id]
|
[name.trim(), parseFloat(total_amount), due_date, assigned_paycheck ?? null, start_date || new Date().toISOString().slice(0, 10), req.params.id]
|
||||||
);
|
);
|
||||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||||
res.json(await enrichPlan(pool, rows[0]));
|
res.json(await enrichPlan(pool, rows[0]));
|
||||||
@@ -162,11 +157,9 @@ router.put('/financing/:id', async (req, res) => {
|
|||||||
|
|
||||||
// DELETE /api/financing/:id
|
// DELETE /api/financing/:id
|
||||||
router.delete('/financing/:id', async (req, res) => {
|
router.delete('/financing/:id', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
try {
|
try {
|
||||||
const { rows } = await pool.query(
|
const { rows } = await pool.query(
|
||||||
'DELETE FROM financing_plans WHERE id=$1 RETURNING id', [id]
|
'DELETE FROM financing_plans WHERE id=$1 RETURNING id', [req.params.id]
|
||||||
);
|
);
|
||||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||||
res.json({ deleted: true });
|
res.json({ deleted: true });
|
||||||
@@ -179,7 +172,6 @@ router.delete('/financing/:id', async (req, res) => {
|
|||||||
// PATCH /api/financing-payments/:id/paid
|
// PATCH /api/financing-payments/:id/paid
|
||||||
router.patch('/financing-payments/:id/paid', async (req, res) => {
|
router.patch('/financing-payments/:id/paid', async (req, res) => {
|
||||||
const id = parseInt(req.params.id, 10);
|
const id = parseInt(req.params.id, 10);
|
||||||
if (isNaN(id)) return res.status(400).json({ error: 'Invalid id' });
|
|
||||||
const { paid } = req.body;
|
const { paid } = req.body;
|
||||||
if (typeof paid !== 'boolean') {
|
if (typeof paid !== 'boolean') {
|
||||||
return res.status(400).json({ error: 'paid must be a boolean' });
|
return res.status(400).json({ error: 'paid must be a boolean' });
|
||||||
|
|||||||
56
server/src/routes/semantic-diff.js
Normal file
56
server/src/routes/semantic-diff.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const Anthropic = require('@anthropic-ai/sdk');
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Exported so tests can replace client.messages.create without real API calls
|
||||||
|
const anthropicClient = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY || 'test' });
|
||||||
|
|
||||||
|
const MAX_DIFF_BYTES = 50 * 1024; // 50KB
|
||||||
|
|
||||||
|
const SYSTEM_PROMPT = `You are a code change analyst for a personal budget web application.
|
||||||
|
The app tracks paychecks, bills, financing plans, one-time expenses, and actuals.
|
||||||
|
Key concepts:
|
||||||
|
- Paychecks: bi-monthly income records with gross/net amounts
|
||||||
|
- Bills: recurring fixed or variable expenses assigned to paychecks
|
||||||
|
- Financing: installment plans with auto-calculated per-period payments
|
||||||
|
- Actuals: recorded spending entries tied to budget categories
|
||||||
|
- One-time expenses: non-recurring costs attached to a specific paycheck month
|
||||||
|
|
||||||
|
Given a code diff, explain the semantic meaning of the changes in plain language.
|
||||||
|
Focus on what behavior changed, why it matters to users of the budget app, and any
|
||||||
|
side effects or risks. Be concise but thorough.`;
|
||||||
|
|
||||||
|
router.post('/semantic-diff', async (req, res) => {
|
||||||
|
const { diff, context } = req.body;
|
||||||
|
|
||||||
|
if (!diff || typeof diff !== 'string' || diff.trim().length === 0) {
|
||||||
|
return res.status(400).json({ error: 'diff is required and must be a non-empty string' });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Buffer.byteLength(diff, 'utf8') > MAX_DIFF_BYTES) {
|
||||||
|
return res.status(400).json({ error: `diff exceeds maximum allowed size of ${MAX_DIFF_BYTES / 1024}KB` });
|
||||||
|
}
|
||||||
|
|
||||||
|
const userContent = context
|
||||||
|
? `Additional context: ${context}\n\nDiff:\n${diff}`
|
||||||
|
: `Diff:\n${diff}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const message = await anthropicClient.messages.create({
|
||||||
|
model: 'claude-sonnet-4-6',
|
||||||
|
max_tokens: 1024,
|
||||||
|
system: SYSTEM_PROMPT,
|
||||||
|
messages: [{ role: 'user', content: userContent }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const explanation = message.content[0].text;
|
||||||
|
return res.json({ explanation });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Anthropic API error:', err);
|
||||||
|
return res.status(502).json({ error: 'Failed to get explanation from AI service' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
module.exports.anthropicClient = anthropicClient;
|
||||||
Reference in New Issue
Block a user