fix(admin): issue #147 — Catalog Tree city edit/delete, sort order, subcategory ops

- handleRename: city rename via /api/locations/rename-city (all rows), district via PUT /api/locations/{id}
- City-level delete: DELETE /api/locations/by-city (blocks on dependencies, 400 with counts)
- Sort order: idempotent ensureSortOrderColumns (ALTER TABLE try/catch) + PATCH /api/{type}s/{id}/sort + up/down arrows on district/category/subcategory
- Tree queries order by sort_order asc, then name
- Subcategory rename/delete verified
- tsc clean
This commit is contained in:
NW
2026-08-10 10:55:12 +01:00
parent 1b7050002e
commit 9ac7afa36e
9 changed files with 434 additions and 11 deletions

View File

@@ -0,0 +1,21 @@
import { db } from '@/lib/db';
/**
* Idempotently adds sort_order columns to locations, categories, and subcategories.
* Uses raw SQL with try/catch since SQLite doesn't support IF NOT EXISTS for ALTER TABLE ADD COLUMN.
*/
export async function ensureSortOrderColumns(): Promise<void> {
const migrations = [
'ALTER TABLE locations ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
'ALTER TABLE categories ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
'ALTER TABLE subcategories ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
];
for (const sql of migrations) {
try {
await db.$executeRawUnsafe(sql);
} catch {
// Column already exists — ignore
}
}
}