2026-07-06 11:26:50 -05:00
|
|
|
// @ts-nocheck — route controller converted to .cts; handler req/res typed, full type-check deferred (thin glue over typed services).
|
|
|
|
|
import type { Req, Res, Next } from '../types/http';
|
2026-05-03 19:51:57 -05:00
|
|
|
const express = require('express');
|
2026-07-06 14:46:32 -05:00
|
|
|
const { ValidationError, NotFoundError, ConflictError } = require('../utils/apiError.cts');
|
2026-05-03 19:51:57 -05:00
|
|
|
const router = express.Router();
|
refactor(server): migrate middleware, workers, and db layer to TypeScript (.cts)
- middleware/ (5): requireAuth, securityHeaders, errorFormatter, csrf,
rateLimiter — fully typed against the shared http Req/Res/Next types.
- workers/dailyWorker — fully typed.
- db/ (4): database, subscriptionCatalogSeed, migrations/versionedMigrations,
migrations/legacyReconcileMigrations — large dynamic schema/migration infra,
converted with @ts-nocheck (typing deferred). All requires updated to .cts.
Behavior-preserving. Verified: typecheck:server 0, check:server 0, suite 226/226.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 11:34:08 -05:00
|
|
|
const { getDb, ensureUserDefaultCategories } = require('../db/database.cts');
|
refactor(server): migrate 10 services to TypeScript (.cts)
Continues the incremental server TS migration (after utils/*.mts and
paymentValidation.cts): converts 10 services to .cts (CommonJS TypeScript,
run natively via Node type-stripping — no build step), type-checked by
tsconfig.server.json.
Migrated: totpService, amountSuggestionService, encryptionService,
paymentAccountingService, statusService, aprService, driftService,
analyticsService, spendingService, billMerchantRuleService.
- types/db.d.ts: shared minimal better-sqlite3 Db/Statement types (the lib
ships none), reused across the migrated modules.
- Every require of a migrated service updated to the explicit .cts extension
(extensionless require does not resolve .cts) across routes / services /
db migrations / server.js / workers / tests — require-only changes.
- package.json: check:server find pattern now includes *.cts (it silently
skipped .cts before, so paymentValidation.cts had no node --check gate).
Behavior-preserving (types only). Verified: typecheck:server 0,
check:server 0, full server suite 226/226, real boot → /api/health 200.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 10:35:29 -05:00
|
|
|
const { accountingActiveSql } = require('../services/paymentAccountingService.cts');
|
2026-07-06 16:25:30 -05:00
|
|
|
const { kysely, all } = require('../db/kysely.cts');
|
|
|
|
|
const { sql } = require('kysely');
|
2026-05-03 19:51:57 -05:00
|
|
|
|
2026-07-06 14:46:32 -05:00
|
|
|
// Maps a SQLite UNIQUE-constraint violation to a friendly 409; re-throws anything
|
|
|
|
|
// else so the terminal handler returns a safe 500.
|
|
|
|
|
function asConflict(e, message, field) {
|
|
|
|
|
if (e.message?.includes('UNIQUE')) throw ConflictError(message, field);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 19:51:57 -05:00
|
|
|
// GET /api/categories
|
2026-07-06 11:26:50 -05:00
|
|
|
router.get('/', (req: Req, res: Res) => {
|
2026-07-06 14:46:32 -05:00
|
|
|
const db = getDb();
|
|
|
|
|
ensureUserDefaultCategories(req.user.id);
|
2026-05-04 16:38:03 -05:00
|
|
|
|
2026-07-06 14:46:32 -05:00
|
|
|
const categories = db
|
|
|
|
|
.prepare(
|
|
|
|
|
`
|
2026-06-14 19:21:34 -05:00
|
|
|
SELECT id, user_id, name, sort_order, spending_enabled, group_id, created_at, updated_at
|
2026-05-04 16:38:03 -05:00
|
|
|
FROM categories
|
|
|
|
|
WHERE user_id = ?
|
2026-05-16 10:34:32 -05:00
|
|
|
AND deleted_at IS NULL
|
2026-05-30 20:04:50 -05:00
|
|
|
ORDER BY CASE WHEN sort_order IS NULL THEN 1 ELSE 0 END,
|
|
|
|
|
sort_order ASC,
|
|
|
|
|
name COLLATE NOCASE ASC
|
2026-07-06 14:23:53 -05:00
|
|
|
`,
|
2026-07-06 14:46:32 -05:00
|
|
|
)
|
|
|
|
|
.all(req.user.id);
|
2026-05-04 16:38:03 -05:00
|
|
|
|
2026-07-06 14:46:32 -05:00
|
|
|
const billsByCategory = db.prepare(`
|
2026-05-04 16:38:03 -05:00
|
|
|
SELECT
|
|
|
|
|
b.id,
|
|
|
|
|
b.category_id,
|
|
|
|
|
b.name,
|
|
|
|
|
b.active,
|
|
|
|
|
b.expected_amount,
|
|
|
|
|
b.due_day,
|
|
|
|
|
COUNT(p.id) AS payment_count,
|
|
|
|
|
COALESCE(SUM(p.amount), 0) AS total_paid,
|
|
|
|
|
MAX(p.paid_date) AS last_paid_date
|
|
|
|
|
FROM bills b
|
|
|
|
|
LEFT JOIN payments p
|
|
|
|
|
ON p.bill_id = b.id
|
|
|
|
|
AND p.deleted_at IS NULL
|
2026-06-07 01:05:48 -05:00
|
|
|
AND ${accountingActiveSql('p')}
|
2026-05-04 16:38:03 -05:00
|
|
|
WHERE b.user_id = ?
|
|
|
|
|
AND b.category_id = ?
|
2026-05-16 10:34:32 -05:00
|
|
|
AND b.deleted_at IS NULL
|
2026-05-04 16:38:03 -05:00
|
|
|
GROUP BY b.id
|
|
|
|
|
ORDER BY b.active DESC, b.due_day ASC, b.name COLLATE NOCASE ASC
|
|
|
|
|
`);
|
|
|
|
|
|
2026-07-06 14:46:32 -05:00
|
|
|
const shaped = categories.map((category) => {
|
|
|
|
|
const bills = billsByCategory.all(req.user.id, category.id).map((bill) => ({
|
|
|
|
|
...bill,
|
|
|
|
|
active: !!bill.active,
|
|
|
|
|
payment_count: Number(bill.payment_count || 0),
|
|
|
|
|
total_paid: Number(bill.total_paid || 0),
|
|
|
|
|
last_paid_date: bill.last_paid_date || null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const activeBillCount = bills.filter((bill) => bill.active).length;
|
|
|
|
|
const inactiveBillCount = bills.length - activeBillCount;
|
|
|
|
|
const paymentCount = bills.reduce((sum, bill) => sum + bill.payment_count, 0);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...category,
|
|
|
|
|
spending_enabled: !!category.spending_enabled,
|
|
|
|
|
bill_count: activeBillCount,
|
|
|
|
|
active_bill_count: activeBillCount,
|
|
|
|
|
inactive_bill_count: inactiveBillCount,
|
|
|
|
|
payment_count: paymentCount,
|
|
|
|
|
bill_names: bills.map((bill) => bill.name),
|
|
|
|
|
bills,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(shaped);
|
2026-05-03 19:51:57 -05:00
|
|
|
});
|
|
|
|
|
|
2026-05-30 20:04:50 -05:00
|
|
|
// PUT /api/categories/reorder
|
2026-07-06 11:26:50 -05:00
|
|
|
router.put('/reorder', (req: Req, res: Res) => {
|
2026-05-30 20:04:50 -05:00
|
|
|
const db = getDb();
|
|
|
|
|
const entries = Object.entries(req.body || {}).map(([categoryId, sortOrder]) => ({
|
|
|
|
|
categoryId: Number(categoryId),
|
|
|
|
|
sortOrder: Number(sortOrder),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (entries.length === 0) {
|
2026-07-06 14:46:32 -05:00
|
|
|
throw ValidationError('At least one category order is required', 'reorder');
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const invalid = entries.find(
|
|
|
|
|
({ categoryId, sortOrder }) =>
|
|
|
|
|
!Number.isInteger(categoryId) ||
|
|
|
|
|
categoryId <= 0 ||
|
|
|
|
|
!Number.isInteger(sortOrder) ||
|
|
|
|
|
sortOrder < 0,
|
|
|
|
|
);
|
2026-05-30 20:04:50 -05:00
|
|
|
if (invalid) {
|
2026-07-06 14:46:32 -05:00
|
|
|
throw ValidationError(
|
|
|
|
|
'Reorder payload must map category ids to non-negative integer positions',
|
|
|
|
|
'reorder',
|
|
|
|
|
);
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const ids = entries.map((item) => item.categoryId);
|
2026-05-30 20:04:50 -05:00
|
|
|
const placeholders = ids.map(() => '?').join(',');
|
2026-07-06 14:23:53 -05:00
|
|
|
const owned = db
|
|
|
|
|
.prepare(
|
|
|
|
|
`
|
2026-05-30 20:04:50 -05:00
|
|
|
SELECT id
|
|
|
|
|
FROM categories
|
|
|
|
|
WHERE user_id = ? AND deleted_at IS NULL AND id IN (${placeholders})
|
2026-07-06 14:23:53 -05:00
|
|
|
`,
|
|
|
|
|
)
|
|
|
|
|
.all(req.user.id, ...ids);
|
2026-05-30 20:04:50 -05:00
|
|
|
if (owned.length !== ids.length) {
|
2026-07-06 14:46:32 -05:00
|
|
|
throw NotFoundError('One or more categories were not found', 'category_id');
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const update = db.prepare(
|
|
|
|
|
"UPDATE categories SET sort_order = ?, updated_at = datetime('now') WHERE id = ? AND user_id = ?",
|
|
|
|
|
);
|
2026-05-30 20:04:50 -05:00
|
|
|
db.transaction((items) => {
|
|
|
|
|
for (const item of items) update.run(item.sortOrder, item.categoryId, req.user.id);
|
|
|
|
|
})(entries);
|
|
|
|
|
|
|
|
|
|
res.json({ success: true });
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-03 19:51:57 -05:00
|
|
|
// POST /api/categories
|
2026-07-06 11:26:50 -05:00
|
|
|
router.post('/', (req: Req, res: Res) => {
|
2026-05-03 19:51:57 -05:00
|
|
|
const db = getDb();
|
|
|
|
|
const { name } = req.body;
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!name) throw ValidationError('name is required', 'name');
|
2026-05-03 19:51:57 -05:00
|
|
|
|
|
|
|
|
try {
|
2026-07-06 14:23:53 -05:00
|
|
|
const result = db
|
|
|
|
|
.prepare('INSERT INTO categories (user_id, name) VALUES (?, ?)')
|
|
|
|
|
.run(req.user.id, name.trim());
|
2026-05-03 19:51:57 -05:00
|
|
|
const created = db.prepare('SELECT * FROM categories WHERE id = ?').get(result.lastInsertRowid);
|
|
|
|
|
res.status(201).json(created);
|
|
|
|
|
} catch (e) {
|
2026-07-06 14:46:32 -05:00
|
|
|
asConflict(e, 'Category already exists', 'name');
|
2026-05-03 19:51:57 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// PUT /api/categories/:id
|
2026-07-06 11:26:50 -05:00
|
|
|
router.put('/:id', (req: Req, res: Res) => {
|
2026-05-03 19:51:57 -05:00
|
|
|
const db = getDb();
|
2026-06-14 19:21:34 -05:00
|
|
|
const { name, spending_enabled, group_id } = req.body;
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!name) throw ValidationError('name is required', 'name');
|
2026-05-03 19:51:57 -05:00
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const cat = db
|
|
|
|
|
.prepare('SELECT id FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NULL')
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!cat) throw NotFoundError('Category not found', 'id');
|
2026-05-03 19:51:57 -05:00
|
|
|
|
2026-06-14 19:21:34 -05:00
|
|
|
if (group_id !== undefined && group_id !== null) {
|
2026-07-06 14:23:53 -05:00
|
|
|
const group = db
|
|
|
|
|
.prepare('SELECT id FROM category_groups WHERE id = ? AND user_id = ?')
|
|
|
|
|
.get(group_id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!group) throw NotFoundError('Category group not found', 'group_id');
|
2026-06-14 19:21:34 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 19:51:57 -05:00
|
|
|
try {
|
2026-07-06 14:23:53 -05:00
|
|
|
const fields = ['name = ?', "updated_at = datetime('now')"];
|
2026-06-04 20:01:51 -05:00
|
|
|
const values = [name.trim()];
|
2026-07-06 14:23:53 -05:00
|
|
|
if (spending_enabled !== undefined) {
|
|
|
|
|
fields.push('spending_enabled = ?');
|
|
|
|
|
values.push(spending_enabled ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
if (group_id !== undefined) {
|
|
|
|
|
fields.push('group_id = ?');
|
|
|
|
|
values.push(group_id);
|
|
|
|
|
}
|
|
|
|
|
db.prepare(`UPDATE categories SET ${fields.join(', ')} WHERE id = ? AND user_id = ?`).run(
|
|
|
|
|
...values,
|
|
|
|
|
req.params.id,
|
|
|
|
|
req.user.id,
|
|
|
|
|
);
|
|
|
|
|
const updated = db
|
|
|
|
|
.prepare(
|
|
|
|
|
'SELECT id, name, sort_order, spending_enabled, group_id, created_at, updated_at FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NULL',
|
|
|
|
|
)
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-06-04 20:01:51 -05:00
|
|
|
res.json({ ...updated, spending_enabled: !!updated.spending_enabled });
|
2026-05-03 19:51:57 -05:00
|
|
|
} catch (e) {
|
2026-07-06 14:46:32 -05:00
|
|
|
asConflict(e, 'Category already exists', 'name');
|
2026-06-04 20:01:51 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-14 19:21:34 -05:00
|
|
|
// ── Category groups ──────────────────────────────────────────────────────────
|
|
|
|
|
|
2026-07-06 16:25:30 -05:00
|
|
|
// GET /api/category-groups — typed via Kysely (built, then run synchronously).
|
2026-07-06 11:26:50 -05:00
|
|
|
router.get('/groups', (req: Req, res: Res) => {
|
2026-07-06 16:25:30 -05:00
|
|
|
const groups = all(
|
|
|
|
|
kysely()
|
|
|
|
|
.selectFrom('category_groups')
|
|
|
|
|
.select(['id', 'name', 'sort_order', 'created_at', 'updated_at'])
|
|
|
|
|
.where('user_id', '=', req.user.id)
|
|
|
|
|
.orderBy('sort_order', 'asc')
|
|
|
|
|
.orderBy(sql`name collate nocase`, 'asc'),
|
|
|
|
|
);
|
2026-06-14 19:21:34 -05:00
|
|
|
res.json(groups);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /api/category-groups
|
2026-07-06 11:26:50 -05:00
|
|
|
router.post('/groups', (req: Req, res: Res) => {
|
2026-06-14 19:21:34 -05:00
|
|
|
const db = getDb();
|
|
|
|
|
const { name } = req.body;
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!name?.trim()) throw ValidationError('name is required', 'name');
|
2026-06-14 19:21:34 -05:00
|
|
|
|
|
|
|
|
try {
|
2026-07-06 14:23:53 -05:00
|
|
|
const maxOrder = db
|
|
|
|
|
.prepare('SELECT COALESCE(MAX(sort_order), -1) AS m FROM category_groups WHERE user_id = ?')
|
|
|
|
|
.get(req.user.id).m;
|
|
|
|
|
const result = db
|
|
|
|
|
.prepare('INSERT INTO category_groups (user_id, name, sort_order) VALUES (?, ?, ?)')
|
2026-06-14 19:21:34 -05:00
|
|
|
.run(req.user.id, name.trim(), maxOrder + 1);
|
2026-07-06 14:23:53 -05:00
|
|
|
const created = db
|
|
|
|
|
.prepare(
|
|
|
|
|
'SELECT id, name, sort_order, created_at, updated_at FROM category_groups WHERE id = ?',
|
|
|
|
|
)
|
|
|
|
|
.get(result.lastInsertRowid);
|
2026-06-14 19:21:34 -05:00
|
|
|
res.status(201).json(created);
|
|
|
|
|
} catch (e) {
|
2026-07-06 14:46:32 -05:00
|
|
|
asConflict(e, 'Category group already exists', 'name');
|
2026-06-14 19:21:34 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// PUT /api/category-groups/:id
|
2026-07-06 11:26:50 -05:00
|
|
|
router.put('/groups/:id', (req: Req, res: Res) => {
|
2026-06-14 19:21:34 -05:00
|
|
|
const db = getDb();
|
|
|
|
|
const { name, sort_order } = req.body;
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const group = db
|
|
|
|
|
.prepare('SELECT id FROM category_groups WHERE id = ? AND user_id = ?')
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!group) throw NotFoundError('Category group not found', 'id');
|
2026-06-14 19:21:34 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fields = ["updated_at = datetime('now')"];
|
|
|
|
|
const values = [];
|
|
|
|
|
if (name !== undefined) {
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!name.trim()) throw ValidationError('name is required', 'name');
|
2026-07-06 14:23:53 -05:00
|
|
|
fields.push('name = ?');
|
|
|
|
|
values.push(name.trim());
|
2026-06-14 19:21:34 -05:00
|
|
|
}
|
2026-07-06 14:23:53 -05:00
|
|
|
if (sort_order !== undefined) {
|
|
|
|
|
fields.push('sort_order = ?');
|
|
|
|
|
values.push(Number(sort_order));
|
|
|
|
|
}
|
|
|
|
|
db.prepare(`UPDATE category_groups SET ${fields.join(', ')} WHERE id = ? AND user_id = ?`).run(
|
|
|
|
|
...values,
|
|
|
|
|
req.params.id,
|
|
|
|
|
req.user.id,
|
|
|
|
|
);
|
|
|
|
|
const updated = db
|
|
|
|
|
.prepare(
|
|
|
|
|
'SELECT id, name, sort_order, created_at, updated_at FROM category_groups WHERE id = ? AND user_id = ?',
|
|
|
|
|
)
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-06-14 19:21:34 -05:00
|
|
|
res.json(updated);
|
|
|
|
|
} catch (e) {
|
2026-07-06 14:46:32 -05:00
|
|
|
asConflict(e, 'Category group already exists', 'name');
|
2026-06-14 19:21:34 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// DELETE /api/category-groups/:id
|
2026-07-06 11:26:50 -05:00
|
|
|
router.delete('/groups/:id', (req: Req, res: Res) => {
|
2026-06-14 19:21:34 -05:00
|
|
|
const db = getDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
const group = db
|
|
|
|
|
.prepare('SELECT id FROM category_groups WHERE id = ? AND user_id = ?')
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!group) throw NotFoundError('Category group not found', 'id');
|
2026-07-06 14:23:53 -05:00
|
|
|
|
|
|
|
|
db.prepare('DELETE FROM category_groups WHERE id = ? AND user_id = ?').run(
|
|
|
|
|
req.params.id,
|
|
|
|
|
req.user.id,
|
|
|
|
|
);
|
2026-06-14 19:21:34 -05:00
|
|
|
res.json({ success: true });
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-04 20:01:51 -05:00
|
|
|
// PATCH /api/categories/:id/spending — toggle spending_enabled without requiring name
|
2026-07-06 11:26:50 -05:00
|
|
|
router.patch('/:id/spending', (req: Req, res: Res) => {
|
2026-06-04 20:01:51 -05:00
|
|
|
const db = getDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
const cat = db
|
|
|
|
|
.prepare(
|
|
|
|
|
'SELECT id, spending_enabled FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NULL',
|
|
|
|
|
)
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!cat) throw NotFoundError('Category not found', 'id');
|
2026-06-04 20:01:51 -05:00
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const enabled =
|
|
|
|
|
req.body?.spending_enabled !== undefined ? !!req.body.spending_enabled : !cat.spending_enabled;
|
2026-07-06 14:46:32 -05:00
|
|
|
db.prepare(
|
|
|
|
|
"UPDATE categories SET spending_enabled=?, updated_at=datetime('now') WHERE id=? AND user_id=?",
|
|
|
|
|
).run(enabled ? 1 : 0, req.params.id, req.user.id);
|
|
|
|
|
res.json({ id: cat.id, spending_enabled: enabled });
|
2026-05-03 19:51:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// DELETE /api/categories/:id
|
2026-07-06 11:26:50 -05:00
|
|
|
router.delete('/:id', (req: Req, res: Res) => {
|
2026-05-03 19:51:57 -05:00
|
|
|
const db = getDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
const cat = db
|
|
|
|
|
.prepare('SELECT id, name FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NULL')
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!cat) throw NotFoundError('Category not found', 'id');
|
2026-05-04 20:12:57 -05:00
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const deleted = db
|
|
|
|
|
.prepare(
|
|
|
|
|
"UPDATE categories SET deleted_at = datetime('now'), updated_at = datetime('now') WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
|
|
|
|
|
)
|
2026-05-16 10:34:32 -05:00
|
|
|
.run(req.params.id, req.user.id);
|
2026-05-04 20:12:57 -05:00
|
|
|
|
2026-05-16 10:34:32 -05:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
deleted: deleted.changes,
|
|
|
|
|
deleted_category_id: cat.id,
|
|
|
|
|
deleted_category_name: cat.name,
|
|
|
|
|
recoverable_until_days: 30,
|
2026-05-04 20:12:57 -05:00
|
|
|
});
|
2026-05-16 10:34:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /api/categories/:id/restore — undo category soft delete
|
2026-07-06 11:26:50 -05:00
|
|
|
router.post('/:id/restore', (req: Req, res: Res) => {
|
2026-05-16 10:34:32 -05:00
|
|
|
const db = getDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
const cat = db
|
|
|
|
|
.prepare(
|
|
|
|
|
'SELECT id, name FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NOT NULL',
|
|
|
|
|
)
|
|
|
|
|
.get(req.params.id, req.user.id);
|
2026-07-06 14:46:32 -05:00
|
|
|
if (!cat) throw NotFoundError('Deleted category not found', 'id');
|
2026-07-06 14:23:53 -05:00
|
|
|
|
|
|
|
|
db.prepare(
|
|
|
|
|
"UPDATE categories SET deleted_at = NULL, updated_at = datetime('now') WHERE id = ? AND user_id = ?",
|
|
|
|
|
).run(req.params.id, req.user.id);
|
|
|
|
|
res.json(
|
|
|
|
|
db
|
|
|
|
|
.prepare('SELECT * FROM categories WHERE id = ? AND user_id = ?')
|
|
|
|
|
.get(req.params.id, req.user.id),
|
|
|
|
|
);
|
2026-05-03 19:51:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|