mirror of
https://origin.cursor.com/mrdevmx/panels.git
synced 2026-10-09 17:13:18 +00:00
<!-- 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> <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> </div>
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
export type FrontRpcCode = "VALIDATION" | "NETWORK" | "INTERNAL";
|
|
|
|
export type FrontResponse<T = unknown> = {
|
|
ok: boolean;
|
|
code: FrontRpcCode | string;
|
|
layer: "front";
|
|
message: string;
|
|
context?: Record<string, unknown>;
|
|
data?: T | null;
|
|
errors?: Record<string, string> | string[] | null;
|
|
};
|
|
|
|
export function buildFrontError(
|
|
message: string,
|
|
code: FrontRpcCode = "VALIDATION",
|
|
context?: Record<string, unknown>,
|
|
errors?: Record<string, string> | string[] | null,
|
|
): FrontResponse {
|
|
return {
|
|
ok: false,
|
|
code,
|
|
layer: "front",
|
|
message,
|
|
context,
|
|
data: null,
|
|
errors: errors ?? null,
|
|
};
|
|
}
|
|
|
|
export function buildFrontNetworkError(method: string, path: string): FrontResponse {
|
|
return buildFrontError(
|
|
`No se pudo conectar con el servidor en ${method} ${path} — verifique su conexión`,
|
|
"NETWORK",
|
|
{ route: `${method} ${path}` },
|
|
);
|
|
}
|
|
|
|
export function isApiEnvelope(value: unknown): value is {
|
|
ok: boolean;
|
|
code: string;
|
|
status?: number;
|
|
layer: string;
|
|
message: string;
|
|
} {
|
|
return !!value && typeof value === "object"
|
|
&& "ok" in value && "message" in value && "layer" in value;
|
|
}
|
|
|
|
export function displayErrorMessage(value: unknown, fallback = "Ocurrió un error"): string {
|
|
if (isApiEnvelope(value)) return value.message;
|
|
if (value && typeof value === "object" && "message" in value) {
|
|
return String((value as { message: unknown }).message);
|
|
}
|
|
if (value && typeof value === "object" && "error" in value) {
|
|
return String((value as { error: unknown }).error);
|
|
}
|
|
return fallback;
|
|
}
|