No description
Find a file
Alberto Martinez b9bf7044b2 refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors
<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
## Summary

Migrates the `core` schema business logic from inline `db.prepare()` calls in the API to PostgreSQL RPC functions (`core.fn_*`) with a unified JSON envelope for errors and HTTP status mapping.

### Database (Liquibase changesets 006–017)

- **006** — RPC infra: `rpc_ok`, `rpc_err`, `rpc_created`, `rpc_from_exception`
- **007** — Catalogs and tenant settings
- **008** — Companies CRUD
- **009** — Projects, checklists, document lists
- **010** — Workers CRUD, pipeline, checklist, assign
- **011** — Budget CRUD + `fn_budget_replace`
- **012** — Badge jobs
- **013** — Payroll (settings, attendance, loans, weeks, destajo)
- **014** — Worker import batch + document store
- **015** — Project/company/worker document metadata RPCs
- **016–017** — Fixes: `needs_badge` default on worker create; Liquibase `splitStatements:false` on function changesets

### API

- `api/rpc.ts` — `callCoreFn()`, `RpcCallError` (jsonb payload fix: pass JS object, not `JSON.stringify`)
- `api/http_errors.ts` — `mapRpcToStatus()`, `respondRpc()`, `respondApiError()`, `onAppError()`
- Refactored: `main.ts`, `companies.ts`, `db.ts`, `budget.ts`, `excel.ts`, `payroll.ts`, `payroll_http.ts`
- Front helpers: `web-panel/composables/api-response.ts`, `web-saas/composables/api-response.ts`

### Envelope contract

DB functions return `{ ok, code, layer: "db", message, context, data, errors }`. The API adds `status` (HTTP code) via `respondRpc()` / `respondApiError()`.

### Out of scope

`iam`, `platform`, `saas.ts`, auth/sessions, S3, PDF generation, Excel parsing, and bootstrap scripts still use direct SQL where appropriate.

## Test plan

- [x] `deno check main.ts` — compila sin errores de tipos
- [x] `npm run build` — web-panel y web-saas compilan
- [x] `deno test` — 25 tests unitarios (http_errors, companies, budget, mx, document_validity)
- [x] Liquibase migrations `006`–`017` aplicadas en Postgres local (`--context-filter=dev`)
- [x] API levantada localmente; `/v1/health` OK
- [x] Smoke CRUD vía `scripts/crud-smoke-test.sh`: empresas, proyectos, trabajadores, catálogos (create/get/list/patch)
- [ ] Import Excel de trabajadores (flujo multipart + S3/local storage)
- [ ] Import presupuesto desde Excel
- [ ] Flujo nómina: asistencia → cerrar semana
- [ ] CI en el remoto (sin checks reportados aún)
<!-- CURSOR_AGENT_PR_BODY_END -->

<div><a href="https://cursor.com/agents/bc-06667c14-38e8-42a8-9ed9-6b1322f12ae7?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/background-agent?bcId=bc-06667c14-38e8-42a8-9ed9-6b1322f12ae7&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a>&nbsp;</div>
2026-09-04 02:34:43 +00:00
.github/workflows api: --allow-sys para el SDK de AWS/R2 en Deno 2026-09-03 05:49:14 +00:00
api refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors 2026-09-04 02:34:43 +00:00
db refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors 2026-09-04 02:34:43 +00:00
docs api: ping R2 con ListObjects, no HeadBucket 2026-09-03 05:42:13 +00:00
overlays compose: unir migrate y api a la red coolify 2026-09-03 05:25:53 +00:00
scripts refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors 2026-09-04 02:34:43 +00:00
tmp/pdfs Nominas 2026-08-25 20:21:49 -05:00
web first commit 2026-08-21 16:55:01 -05:00
web-panel refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors 2026-09-04 02:34:43 +00:00
web-saas refactor(core): migrate core CRUD to PostgreSQL RPC + unified HTTP errors 2026-09-04 02:34:43 +00:00
.dockerignore first commit 2026-08-21 16:55:01 -05:00
.env.example api: compatibilidad S3 con Cloudflare R2 (reemplazo de Contabo) 2026-09-03 04:40:54 +00:00
.gitignore first commit 2026-08-21 16:55:01 -05:00
docker-compose.yml compose: unir migrate y api a la red coolify 2026-09-03 05:25:53 +00:00
Dockerfile.api api: --allow-sys para el SDK de AWS/R2 en Deno 2026-09-03 05:49:14 +00:00
Dockerfile.migrate deploy: Liquibase automático en cada deploy (job migrate) 2026-09-03 04:54:15 +00:00
Dockerfile.provision db/infra: ETL de datos, backups y limpieza de despliegue (fase 5-7) 2026-09-02 21:05:07 +00:00
Dockerfile.web-panel first commit 2026-08-21 16:55:01 -05:00
Dockerfile.web-saas first commit 2026-08-21 16:55:01 -05:00
package.json ops: sacar el overlay local del glob docker-compose*.yml 2026-09-03 05:19:10 +00:00