From 71cfc29f9ad4046cc69d52fda964a6853e266800 Mon Sep 17 00:00:00 2001 From: Claude VM Date: Thu, 21 May 2026 07:57:55 +0300 Subject: [PATCH] feat(geoportal-v2): export toolbar + Semnez ca picker + CF intern/Extras split MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V2 panel toolbar replaces the single "Comandă CF" button with two rows: [Încadrare] [Pl. situație] [Coord.] [DXF] ← 4 exports [CF intern] [Extras CF] ← 2 CF flows Each export button pops an inline modal: - PIZ / PAD: SignAsPicker (PFA / PJA radio list, manual-add inline, co-signer slot on PIZ) + basemap toggle (google / orto for PIZ). - Coord / DXF: no picker — single-click download via JWT proxy. "CF intern" is the free copycf flow from eTerra (proxied via gis-api); "Extras CF" keeps the existing CfOrderModal (1 credit ePay). The two modes are now visually balanced as a 2-button row. Sign-as picker rows merge user-owned Signatory table entries with the SIGN_AS_DEFAULT_OPTIONS env-driven fallback (org-wide hardcoded options; defaults seed two Studii de teren entries — Tiurbe PFA + SRL PJA). New rows added via the picker's "Adaugă autorizație" inline form write to the Signatory table; ENV rows are read-only. Architots side ships fully: - prisma Signatory model + ALTER TABLE applied (per the schema-drift feedback memory). - /api/sign-as-options (GET, POST) + /api/sign-as-options/[id] (PATCH, DELETE). - /api/cf-intern/order and /api/gis/parcel/[id]/{piz,pad,coords,dxf} proxy routes — auth check + JWT forward, stream binary back. - gis-api thin client extended with the matching exports.* namespace. Until the gis-api endpoints ship (next session — full spec in docs/plans/005-gis-api-export-endpoints.md), each export proxy returns 501 "…urmează" with a Romanian message so the modal shows what's coming instead of a hard error. Co-Authored-By: Claude Opus 4.7 (1M context) --- docs/plans/005-gis-api-export-endpoints.md | 346 ++++++++++++++++++ prisma/schema.prisma | 22 ++ src/app/api/cf-intern/order/route.ts | 60 +++ src/app/api/gis/parcel/[id]/coords/route.ts | 53 +++ src/app/api/gis/parcel/[id]/dxf/route.ts | 67 ++++ src/app/api/gis/parcel/[id]/pad/route.ts | 92 +++++ src/app/api/gis/parcel/[id]/piz/route.ts | 97 +++++ src/app/api/sign-as-options/[id]/route.ts | 100 +++++ src/app/api/sign-as-options/route.ts | 117 ++++++ src/lib/gis-api-client.ts | 69 ++++ .../geoportal/v2/exports/export-modal.tsx | 286 +++++++++++++++ .../geoportal/v2/feature-info-panel.tsx | 161 +++++++- .../geoportal/v2/sign-as/env-defaults.ts | 81 ++++ .../geoportal/v2/sign-as/sign-as-picker.tsx | 324 ++++++++++++++++ src/modules/geoportal/v2/sign-as/types.ts | 57 +++ 15 files changed, 1917 insertions(+), 15 deletions(-) create mode 100644 docs/plans/005-gis-api-export-endpoints.md create mode 100644 src/app/api/cf-intern/order/route.ts create mode 100644 src/app/api/gis/parcel/[id]/coords/route.ts create mode 100644 src/app/api/gis/parcel/[id]/dxf/route.ts create mode 100644 src/app/api/gis/parcel/[id]/pad/route.ts create mode 100644 src/app/api/gis/parcel/[id]/piz/route.ts create mode 100644 src/app/api/sign-as-options/[id]/route.ts create mode 100644 src/app/api/sign-as-options/route.ts create mode 100644 src/modules/geoportal/v2/exports/export-modal.tsx create mode 100644 src/modules/geoportal/v2/sign-as/env-defaults.ts create mode 100644 src/modules/geoportal/v2/sign-as/sign-as-picker.tsx create mode 100644 src/modules/geoportal/v2/sign-as/types.ts diff --git a/docs/plans/005-gis-api-export-endpoints.md b/docs/plans/005-gis-api-export-endpoints.md new file mode 100644 index 0000000..a9a92e1 --- /dev/null +++ b/docs/plans/005-gis-api-export-endpoints.md @@ -0,0 +1,346 @@ +# Plan 005 — gis-api export endpoints (PIZ / PAD / Coord / DXF / CF intern) + +**Date:** 2026-05-21 +**Author:** ArchiTools session (V2 panel toolbar) +**Owner of execution:** gis-api session (`/home/orchestrator/Code/gis-api`) +**Status:** Architots side complete; gis-api endpoints not yet built. + +--- + +## Why this exists + +The V2 panel on ArchiTools now exposes a 4-button export toolbar +(PIZ / Plan situație / Coord. / DXF) plus a 2-button CF row (CF intern / +Extras CF). Every button except "Extras CF" routes through gis-api so a +future eterra.live migration or new app (planhub) consumes the same +endpoints — same code path, same auth, same audit log. + +Architots ships the UI + a thin proxy under `/api/gis/parcel/[id]/{piz, +pad,coords,dxf}` and `/api/cf-intern/order`. Each route forwards to +gis-api with the user's Authentik JWT, then streams the response back. +Until these gis-api endpoints exist, those routes surface a 404 from +gis-api as a friendly Romanian message in the modal ("…urmează — +endpoint-ul gis-api se livrează în sesiunea următoare"). + +Source for the heavy lifting (PDF renderers, basemap fetchers, DXF +writer, ExcelJS layout) is `~/Code/eterra-live/src/`. The relevant +files are listed below per endpoint. Port them into gis-api; keep them +provider-agnostic (no eterra.live user table, no usage tracking). + +--- + +## Shared contract + +All endpoints below take the **GisFeature uuid** in the path. Architots +already resolves the uuid via the find chain (uuid → by-ref → search) +and passes it in. The endpoints look up geometry + enrichment via +`gis_core."GisFeature"` (or the orchestrator's enrichment path when a +fresh fetch is needed). + +### Auth + +- Bearer JWT (Authentik) — same as every other v1 endpoint. +- `enrichment_scope` claim must be at least `basic` for CF intern; + `none` is allowed for the geometry-only exports (PIZ/PAD/Coord/DXF). +- Per-tenant rate limit applies (existing `GIS_API_TENANT_RATE_LIMITS`). + +### Errors + +| Status | Code | Architots renders… | +|--------|-------------------------|------------------------------------------------------| +| 400 | `signer_required` | "Selectează un semnatar." | +| 400 | `invalid_geometry` | "Geometria parcelei e invalidă." | +| 401 | `unauthorized` | "Autentificare expirată. Reîncarcă pagina." | +| 403 | `enrichment_scope_*` | "Nu ai permisiunea pentru acest export." | +| 404 | `parcel_not_found` | "Parcela nu există în baza gis_core." | +| 502 | `eterra_fetch_failed` | "ANCPI nu răspunde momentan." | +| 503 | `no_available_account` | "Pool-ul ANCPI e temporar epuizat." | +| 504 | `upstream_timeout` | "Timeout la generare. Reîncearcă." | + +Architots already handles each of these (see `feature-info-panel.tsx` +error mapping for the deep-enrich endpoint — same shape). + +### Signer payload shape + +```ts +type Signer = { + kind: "user" | "org"; // PFA vs PJA + displayName: string; // "Dan-Gheorghe Tiurbe" or "Studii de teren SRL" + authClass: string | null; // "Cat. D" / "Clasa III" + authNumber: string; // "RO-B-F/3183" +}; +``` + +gis-api never resolves signers — architots picks them client-side +(merged DB rows + ENV defaults) and sends the final string-only struct +in the request body. gis-api renders verbatim into the PDF footer / +PIZ cartouche. Co-signer rendering follows the same shape. + +--- + +## Endpoint 1: `POST /api/v1/parcel/:id/piz` + +PDF — Plan de încadrare în zonă, A4 portrait, scara 1:5000 (1:10000 +peste 400 m), parcela conturată cu roșu peste fundal satelit / orto. + +### Request body + +```ts +{ + signer: Signer, // required + coSigner?: Signer | null, // optional second name in footer + basemap?: "google" | "orto", // default "google" + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE", // disambiguator +} +``` + +### Response + +`200 application/pdf` with `Content-Disposition: attachment; filename="PZ___.pdf"`. + +### Port from eterra.live + +- `src/app/api/geoportal/piz/route.ts` (logic + filename + content-disposition) +- `src/lib/piz/renderer.ts` (PDF layout) +- `src/lib/piz/google-tiles.ts` (Google satellite tile composite, EPSG:3857) +- `src/lib/piz/overlay-tiles.ts` (Harta 1970 overlay — optional, can ship later) +- `src/lib/eterra-basemap.ts` (ANCPI ortofoto WMS) +- `src/lib/pad/geometry.ts` (`extractVertices`, `computeBBox`) +- `src/lib/auth-display.ts` (`formatSignerOneLine`) +- `src/lib/categorie-folosinta.ts` + +### Replace in port + +- Drop `getEffectiveSession` → use gis-api's bearer-token + claims context. +- Drop `prisma.eterraUser.findUnique` → use the signer struct from body. +- Drop `resolveSignerIdentity` → trust body.signer (architots-side validated). +- Drop `trackUsage` → optional; can keep a thin audit row in gis_meta if + desired, but skip credit accounting. +- `enrichFeatureIfNeeded` → call the existing + orchestrator path (or auto-trigger `/api/v1/parcel/enrich` internally). + Best-effort: render with whatever's in DB on failure (filename + footer + okay even without NR_CF). + +### Special considerations + +- Whichever PDF lib eterra.live uses (skia-canvas, pdf-lib, etc.) needs + to be added to gis-api's package.json. Memory says eterra.live runs + Next 16 with skia-canvas — should port cleanly. +- Google tiles require an HTTP fetch grid. Cache per-bbox (Redis or + filesystem) to avoid hammering Google when users repeat exports. +- Ortofoto requires ANCPI WMS auth — needs a service account in + Infisical (`/gis-api/ANCPI_ORTOFOTO_USER` + `_PASS`?). + +--- + +## Endpoint 2: `POST /api/v1/parcel/:id/pad` + +PDF — Plan de amplasament și delimitare. Auto-picks scale + paper + +orientation based on geometry; rasters neighbors (parcels + +buildings) within the drawing bbox; renders a cartouche with CF / topo +/ categorie / adresă / coords table. + +### Request body + +```ts +{ + signer: Signer, // required (single signer) + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE", + scale?: number, // override autoSelectLayout + paper?: string, // "A4" | "A3" | … +} +``` + +### Response + +`200 application/pdf`, `PS___.pdf`. + +### Port from eterra.live + +- `src/app/api/geoportal/pad/route.ts` +- `src/lib/pad/renderer.ts` +- `src/lib/pad/geometry.ts` (`computeSegments`, `computeArea`, + `computePerimeter`, `autoSelectLayout`, `fillBboxToDrawable`) +- Plus same shared libs as PIZ. + +### Special considerations + +- Neighbor SQL stays in raw `$queryRaw` (already PostGIS-aware, + intersection with the draw bbox, `isSubject` flag for buildings). + Just point at gis-api's gis_core connection. +- No basemap fetch — PAD is line-drawing only. + +--- + +## Endpoint 3: `GET /api/v1/parcel/:id/coords-xlsx` + +XLSX — coordonate Stereo70 per vârf, lungimi segmente, suprafață +calculată Shoelace. 3 sheets (Sumar / Coordonate / Segmente). + +### Request + +No body. The parcel id in the path is sufficient. + +### Response + +`200 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`, +filename `coordonate_.xlsx`. + +### Port from eterra.live + +- `src/app/api/geoportal/coords-xlsx/route.ts` (~280 LOC, mostly + ExcelJS scaffolding). One file, minimal dependencies (`exceljs` + + Prisma) — easiest port; recommend starting here to validate the + contract end-to-end. + +### Replace in port + +- `prisma.gisFeature.findMany` already targets gis_core in eterra.live + — same shape on gis-api side, just swap the client import. +- Drop `enrichment` and `attributes` reads if scope doesn't permit + (Coord works fine with geometry alone). + +--- + +## Endpoint 4: `POST /api/v1/parcel/:id/dxf` + +DXF — parcela ca polilinie închisă pe layer-ul `PARCELA_`, plus +vecini opționali pe layer `VECINI` și clădiri pe `CLADIRI`. Coordonate +Stereo70. + +### Request body + +```ts +{ + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE", + includeNeighbors?: boolean, // default true for TERENURI, false for CLADIRI +} +``` + +### Response + +`200 application/dxf`, filename `.dxf`. + +### Port from eterra.live + +- `src/app/api/geoportal/export/route.ts` (single-parcel branch when + `format=dxf`) +- `src/lib/dxf-writer.ts` (the pure-JS DXF emitter — no native deps) + +### Replace in port + +- Strip the `geojson` + `gpkg` branches — gis-api endpoint is DXF-only + (architots GeoJSON export already exists via the gis-api search path, + and GPKG is rarely useful for single-parcel workflows). +- The neighbor query is similar to PAD's — share the helper. + +--- + +## Endpoint 5: `POST /api/v1/enrichment/cf-intern` + +PDF — extras CF din circuitul intern eTerra (`copycf`). Gratuit (nu +consumă credit ePay). Streams the eTerra response directly. + +### Request body + +```ts +{ + nrCadastral: string, // "354686" + siruta: string, // "54975" +} +``` + +### Response + +`200 application/pdf` with the eTerra-generated filename in +`Content-Disposition`. + +### Port from eterra.live + +- `src/app/api/eterra/cf-intern/route.ts` +- `src/lib/eterra-session.ts` (account pool — likely already partially + shared with `/parcel/enrich` orchestrator path) +- Rate limit: keep eterra.live's `RATE_LIMITS.docDownload` window + (default 10/h per user — tune in gis-api env). + +### Replace in port + +- Drop usage tracking + per-user eterra session caching tied to + `eterraUser` — use the account pool that already serves enrichment. +- Persist the CF row via gis-api's existing + `prisma.cfExtract` writer (CfExtract table on gis_core or + architools_postgres depending on architecture decision — likely + gis_core for consistency). + +### Special considerations + +- Already-cached CF (catalog hit) should short-circuit and return the + cached PDF — saves an eTerra round-trip. The orchestrator's + `enrichment/catalog/:nrCadastral` endpoint can drive this. + +--- + +## Implementation order recommendation + +1. **Coord-XLSX (Endpoint 3)** — simplest, validates auth + contract. +2. **DXF (Endpoint 4)** — also simple, no PDF lib needed. +3. **PAD (Endpoint 2)** — first PDF endpoint, no basemap; isolates the + skia-canvas / pdf lib selection question. +4. **CF intern (Endpoint 5)** — depends on eTerra account pool; orth- + ogonal to the others. +5. **PIZ (Endpoint 1)** — hardest, needs basemap fetcher + tile cache. + +Architots already speaks all five via the thin client +(`gisApi.exports.{piz,pad,coordsXlsx,dxf,cfIntern}` in +`src/lib/gis-api-client.ts`). No architots change required as each +gis-api endpoint ships. + +--- + +## Verification per endpoint + +After each ships: + +```bash +# From architots cwd, on the live deploy (assumes test cad 354686 in UAT Cluj-Napoca, siruta 54975): +# (Replace with the GisFeature uuid for that cadref.) + +# Coord +curl -fsS https://tools.beletage.ro/api/gis/parcel//coords \ + -H "Cookie: $(...your session cookie...)" -o coord.xlsx + +# DXF +curl -fsS https://tools.beletage.ro/api/gis/parcel//dxf \ + -X POST -H "Content-Type: application/json" -d '{}' \ + -H "Cookie: $(...)" -o test.dxf + +# PIZ (substitute signer) +curl -fsS https://tools.beletage.ro/api/gis/parcel//piz \ + -X POST -H "Content-Type: application/json" \ + -d '{"signer":{"kind":"user","displayName":"Test","authClass":"Cat. D","authNumber":"RO-B-F/3183"},"basemap":"google"}' \ + -H "Cookie: $(...)" -o test_piz.pdf +``` + +Open each in its native viewer (Excel, AutoCAD/QCAD, PDF reader) +to confirm rendering parity with eterra.live's output. + +--- + +## Out of scope for this plan + +- Bulk-export multi-parcel (UAT-level) — separate flow, will live at + `/api/v1/uat/:siruta/export` once Coord + DXF singles are stable. +- Mobile-first signer UI — current picker is desktop layout only. +- PIZ overlay raster (Harta 1970) — port last, low priority. +- Cache eviction policy for the Google tile composites — initial impl + can be filesystem with daily rotation. + +--- + +## Related memory + +- [[architots-cutover-state-2026-05-20]] — V2 panel state +- [[v2-panel-contract]] — panel section layout +- [[parcel-enrich-contract]] — gis-api /parcel/enrich auto vs manual +- [[gis-api-parcela-by-ref]] — find chain endpoint +- [[architots-auto-deploy-webhook]] — push → live in ~90s diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d38f06b..b991086 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -202,3 +202,25 @@ model CfExtract { @@index([userId]) @@index([userId, nrCadastral]) } + +// ─── Geoportal Exports: Signatories (PIZ/PAD "Semnez ca:") ───────── +// Each row is an authorisation a user can sign exports with: themselves +// as a PFA (kind=user) or on behalf of an org/PJA (kind=org). The picker +// merges these rows with the SIGN_AS_DEFAULT_OPTIONS env-driven fallback +// (hardcoded org-wide entries) so a fresh install has usable defaults +// before anyone configures their own. +model Signatory { + id String @id @default(uuid()) + userId String /// Authentik sub of the owner + kind String /// 'user' | 'org' + displayName String /// "Dan-Gheorghe Tiurbe" or "Studii de teren SRL" + authClass String? /// "Cat. D" / "Clasa III" + authNumber String /// "RO-B-F/3183" + isDefault Boolean @default(false) /// primary signer for this user + notes String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + @@index([userId]) + @@index([userId, isDefault]) +} diff --git a/src/app/api/cf-intern/order/route.ts b/src/app/api/cf-intern/order/route.ts new file mode 100644 index 0000000..98d553b --- /dev/null +++ b/src/app/api/cf-intern/order/route.ts @@ -0,0 +1,60 @@ +// POST /api/cf-intern/order +// +// Internal-circuit CF download — free eTerra `copycf` flow proxied via +// gis-api. Body: { nrCadastral, siruta }. Streams the PDF back. +// +// Until gis-api ships POST /api/v1/enrichment/cf-intern (see docs/plans/005-…), +// this route surfaces gis-api's 404 as a friendly "shipping next" message. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { gisApi, GisApiError } from "@/lib/gis-api-client"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +type Body = { nrCadastral?: string; siruta?: string }; + +export async function POST(request: Request) { + const session = await getAuthSession(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const body = (await request.json()) as Body; + if (!body.nrCadastral?.trim() || !body.siruta?.trim()) { + return NextResponse.json( + { error: "missing_params", message: "Necesare: nrCadastral, siruta." }, + { status: 400 }, + ); + } + + try { + const upstream = await gisApi.exports.cfIntern({ + nrCadastral: body.nrCadastral.trim(), + siruta: body.siruta.trim(), + }); + const headers = new Headers(); + headers.set("Content-Type", upstream.headers.get("Content-Type") ?? "application/pdf"); + const cd = upstream.headers.get("Content-Disposition"); + if (cd) headers.set("Content-Disposition", cd); + return new NextResponse(upstream.body, { status: 200, headers }); + } catch (err) { + if (err instanceof GisApiError) { + if (err.status === 404) { + return NextResponse.json( + { + error: "endpoint_not_deployed", + message: + "CF intern urmează — endpoint-ul gis-api se livrează în sesiunea următoare.", + }, + { status: 501 }, + ); + } + return NextResponse.json( + { error: err.code, body: err.body }, + { status: err.status }, + ); + } + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: "internal_error", hint: msg.slice(0, 200) }, { status: 500 }); + } +} diff --git a/src/app/api/gis/parcel/[id]/coords/route.ts b/src/app/api/gis/parcel/[id]/coords/route.ts new file mode 100644 index 0000000..edea6a6 --- /dev/null +++ b/src/app/api/gis/parcel/[id]/coords/route.ts @@ -0,0 +1,53 @@ +// GET /api/gis/parcel/[id]/coords +// +// Forwards the Stereo70 coordinate XLSX export to gis-api. No request body — +// the parcel id in the path is enough. Until gis-api ships +// /api/v1/parcel/:id/coords-xlsx (see docs/plans/005-…), returns 501. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { gisApi, GisApiError } from "@/lib/gis-api-client"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +export async function GET( + _request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const session = await getAuthSession(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const { id } = await params; + try { + const upstream = await gisApi.exports.coordsXlsx(id); + const headers = new Headers(); + headers.set( + "Content-Type", + upstream.headers.get("Content-Type") ?? + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ); + const cd = upstream.headers.get("Content-Disposition"); + if (cd) headers.set("Content-Disposition", cd); + return new NextResponse(upstream.body, { status: 200, headers }); + } catch (err) { + if (err instanceof GisApiError) { + if (err.status === 404) { + return NextResponse.json( + { + error: "endpoint_not_deployed", + message: + "Export de coordonate urmează — endpoint-ul gis-api se livrează în sesiunea următoare.", + }, + { status: 501 }, + ); + } + return NextResponse.json( + { error: err.code, body: err.body }, + { status: err.status }, + ); + } + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: "internal_error", hint: msg.slice(0, 200) }, { status: 500 }); + } +} diff --git a/src/app/api/gis/parcel/[id]/dxf/route.ts b/src/app/api/gis/parcel/[id]/dxf/route.ts new file mode 100644 index 0000000..c045978 --- /dev/null +++ b/src/app/api/gis/parcel/[id]/dxf/route.ts @@ -0,0 +1,67 @@ +// POST /api/gis/parcel/[id]/dxf +// +// Forwards a DXF export request to gis-api. Body is optional: +// { layerId?, includeNeighbors? }. Until gis-api ships /api/v1/parcel/:id/dxf +// (see docs/plans/005-…), returns 501. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { gisApi, GisApiError } from "@/lib/gis-api-client"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +type Body = { + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; + includeNeighbors?: boolean; +}; + +export async function POST( + request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const session = await getAuthSession(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const { id } = await params; + let body: Body = {}; + try { + body = (await request.json()) as Body; + } catch { + body = {}; + } + + try { + const upstream = await gisApi.exports.dxf(id, { + layerId: body.layerId, + includeNeighbors: body.includeNeighbors, + }); + const headers = new Headers(); + headers.set( + "Content-Type", + upstream.headers.get("Content-Type") ?? "application/dxf", + ); + const cd = upstream.headers.get("Content-Disposition"); + if (cd) headers.set("Content-Disposition", cd); + return new NextResponse(upstream.body, { status: 200, headers }); + } catch (err) { + if (err instanceof GisApiError) { + if (err.status === 404) { + return NextResponse.json( + { + error: "endpoint_not_deployed", + message: + "Export DXF urmează — endpoint-ul gis-api se livrează în sesiunea următoare.", + }, + { status: 501 }, + ); + } + return NextResponse.json( + { error: err.code, body: err.body }, + { status: err.status }, + ); + } + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: "internal_error", hint: msg.slice(0, 200) }, { status: 500 }); + } +} diff --git a/src/app/api/gis/parcel/[id]/pad/route.ts b/src/app/api/gis/parcel/[id]/pad/route.ts new file mode 100644 index 0000000..ff6b4e4 --- /dev/null +++ b/src/app/api/gis/parcel/[id]/pad/route.ts @@ -0,0 +1,92 @@ +// POST /api/gis/parcel/[id]/pad +// +// Forwards a PAD (Plan de amplasament și delimitare) export request to gis-api. +// Until gis-api ships POST /api/v1/parcel/:id/pad (see docs/plans/005-…), +// this route returns 501 with a friendly Romanian message. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { gisApi, GisApiError } from "@/lib/gis-api-client"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +type SignerInput = { + kind?: unknown; + displayName?: unknown; + authClass?: unknown; + authNumber?: unknown; +}; + +type Body = { + signer?: SignerInput; + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; + scale?: number; + paper?: string; +}; + +function parseSigner(raw: SignerInput | null | undefined) { + if (!raw || (raw.kind !== "user" && raw.kind !== "org")) return null; + if (typeof raw.displayName !== "string" || !raw.displayName.trim()) return null; + if (typeof raw.authNumber !== "string" || !raw.authNumber.trim()) return null; + return { + kind: raw.kind as "user" | "org", + displayName: raw.displayName.trim(), + authClass: + typeof raw.authClass === "string" && raw.authClass.trim() + ? raw.authClass.trim() + : null, + authNumber: raw.authNumber.trim(), + }; +} + +export async function POST( + request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const session = await getAuthSession(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const { id } = await params; + const body = (await request.json()) as Body; + const signer = parseSigner(body.signer); + if (!signer) { + return NextResponse.json({ error: "signer_required" }, { status: 400 }); + } + + try { + const upstream = await gisApi.exports.pad(id, { + signer, + layerId: body.layerId, + scale: body.scale, + paper: body.paper, + }); + const headers = new Headers(); + headers.set( + "Content-Type", + upstream.headers.get("Content-Type") ?? "application/pdf", + ); + const cd = upstream.headers.get("Content-Disposition"); + if (cd) headers.set("Content-Disposition", cd); + return new NextResponse(upstream.body, { status: 200, headers }); + } catch (err) { + if (err instanceof GisApiError) { + if (err.status === 404) { + return NextResponse.json( + { + error: "endpoint_not_deployed", + message: + "Plan de situație urmează — endpoint-ul gis-api se livrează în sesiunea următoare.", + }, + { status: 501 }, + ); + } + return NextResponse.json( + { error: err.code, body: err.body }, + { status: err.status }, + ); + } + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: "internal_error", hint: msg.slice(0, 200) }, { status: 500 }); + } +} diff --git a/src/app/api/gis/parcel/[id]/piz/route.ts b/src/app/api/gis/parcel/[id]/piz/route.ts new file mode 100644 index 0000000..99d13a9 --- /dev/null +++ b/src/app/api/gis/parcel/[id]/piz/route.ts @@ -0,0 +1,97 @@ +// POST /api/gis/parcel/[id]/piz +// +// Forwards a PIZ (Plan de încadrare în zonă) export request to gis-api. +// Body shape: { signer, coSigner?, basemap?, layerId? } (see types in +// src/modules/geoportal/v2/sign-as/types.ts). gis-api renders the PDF and +// returns the binary stream — we forward it as-is. +// +// Until gis-api ships POST /api/v1/parcel/:id/piz (see docs/plans/005-…), +// this route surfaces gis-api's 404 as a friendly "shipping next" message. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { gisApi, GisApiError } from "@/lib/gis-api-client"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +type SignerInput = { + kind?: unknown; + displayName?: unknown; + authClass?: unknown; + authNumber?: unknown; +}; + +type Body = { + signer?: SignerInput; + coSigner?: SignerInput | null; + basemap?: "google" | "orto"; + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; +}; + +function parseSigner(raw: SignerInput | null | undefined) { + if (!raw || (raw.kind !== "user" && raw.kind !== "org")) return null; + if (typeof raw.displayName !== "string" || !raw.displayName.trim()) return null; + if (typeof raw.authNumber !== "string" || !raw.authNumber.trim()) return null; + return { + kind: raw.kind as "user" | "org", + displayName: raw.displayName.trim(), + authClass: + typeof raw.authClass === "string" && raw.authClass.trim() + ? raw.authClass.trim() + : null, + authNumber: raw.authNumber.trim(), + }; +} + +export async function POST( + request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const session = await getAuthSession(); + if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const { id } = await params; + const body = (await request.json()) as Body; + const signer = parseSigner(body.signer); + if (!signer) { + return NextResponse.json({ error: "signer_required" }, { status: 400 }); + } + const coSigner = body.coSigner ? parseSigner(body.coSigner) : null; + + try { + const upstream = await gisApi.exports.piz(id, { + signer, + coSigner, + basemap: body.basemap ?? "google", + layerId: body.layerId, + }); + const headers = new Headers(); + headers.set( + "Content-Type", + upstream.headers.get("Content-Type") ?? "application/pdf", + ); + const cd = upstream.headers.get("Content-Disposition"); + if (cd) headers.set("Content-Disposition", cd); + return new NextResponse(upstream.body, { status: 200, headers }); + } catch (err) { + if (err instanceof GisApiError) { + if (err.status === 404) { + return NextResponse.json( + { + error: "endpoint_not_deployed", + message: + "Plan de încadrare urmează — endpoint-ul gis-api se livrează în sesiunea următoare.", + }, + { status: 501 }, + ); + } + return NextResponse.json( + { error: err.code, body: err.body }, + { status: err.status }, + ); + } + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: "internal_error", hint: msg.slice(0, 200) }, { status: 500 }); + } +} diff --git a/src/app/api/sign-as-options/[id]/route.ts b/src/app/api/sign-as-options/[id]/route.ts new file mode 100644 index 0000000..1101a46 --- /dev/null +++ b/src/app/api/sign-as-options/[id]/route.ts @@ -0,0 +1,100 @@ +// PATCH /api/sign-as-options/[id] +// → update a user-owned Signatory. ENV-source rows (id starts with "env-") +// are read-only and return 400. +// DELETE /api/sign-as-options/[id] +// → remove a user-owned Signatory. ENV rows return 400. + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { prisma } from "@/core/storage/prisma"; +import type { SignAsUpdateInput } from "@/modules/geoportal/v2/sign-as/types"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +function sessionUserId(session: unknown): string | null { + const s = session as { user?: { id?: unknown } } | null; + const id = s?.user?.id; + return typeof id === "string" && id.length > 0 ? id : null; +} + +export async function PATCH( + request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const { id } = await params; + if (id.startsWith("env-")) { + return NextResponse.json({ error: "env_row_readonly" }, { status: 400 }); + } + const session = await getAuthSession(); + const userId = sessionUserId(session); + if (!userId) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const existing = await prisma.signatory.findUnique({ where: { id } }); + if (!existing || existing.userId !== userId) { + return NextResponse.json({ error: "not_found" }, { status: 404 }); + } + + const body = (await request.json()) as SignAsUpdateInput; + if (body.kind && body.kind !== "user" && body.kind !== "org") { + return NextResponse.json({ error: "kind_invalid" }, { status: 400 }); + } + + if (body.isDefault === true) { + await prisma.signatory.updateMany({ + where: { userId, isDefault: true, NOT: { id } }, + data: { isDefault: false }, + }); + } + + const row = await prisma.signatory.update({ + where: { id }, + data: { + ...(body.kind !== undefined ? { kind: body.kind } : {}), + ...(body.displayName !== undefined + ? { displayName: body.displayName.trim() } + : {}), + ...(body.authClass !== undefined + ? { authClass: body.authClass?.trim() || null } + : {}), + ...(body.authNumber !== undefined + ? { authNumber: body.authNumber.trim() } + : {}), + ...(body.isDefault !== undefined ? { isDefault: body.isDefault } : {}), + ...(body.notes !== undefined ? { notes: body.notes?.trim() || null } : {}), + }, + }); + + return NextResponse.json({ + option: { + id: row.id, + source: "db" as const, + kind: row.kind as "user" | "org", + displayName: row.displayName, + authClass: row.authClass, + authNumber: row.authNumber, + isDefault: row.isDefault, + notes: row.notes, + }, + }); +} + +export async function DELETE( + _request: Request, + { params }: { params: Promise<{ id: string }> }, +) { + const { id } = await params; + if (id.startsWith("env-")) { + return NextResponse.json({ error: "env_row_readonly" }, { status: 400 }); + } + const session = await getAuthSession(); + const userId = sessionUserId(session); + if (!userId) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const existing = await prisma.signatory.findUnique({ where: { id } }); + if (!existing || existing.userId !== userId) { + return NextResponse.json({ error: "not_found" }, { status: 404 }); + } + await prisma.signatory.delete({ where: { id } }); + return NextResponse.json({ ok: true }); +} diff --git a/src/app/api/sign-as-options/route.ts b/src/app/api/sign-as-options/route.ts new file mode 100644 index 0000000..3ef4262 --- /dev/null +++ b/src/app/api/sign-as-options/route.ts @@ -0,0 +1,117 @@ +// GET /api/sign-as-options +// → merged list of the caller's own Signatory rows + ENV-default rows +// visible to their company. Used by the V2 panel's "Semnez ca:" picker. +// +// POST /api/sign-as-options +// → create a new user-owned Signatory row. +// Body: SignAsCreateInput from "@/modules/geoportal/v2/sign-as/types". + +import { NextResponse } from "next/server"; +import { getAuthSession } from "@/core/auth/require-auth"; +import { prisma } from "@/core/storage/prisma"; +import { getEnvDefaults } from "@/modules/geoportal/v2/sign-as/env-defaults"; +import type { + SignAsCreateInput, + SignAsOption, +} from "@/modules/geoportal/v2/sign-as/types"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; + +function sessionUserId(session: unknown): string | null { + const s = session as { user?: { id?: unknown } } | null; + const id = s?.user?.id; + return typeof id === "string" && id.length > 0 ? id : null; +} + +function sessionCompany(session: unknown): string | null { + const s = session as { user?: { company?: unknown } } | null; + const c = s?.user?.company; + return typeof c === "string" && c.length > 0 ? c : null; +} + +export async function GET() { + const session = await getAuthSession(); + const userId = sessionUserId(session); + if (!userId) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const company = sessionCompany(session); + const dbRows = await prisma.signatory.findMany({ + where: { userId }, + orderBy: [{ isDefault: "desc" }, { createdAt: "asc" }], + }); + + const envRows = getEnvDefaults(company); + + // Drop env rows that the user has overridden with a same-(kind,authNumber) + // personal entry — their copy takes precedence. + const overridden = new Set(dbRows.map((r) => `${r.kind}:${r.authNumber}`)); + const envVisible = envRows.filter( + (r) => !overridden.has(`${r.kind}:${r.authNumber}`), + ); + + const out: SignAsOption[] = [ + ...dbRows.map((r) => ({ + id: r.id, + source: "db" as const, + kind: r.kind as "user" | "org", + displayName: r.displayName, + authClass: r.authClass, + authNumber: r.authNumber, + isDefault: r.isDefault, + notes: r.notes, + })), + ...envVisible, + ]; + + return NextResponse.json({ options: out }); +} + +export async function POST(request: Request) { + const session = await getAuthSession(); + const userId = sessionUserId(session); + if (!userId) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + + const body = (await request.json()) as Partial; + if (!body.kind || (body.kind !== "user" && body.kind !== "org")) { + return NextResponse.json({ error: "kind_invalid" }, { status: 400 }); + } + if (!body.displayName?.trim()) { + return NextResponse.json({ error: "displayName_required" }, { status: 400 }); + } + if (!body.authNumber?.trim()) { + return NextResponse.json({ error: "authNumber_required" }, { status: 400 }); + } + + if (body.isDefault) { + await prisma.signatory.updateMany({ + where: { userId, isDefault: true }, + data: { isDefault: false }, + }); + } + + const row = await prisma.signatory.create({ + data: { + userId, + kind: body.kind, + displayName: body.displayName.trim(), + authClass: body.authClass?.trim() || null, + authNumber: body.authNumber.trim(), + isDefault: Boolean(body.isDefault), + notes: body.notes?.trim() || null, + }, + }); + + return NextResponse.json({ + option: { + id: row.id, + source: "db" as const, + kind: row.kind as "user" | "org", + displayName: row.displayName, + authClass: row.authClass, + authNumber: row.authNumber, + isDefault: row.isDefault, + notes: row.notes, + }, + }); +} diff --git a/src/lib/gis-api-client.ts b/src/lib/gis-api-client.ts index cf07cba..43f7053 100644 --- a/src/lib/gis-api-client.ts +++ b/src/lib/gis-api-client.ts @@ -410,4 +410,73 @@ export const gisApi = { accessToken: opts.accessToken, }), }, + + // ── Exports (geoportal V2 panel toolbar) ───────────────────────── + // All four endpoints return binary bodies (PDF / XLSX / DXF) — proxy + // them through rawResponse so the architots route handler can stream + // straight to the browser without re-encoding. + // + // Contract documented in docs/plans/005-gis-api-export-endpoints.md. + // Implementation pending in gis-api session — until shipped, each call + // here will get a 404 from gis-api, which architots routes surface + // as a friendly "Această funcție urmează" message. + exports: { + piz: ( + id: string, + body: { + signer: { kind: "user" | "org"; displayName: string; authClass: string | null; authNumber: string }; + coSigner?: { kind: "user" | "org"; displayName: string; authClass: string | null; authNumber: string } | null; + basemap?: "google" | "orto"; + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; + }, + opts: GisApiCallOpts = {}, + ) => + rawResponse(`/api/v1/parcel/${encodeURIComponent(id)}/piz`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + accessToken: opts.accessToken, + }), + pad: ( + id: string, + body: { + signer: { kind: "user" | "org"; displayName: string; authClass: string | null; authNumber: string }; + layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; + scale?: number; + paper?: string; + }, + opts: GisApiCallOpts = {}, + ) => + rawResponse(`/api/v1/parcel/${encodeURIComponent(id)}/pad`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + accessToken: opts.accessToken, + }), + coordsXlsx: (id: string, opts: GisApiCallOpts = {}) => + rawResponse(`/api/v1/parcel/${encodeURIComponent(id)}/coords-xlsx`, { + accessToken: opts.accessToken, + }), + dxf: ( + id: string, + body: { layerId?: "TERENURI_ACTIVE" | "CLADIRI_ACTIVE"; includeNeighbors?: boolean } = {}, + opts: GisApiCallOpts = {}, + ) => + rawResponse(`/api/v1/parcel/${encodeURIComponent(id)}/dxf`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + accessToken: opts.accessToken, + }), + cfIntern: ( + body: { nrCadastral: string; siruta: string }, + opts: GisApiCallOpts = {}, + ) => + rawResponse("/api/v1/enrichment/cf-intern", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + accessToken: opts.accessToken, + }), + }, }; diff --git a/src/modules/geoportal/v2/exports/export-modal.tsx b/src/modules/geoportal/v2/exports/export-modal.tsx new file mode 100644 index 0000000..04221a0 --- /dev/null +++ b/src/modules/geoportal/v2/exports/export-modal.tsx @@ -0,0 +1,286 @@ +"use client"; + +import { useEffect, useRef, useState } from "react"; +import { createPortal } from "react-dom"; +import { X, Loader2, Download, AlertCircle, FileText, Map as MapIcon } from "lucide-react"; +import { cn } from "@/shared/lib/utils"; +import { SignAsPicker } from "../sign-as/sign-as-picker"; +import type { SignerPayload } from "../sign-as/types"; + +export type ExportKind = "piz" | "pad" | "coords" | "dxf"; + +export interface ExportModalProps { + open: boolean; + kind: ExportKind; + /** GisFeature uuid (when known) — used in the route path. */ + parcelId: string; + /** Display-only metadata for the modal header. */ + cadastralRef: string | null; + uatName: string | null; + layerId: string | null; + onClose: () => void; +} + +const TITLE: Record = { + piz: "Plan de încadrare în zonă", + pad: "Plan de situație (PAD)", + coords: "Coordonate Stereo70 (XLSX)", + dxf: "Export DXF", +}; + +const DESCRIPTION: Record = { + piz: "PDF A4 cu parcela încadrată la scara 1:5000 peste imagine satelit / ortofoto ANCPI.", + pad: "PDF cu plan de amplasament + delimitare, cu cartuș (CF, topo, categorie, adresă) și tabel de coordonate.", + coords: "Tabel Excel cu coordonatele vârfurilor (X/Y Stereo70), lungimile segmentelor și suprafața.", + dxf: "Fișier DXF al parcelei (+ vecini și clădiri din intersecție) pentru deschidere în AutoCAD / QCAD.", +}; + +const NEEDS_SIGNER: Record = { + piz: true, + pad: true, + coords: false, + dxf: false, +}; + +const NEEDS_BASEMAP: Record = { + piz: true, + pad: false, + coords: false, + dxf: false, +}; + +const FILE_EXT: Record = { + piz: "pdf", + pad: "pdf", + coords: "xlsx", + dxf: "dxf", +}; + +type State = + | { kind: "idle" } + | { kind: "running" } + | { kind: "done"; blobUrl: string; filename: string } + | { kind: "error"; message: string }; + +export function ExportModal({ + open, + kind, + parcelId, + cadastralRef, + uatName, + layerId, + onClose, +}: ExportModalProps) { + const [signer, setSigner] = useState(null); + const [coSigner, setCoSigner] = useState(null); + const [basemap, setBasemap] = useState<"google" | "orto">("google"); + const [state, setState] = useState({ kind: "idle" }); + const lastBlobUrl = useRef(null); + + // Reset transient state when the modal closes or jumps to a new parcel. + useEffect(() => { + if (!open) { + setState({ kind: "idle" }); + if (lastBlobUrl.current) { + URL.revokeObjectURL(lastBlobUrl.current); + lastBlobUrl.current = null; + } + } + }, [open, parcelId]); + + if (!open) return null; + if (typeof document === "undefined") return null; + + const handleGenerate = async () => { + setState({ kind: "running" }); + try { + const body: Record = {}; + if (NEEDS_SIGNER[kind]) { + if (!signer) { + setState({ kind: "error", message: "Selectează un semnatar." }); + return; + } + body.signer = signer; + if (coSigner) body.coSigner = coSigner; + } + if (NEEDS_BASEMAP[kind]) { + body.basemap = basemap; + } + if (layerId) body.layerId = layerId; + + const isJsonOnly = kind === "coords" || kind === "dxf"; + const res = await fetch(`/api/gis/parcel/${parcelId}/${kind}`, { + method: isJsonOnly && kind === "coords" ? "GET" : "POST", + headers: { "Content-Type": "application/json" }, + body: isJsonOnly && kind === "coords" ? undefined : JSON.stringify(body), + }); + + if (!res.ok) { + let msg = `Eroare HTTP ${res.status}`; + const text = await res.text(); + try { + const j = JSON.parse(text); + if (typeof j.error === "string") msg = j.error; + else if (typeof j.message === "string") msg = j.message; + } catch { + if (text) msg += `: ${text.slice(0, 200)}`; + } + setState({ kind: "error", message: msg }); + return; + } + + const blob = await res.blob(); + const url = URL.createObjectURL(blob); + lastBlobUrl.current = url; + const cd = res.headers.get("content-disposition") ?? ""; + const m = /filename\*?=(?:UTF-8''|"?)([^";]+)/i.exec(cd); + const fallback = `${kind}_${cadastralRef ?? parcelId}.${FILE_EXT[kind]}`; + const filename = m?.[1] ? decodeURIComponent(m[1]) : fallback; + setState({ kind: "done", blobUrl: url, filename }); + + // Auto-download — most users just want the file. + const a = document.createElement("a"); + a.href = url; + a.download = filename; + a.click(); + } catch (err) { + setState({ + kind: "error", + message: err instanceof Error ? err.message : String(err), + }); + } + }; + + return createPortal( +
+
e.stopPropagation()} + > +
+ {kind === "piz" ? ( + + ) : ( + + )} +
+
{TITLE[kind]}
+
+ {cadastralRef && {cadastralRef}} + {cadastralRef && uatName && " · "} + {uatName} +
+
+ +
+ +
+

{DESCRIPTION[kind]}

+ + {NEEDS_BASEMAP[kind] && ( +
+
+ Fundal hartă +
+
+ + +
+
+ )} + + {NEEDS_SIGNER[kind] && ( + + )} + + {state.kind === "error" && ( +
+ +
{state.message}
+
+ )} + + {state.kind === "done" && ( +
+ + +
+ )} +
+ +
+ + +
+
+
, + document.body, + ); +} diff --git a/src/modules/geoportal/v2/feature-info-panel.tsx b/src/modules/geoportal/v2/feature-info-panel.tsx index efc0470..4667a7e 100644 --- a/src/modules/geoportal/v2/feature-info-panel.tsx +++ b/src/modules/geoportal/v2/feature-info-panel.tsx @@ -7,9 +7,11 @@ import { Home, Building, Building2, MapPin, ChevronRight, Users, Sparkles, ShieldCheck, AlertTriangle, HelpCircle, Factory, Warehouse, + Map as MapIcon, FileSpreadsheet, FileBox, Receipt, ScrollText, } from "lucide-react"; import { cn } from "@/shared/lib/utils"; import { CfOrderModal } from "./cf-order-modal"; +import { ExportModal, type ExportKind } from "./exports/export-modal"; import { useUatName } from "./uat-lookup"; const AUTH_RETRY_KEY = "gis_panel_auth_retry"; @@ -406,14 +408,63 @@ export function FeatureInfoPanel({ feature, onClose, onSelectFeature, basic = fa const [condoOwners, setCondoOwners] = useState(null); const [condoLoading, setCondoLoading] = useState(false); const [cfModalOpen, setCfModalOpen] = useState(false); + const [cfInternBusy, setCfInternBusy] = useState(false); + const [cfInternError, setCfInternError] = useState(null); + const [exportKind, setExportKind] = useState(null); - // Close the CF modal whenever the user switches to a different - // parcel — keeps the modal scoped to a single decision instead of - // silently re-targeting mid-flight. + // Close the modals whenever the user switches to a different parcel — + // keeps the modal scoped to a single decision instead of silently + // re-targeting mid-flight. useEffect(() => { setCfModalOpen(false); + setExportKind(null); + setCfInternError(null); }, [feature.cadastralRef, feature.siruta, feature.layerId]); + const handleCfIntern = useCallback(async () => { + if (!feature.cadastralRef || !feature.siruta) return; + setCfInternBusy(true); + setCfInternError(null); + try { + const res = await fetch("/api/cf-intern/order", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + nrCadastral: feature.cadastralRef, + siruta: feature.siruta, + }), + }); + if (!res.ok) { + let msg = `Eroare HTTP ${res.status}`; + const text = await res.text(); + try { + const j = JSON.parse(text); + if (typeof j.error === "string") msg = j.error; + } catch { + if (text) msg += `: ${text.slice(0, 200)}`; + } + setCfInternError(msg); + return; + } + const blob = await res.blob(); + const url = URL.createObjectURL(blob); + const cd = res.headers.get("content-disposition") ?? ""; + const m = /filename\*?=(?:UTF-8''|"?)([^";]+)/i.exec(cd); + const filename = m?.[1] + ? decodeURIComponent(m[1]) + : `cf_intern_${feature.cadastralRef}.pdf`; + const a = document.createElement("a"); + a.href = url; + a.download = filename; + a.click(); + setTimeout(() => URL.revokeObjectURL(url), 30_000); + } catch (err) { + setCfInternError(err instanceof Error ? err.message : String(err)); + } finally { + setCfInternBusy(false); + } + }, [feature.cadastralRef, feature.siruta]); + const authRetriedRef = useRef( typeof window !== "undefined" && sessionStorage.getItem(AUTH_RETRY_KEY) === "1", @@ -1297,18 +1348,85 @@ export function FeatureInfoPanel({ feature, onClose, onSelectFeature, basic = fa )} - {/* Actions toolbar */} -
- + {/* Actions toolbar — exports + CF split */} +
+ {/* 4 export buttons row: PIZ / Plan situație / Coord / DXF */} +
+ + + + +
+ + {/* CF row: intern (free, ~2-3s) vs ANCPI extract (1 credit ePay) */} +
+ + +
+ + {cfInternError && ( +
+ {cfInternError} +
+ )}
{/* CF order modal — confirmation + animated multi-step progress */} @@ -1318,6 +1436,19 @@ export function FeatureInfoPanel({ feature, onClose, onSelectFeature, basic = fa siruta={feature.siruta} onClose={() => setCfModalOpen(false)} /> + + {/* Export modal — used by all 4 of PIZ/PAD/Coord/DXF buttons */} + {exportKind && ( + setExportKind(null)} + /> + )}
); } diff --git a/src/modules/geoportal/v2/sign-as/env-defaults.ts b/src/modules/geoportal/v2/sign-as/env-defaults.ts new file mode 100644 index 0000000..42db795 --- /dev/null +++ b/src/modules/geoportal/v2/sign-as/env-defaults.ts @@ -0,0 +1,81 @@ +// Hardcoded fallback rows for the "Semnez ca:" picker, sourced from +// SIGN_AS_DEFAULT_OPTIONS env var (JSON array). When the env var is empty, +// we fall back to a built-in seed of the two Beletage-group authorisations +// the user explicitly named (Tiurbe PFA + Studii de teren SRL PJA) so a +// fresh install has usable defaults without manual configuration. +// +// Env shape (single line, escaped JSON): +// SIGN_AS_DEFAULT_OPTIONS='[{"kind":"user","displayName":"...","authClass":"Cat. D","authNumber":"RO-B-F/3183","company":"studii-de-teren"},...]' + +import type { SignAsOption } from "./types"; + +type RawEnvRow = { + kind?: string; + displayName?: string; + authClass?: string | null; + authNumber?: string; + company?: string | null; + isDefault?: boolean; + notes?: string | null; +}; + +const SEED: RawEnvRow[] = [ + { + kind: "user", + displayName: "Dan-Gheorghe Tiurbe", + authClass: "Cat. D", + authNumber: "RO-B-F/3183", + company: "studii-de-teren", + notes: "PFA", + }, + { + kind: "org", + displayName: "Studii de teren SRL", + authClass: "Clasa III", + authNumber: "RO-B-J/3188", + company: "studii-de-teren", + notes: "PJA", + }, +]; + +function parseEnv(): RawEnvRow[] { + const raw = process.env.SIGN_AS_DEFAULT_OPTIONS?.trim(); + if (!raw) return SEED; + try { + const parsed = JSON.parse(raw); + if (!Array.isArray(parsed)) return SEED; + return parsed as RawEnvRow[]; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + console.warn("[sign-as] SIGN_AS_DEFAULT_OPTIONS parse failed:", msg); + return SEED; + } +} + +/** Build the merged env-default list visible to a given user. Rows without a + * `company` field are global; rows with one are filtered to matching users. + * Returns SignAsOption shape so the API can concat with DB rows cleanly. */ +export function getEnvDefaults(userCompany: string | null): SignAsOption[] { + const rows = parseEnv(); + const out: SignAsOption[] = []; + let idx = 0; + for (const r of rows) { + if (!r.kind || !r.displayName || !r.authNumber) continue; + if (r.kind !== "user" && r.kind !== "org") continue; + if (r.company && userCompany && r.company !== userCompany) continue; + if (r.company && !userCompany) continue; + out.push({ + id: `env-${idx}`, + source: "env", + kind: r.kind, + displayName: r.displayName, + authClass: r.authClass ?? null, + authNumber: r.authNumber, + isDefault: Boolean(r.isDefault), + notes: r.notes ?? null, + company: r.company ?? null, + }); + idx += 1; + } + return out; +} diff --git a/src/modules/geoportal/v2/sign-as/sign-as-picker.tsx b/src/modules/geoportal/v2/sign-as/sign-as-picker.tsx new file mode 100644 index 0000000..f3c13ca --- /dev/null +++ b/src/modules/geoportal/v2/sign-as/sign-as-picker.tsx @@ -0,0 +1,324 @@ +"use client"; + +import { useEffect, useMemo, useState } from "react"; +import { Loader2, User as UserIcon, Building2, Star, Plus } from "lucide-react"; +import { cn } from "@/shared/lib/utils"; +import { + formatSignerShort, + type SignAsOption, + type SignerPayload, +} from "./types"; + +export interface SignAsPickerProps { + value: SignerPayload | null; + onChange: (signer: SignerPayload | null) => void; + /** When true, also offer a co-signer slot (PIZ surveyors commonly sign as + * PFA + their firm's PJA on the same drawing). */ + allowCoSigner?: boolean; + coSigner?: SignerPayload | null; + onCoSignerChange?: (signer: SignerPayload | null) => void; +} + +function toPayload(opt: SignAsOption): SignerPayload { + return { + kind: opt.kind, + displayName: opt.displayName, + authClass: opt.authClass, + authNumber: opt.authNumber, + }; +} + +function isSamePayload(a: SignerPayload | null, b: SignerPayload | null): boolean { + if (!a || !b) return false; + return a.kind === b.kind && a.authNumber === b.authNumber; +} + +export function SignAsPicker({ + value, + onChange, + allowCoSigner = false, + coSigner, + onCoSignerChange, +}: SignAsPickerProps) { + const [options, setOptions] = useState(null); + const [error, setError] = useState(null); + const [showAdd, setShowAdd] = useState(false); + const [addBusy, setAddBusy] = useState(false); + const [draft, setDraft] = useState<{ + kind: "user" | "org"; + displayName: string; + authClass: string; + authNumber: string; + }>({ kind: "user", displayName: "", authClass: "", authNumber: "" }); + + const load = async () => { + try { + const res = await fetch("/api/sign-as-options", { cache: "no-store" }); + if (!res.ok) { + const t = await res.text(); + setError(`Eroare ${res.status}: ${t.slice(0, 200)}`); + return; + } + const data = (await res.json()) as { options: SignAsOption[] }; + setOptions(data.options); + // Auto-select the default if nothing is selected yet. + if (!value && data.options.length > 0) { + const def = data.options.find((o) => o.isDefault) ?? data.options[0]!; + onChange(toPayload(def)); + } + } catch (err) { + setError(err instanceof Error ? err.message : String(err)); + } + }; + + useEffect(() => { + load(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + const visiblePrimary = useMemo( + () => options ?? [], + [options], + ); + + const visibleSecondary = useMemo( + () => + allowCoSigner && options + ? options.filter((o) => !isSamePayload(toPayload(o), value)) + : [], + [allowCoSigner, options, value], + ); + + const handleAdd = async () => { + if (!draft.displayName.trim() || !draft.authNumber.trim()) return; + setAddBusy(true); + try { + const res = await fetch("/api/sign-as-options", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + kind: draft.kind, + displayName: draft.displayName.trim(), + authClass: draft.authClass.trim() || null, + authNumber: draft.authNumber.trim(), + }), + }); + if (!res.ok) { + const t = await res.text(); + setError(`Eroare ${res.status}: ${t.slice(0, 200)}`); + return; + } + const { option } = (await res.json()) as { option: SignAsOption }; + setOptions((prev) => [option, ...(prev ?? [])]); + onChange(toPayload(option)); + setShowAdd(false); + setDraft({ kind: "user", displayName: "", authClass: "", authNumber: "" }); + } finally { + setAddBusy(false); + } + }; + + if (options === null && !error) { + return ( +
+ Se încarcă semnatari… +
+ ); + } + + return ( +
+
+
+ Semnez ca +
+
+ {visiblePrimary.map((opt) => { + const checked = isSamePayload(value, toPayload(opt)); + return ( + + ); + })} +
+ + {!showAdd ? ( + + ) : ( +
+
+ + +
+ + setDraft((d) => ({ ...d, displayName: e.target.value })) + } + className="w-full rounded border bg-background px-2 py-1 text-xs" + /> +
+ + setDraft((d) => ({ ...d, authClass: e.target.value })) + } + className="w-1/3 rounded border bg-background px-2 py-1 text-xs" + /> + + setDraft((d) => ({ ...d, authNumber: e.target.value })) + } + className="flex-1 rounded border bg-background px-2 py-1 text-xs" + /> +
+
+ + +
+
+ )} +
+ + {allowCoSigner && value && onCoSignerChange && ( +
+
+ Co-semnatar (opțional) +
+
+ + {visibleSecondary.map((opt) => { + const checked = isSamePayload(coSigner ?? null, toPayload(opt)); + return ( + + ); + })} +
+
+ )} + + {error && ( +
+ {error} +
+ )} +
+ ); +} diff --git a/src/modules/geoportal/v2/sign-as/types.ts b/src/modules/geoportal/v2/sign-as/types.ts new file mode 100644 index 0000000..8c46396 --- /dev/null +++ b/src/modules/geoportal/v2/sign-as/types.ts @@ -0,0 +1,57 @@ +// Picker rows shown in the PIZ/PAD "Semnez ca:" modal. Two sources: +// - `db`: row in the Signatory table, scoped to a user (id present). +// - `env`: hardcoded fallback from SIGN_AS_DEFAULT_OPTIONS JSON env var, +// surfaced to everyone (or filtered by `company` when set). +// Both share the same shape so the UI doesn't branch on source — only the +// `source` discriminator drives the "remove from my list" affordance. + +export type SignAsKind = "user" | "org"; +export type SignAsSource = "db" | "env"; + +export type SignAsOption = { + id: string; + source: SignAsSource; + kind: SignAsKind; + displayName: string; + authClass: string | null; + authNumber: string; + isDefault: boolean; + notes: string | null; + /** Only set on `source=env` rows that are scoped to a specific company. */ + company?: string | null; +}; + +/** Body of POST /api/sign-as-options. */ +export type SignAsCreateInput = { + kind: SignAsKind; + displayName: string; + authClass?: string | null; + authNumber: string; + isDefault?: boolean; + notes?: string | null; +}; + +/** Body of PATCH /api/sign-as-options/[id]. */ +export type SignAsUpdateInput = Partial; + +/** What the picker actually sends along to PIZ/PAD endpoints in the export + * request body. Strings only — gis-api renders them verbatim into the + * footer / cartus, no further lookup. */ +export type SignerPayload = { + kind: SignAsKind; + displayName: string; + authClass: string | null; + authNumber: string; +}; + +/** Format a one-line label for use in PDF footers / list rows. */ +export function formatSignerLine(s: { displayName: string; authClass?: string | null; authNumber: string }): string { + const cls = s.authClass ? ` - ${s.authClass}` : ""; + return `${s.displayName}${cls} - ${s.authNumber}`; +} + +/** Format the chip / row label shown in the picker UI. */ +export function formatSignerShort(s: { kind: SignAsKind; displayName: string; authClass?: string | null; authNumber: string }): string { + const prefix = s.kind === "user" ? "PFA" : "PJA"; + return `${prefix} ${s.displayName} - ${s.authNumber}`; +}