feat(geoportal-v2): export toolbar + Semnez ca picker + CF intern/Extras split
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) <noreply@anthropic.com>
This commit is contained in:
@@ -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_<cad>_<UAT>_<YYYY-MM-DD>.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_<cad>_<UAT>_<YYYY-MM-DD>.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_<cad>.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_<cad>`, 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 `<cad>.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 <PARCEL_UUID> with the GisFeature uuid for that cadref.)
|
||||
|
||||
# Coord
|
||||
curl -fsS https://tools.beletage.ro/api/gis/parcel/<PARCEL_UUID>/coords \
|
||||
-H "Cookie: $(...your session cookie...)" -o coord.xlsx
|
||||
|
||||
# DXF
|
||||
curl -fsS https://tools.beletage.ro/api/gis/parcel/<PARCEL_UUID>/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/<PARCEL_UUID>/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
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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<SignAsCreateInput>;
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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,
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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<ExportKind, string> = {
|
||||
piz: "Plan de încadrare în zonă",
|
||||
pad: "Plan de situație (PAD)",
|
||||
coords: "Coordonate Stereo70 (XLSX)",
|
||||
dxf: "Export DXF",
|
||||
};
|
||||
|
||||
const DESCRIPTION: Record<ExportKind, string> = {
|
||||
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<ExportKind, boolean> = {
|
||||
piz: true,
|
||||
pad: true,
|
||||
coords: false,
|
||||
dxf: false,
|
||||
};
|
||||
|
||||
const NEEDS_BASEMAP: Record<ExportKind, boolean> = {
|
||||
piz: true,
|
||||
pad: false,
|
||||
coords: false,
|
||||
dxf: false,
|
||||
};
|
||||
|
||||
const FILE_EXT: Record<ExportKind, string> = {
|
||||
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<SignerPayload | null>(null);
|
||||
const [coSigner, setCoSigner] = useState<SignerPayload | null>(null);
|
||||
const [basemap, setBasemap] = useState<"google" | "orto">("google");
|
||||
const [state, setState] = useState<State>({ kind: "idle" });
|
||||
const lastBlobUrl = useRef<string | null>(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<string, unknown> = {};
|
||||
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(
|
||||
<div
|
||||
className="fixed inset-0 z-[1100] flex items-center justify-center bg-black/40 p-4"
|
||||
onClick={onClose}
|
||||
>
|
||||
<div
|
||||
className="w-full max-w-md overflow-hidden rounded-lg border bg-background shadow-xl"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="flex items-start gap-2 border-b bg-muted/30 px-3 py-2">
|
||||
{kind === "piz" ? (
|
||||
<MapIcon className="mt-0.5 h-4 w-4 text-primary" />
|
||||
) : (
|
||||
<FileText className="mt-0.5 h-4 w-4 text-primary" />
|
||||
)}
|
||||
<div className="flex-1">
|
||||
<div className="text-sm font-semibold">{TITLE[kind]}</div>
|
||||
<div className="text-[11px] text-muted-foreground">
|
||||
{cadastralRef && <span className="font-mono">{cadastralRef}</span>}
|
||||
{cadastralRef && uatName && " · "}
|
||||
{uatName}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded p-1 hover:bg-muted"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="max-h-[60vh] space-y-3 overflow-y-auto px-3 py-3">
|
||||
<p className="text-xs text-muted-foreground">{DESCRIPTION[kind]}</p>
|
||||
|
||||
{NEEDS_BASEMAP[kind] && (
|
||||
<div>
|
||||
<div className="mb-1 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
|
||||
Fundal hartă
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBasemap("google")}
|
||||
className={cn(
|
||||
"flex-1 rounded-md border px-2 py-1.5 text-xs",
|
||||
basemap === "google"
|
||||
? "border-primary bg-primary/5 font-medium"
|
||||
: "border-border hover:bg-muted/50",
|
||||
)}
|
||||
>
|
||||
Google Satellite
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBasemap("orto")}
|
||||
className={cn(
|
||||
"flex-1 rounded-md border px-2 py-1.5 text-xs",
|
||||
basemap === "orto"
|
||||
? "border-primary bg-primary/5 font-medium"
|
||||
: "border-border hover:bg-muted/50",
|
||||
)}
|
||||
>
|
||||
Ortofoto ANCPI
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{NEEDS_SIGNER[kind] && (
|
||||
<SignAsPicker
|
||||
value={signer}
|
||||
onChange={setSigner}
|
||||
allowCoSigner={kind === "piz"}
|
||||
coSigner={coSigner}
|
||||
onCoSignerChange={setCoSigner}
|
||||
/>
|
||||
)}
|
||||
|
||||
{state.kind === "error" && (
|
||||
<div className="flex items-start gap-2 rounded border border-destructive/40 bg-destructive/10 p-2 text-xs text-destructive">
|
||||
<AlertCircle className="mt-0.5 h-3.5 w-3.5 flex-shrink-0" />
|
||||
<div className="min-w-0">{state.message}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{state.kind === "done" && (
|
||||
<div className="flex items-start gap-2 rounded border border-emerald-500/40 bg-emerald-500/10 p-2 text-xs">
|
||||
<Download className="mt-0.5 h-3.5 w-3.5 flex-shrink-0 text-emerald-600" />
|
||||
<div className="min-w-0">
|
||||
<div className="font-medium">Gata.</div>
|
||||
<a
|
||||
href={state.blobUrl}
|
||||
download={state.filename}
|
||||
className="text-primary underline-offset-2 hover:underline"
|
||||
>
|
||||
{state.filename}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end gap-2 border-t bg-muted/30 px-3 py-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded px-3 py-1.5 text-xs hover:bg-muted"
|
||||
>
|
||||
Închide
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleGenerate}
|
||||
disabled={state.kind === "running" || (NEEDS_SIGNER[kind] && !signer)}
|
||||
className="inline-flex items-center gap-1 rounded bg-primary px-3 py-1.5 text-xs font-medium text-primary-foreground disabled:opacity-50"
|
||||
>
|
||||
{state.kind === "running" ? (
|
||||
<>
|
||||
<Loader2 className="h-3 w-3 animate-spin" /> Generare…
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Download className="h-3 w-3" /> Generează
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
@@ -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<CondoOwner[] | null>(null);
|
||||
const [condoLoading, setCondoLoading] = useState(false);
|
||||
const [cfModalOpen, setCfModalOpen] = useState(false);
|
||||
const [cfInternBusy, setCfInternBusy] = useState(false);
|
||||
const [cfInternError, setCfInternError] = useState<string | null>(null);
|
||||
const [exportKind, setExportKind] = useState<ExportKind | null>(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<boolean>(
|
||||
typeof window !== "undefined" &&
|
||||
sessionStorage.getItem(AUTH_RETRY_KEY) === "1",
|
||||
@@ -1297,20 +1348,87 @@ export function FeatureInfoPanel({ feature, onClose, onSelectFeature, basic = fa
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions toolbar */}
|
||||
<div className="flex flex-wrap gap-1 border-t bg-muted/30 p-1.5">
|
||||
{/* Actions toolbar — exports + CF split */}
|
||||
<div className="space-y-1 border-t bg-muted/30 p-1.5">
|
||||
{/* 4 export buttons row: PIZ / Plan situație / Coord / DXF */}
|
||||
<div className="grid grid-cols-4 gap-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setExportKind("piz")}
|
||||
disabled={!feature.cadastralRef}
|
||||
className="inline-flex flex-col items-center gap-0.5 rounded bg-background px-1 py-1.5 text-[10px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Plan de încadrare în zonă (PDF, 1:5000)"
|
||||
>
|
||||
<MapIcon className="h-3.5 w-3.5" />
|
||||
<span>Încadrare</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setExportKind("pad")}
|
||||
disabled={!feature.cadastralRef}
|
||||
className="inline-flex flex-col items-center gap-0.5 rounded bg-background px-1 py-1.5 text-[10px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Plan de situație / amplasament (PDF)"
|
||||
>
|
||||
<ScrollText className="h-3.5 w-3.5" />
|
||||
<span>Pl. situație</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setExportKind("coords")}
|
||||
disabled={!feature.cadastralRef}
|
||||
className="inline-flex flex-col items-center gap-0.5 rounded bg-background px-1 py-1.5 text-[10px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Coordonate Stereo70 (XLSX)"
|
||||
>
|
||||
<FileSpreadsheet className="h-3.5 w-3.5" />
|
||||
<span>Coord.</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setExportKind("dxf")}
|
||||
disabled={!feature.cadastralRef}
|
||||
className="inline-flex flex-col items-center gap-0.5 rounded bg-background px-1 py-1.5 text-[10px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Export DXF (parcela + vecini)"
|
||||
>
|
||||
<FileBox className="h-3.5 w-3.5" />
|
||||
<span>DXF</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* CF row: intern (free, ~2-3s) vs ANCPI extract (1 credit ePay) */}
|
||||
<div className="grid grid-cols-2 gap-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCfIntern}
|
||||
disabled={cfInternBusy || !feature.cadastralRef || !feature.siruta}
|
||||
className="inline-flex items-center justify-center gap-1 rounded bg-background px-2 py-1.5 text-[11px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Descarcă extras CF intern din eTerra (gratuit)"
|
||||
>
|
||||
{cfInternBusy ? (
|
||||
<Loader2 className="h-3 w-3 animate-spin" />
|
||||
) : (
|
||||
<FileText className="h-3 w-3" />
|
||||
)}
|
||||
CF intern
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setCfModalOpen(true)}
|
||||
disabled={!feature.cadastralRef || !feature.siruta}
|
||||
className="inline-flex items-center gap-1 rounded bg-background px-2 py-1 text-[11px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Comandă extras Carte Funciară (1 credit ePay)"
|
||||
className="inline-flex items-center justify-center gap-1 rounded bg-background px-2 py-1.5 text-[11px] font-medium hover:bg-muted disabled:opacity-50"
|
||||
title="Comandă extras de Carte Funciară prin ePay (1 credit)"
|
||||
>
|
||||
<FileText className="h-3 w-3" />
|
||||
Comandă CF
|
||||
<Receipt className="h-3 w-3" />
|
||||
Extras CF
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{cfInternError && (
|
||||
<div className="rounded border border-destructive/40 bg-destructive/10 px-2 py-1 text-[10px] text-destructive">
|
||||
{cfInternError}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* CF order modal — confirmation + animated multi-step progress */}
|
||||
<CfOrderModal
|
||||
open={cfModalOpen}
|
||||
@@ -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 && (
|
||||
<ExportModal
|
||||
open
|
||||
kind={exportKind}
|
||||
parcelId={detail?.id ? String(detail.id) : feature.id}
|
||||
cadastralRef={feature.cadastralRef}
|
||||
uatName={uatName ?? null}
|
||||
layerId={feature.layerId}
|
||||
onClose={() => setExportKind(null)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<SignAsOption[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(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 (
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<Loader2 className="h-3 w-3 animate-spin" /> Se încarcă semnatari…
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div>
|
||||
<div className="mb-1 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
|
||||
Semnez ca
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
{visiblePrimary.map((opt) => {
|
||||
const checked = isSamePayload(value, toPayload(opt));
|
||||
return (
|
||||
<label
|
||||
key={opt.id}
|
||||
className={cn(
|
||||
"flex cursor-pointer items-start gap-2 rounded-md border p-2 text-xs transition-colors",
|
||||
checked
|
||||
? "border-primary bg-primary/5"
|
||||
: "border-border bg-background hover:bg-muted/50",
|
||||
)}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="signer-primary"
|
||||
className="mt-0.5"
|
||||
checked={checked}
|
||||
onChange={() => onChange(toPayload(opt))}
|
||||
/>
|
||||
{opt.kind === "user" ? (
|
||||
<UserIcon className="mt-0.5 h-3.5 w-3.5 text-muted-foreground" />
|
||||
) : (
|
||||
<Building2 className="mt-0.5 h-3.5 w-3.5 text-muted-foreground" />
|
||||
)}
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-1 font-medium">
|
||||
{formatSignerShort(opt)}
|
||||
{opt.isDefault && (
|
||||
<Star className="h-3 w-3 text-amber-500" aria-label="Implicit" />
|
||||
)}
|
||||
</div>
|
||||
{opt.authClass && (
|
||||
<div className="text-[10px] text-muted-foreground">
|
||||
{opt.authClass}
|
||||
{opt.source === "env" ? " · prestabilit" : ""}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{!showAdd ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAdd(true)}
|
||||
className="mt-1 inline-flex items-center gap-1 rounded px-1.5 py-1 text-[10px] text-primary hover:bg-primary/10"
|
||||
>
|
||||
<Plus className="h-3 w-3" /> Adaugă autorizație nouă
|
||||
</button>
|
||||
) : (
|
||||
<div className="mt-1 space-y-1 rounded-md border border-dashed bg-muted/40 p-2">
|
||||
<div className="flex gap-1">
|
||||
<label className="flex flex-1 items-center gap-1 text-[10px]">
|
||||
<input
|
||||
type="radio"
|
||||
checked={draft.kind === "user"}
|
||||
onChange={() => setDraft((d) => ({ ...d, kind: "user" }))}
|
||||
/>
|
||||
PFA
|
||||
</label>
|
||||
<label className="flex flex-1 items-center gap-1 text-[10px]">
|
||||
<input
|
||||
type="radio"
|
||||
checked={draft.kind === "org"}
|
||||
onChange={() => setDraft((d) => ({ ...d, kind: "org" }))}
|
||||
/>
|
||||
PJA
|
||||
</label>
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Nume / Denumire"
|
||||
value={draft.displayName}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({ ...d, displayName: e.target.value }))
|
||||
}
|
||||
className="w-full rounded border bg-background px-2 py-1 text-xs"
|
||||
/>
|
||||
<div className="flex gap-1">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Cat./Clasa"
|
||||
value={draft.authClass}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({ ...d, authClass: e.target.value }))
|
||||
}
|
||||
className="w-1/3 rounded border bg-background px-2 py-1 text-xs"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Nr. autorizație"
|
||||
value={draft.authNumber}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({ ...d, authNumber: e.target.value }))
|
||||
}
|
||||
className="flex-1 rounded border bg-background px-2 py-1 text-xs"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
<button
|
||||
type="button"
|
||||
disabled={addBusy}
|
||||
onClick={handleAdd}
|
||||
className="rounded bg-primary px-2 py-1 text-[10px] font-medium text-primary-foreground disabled:opacity-50"
|
||||
>
|
||||
{addBusy ? "Salvez…" : "Salvează"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAdd(false)}
|
||||
className="rounded px-2 py-1 text-[10px] hover:bg-muted"
|
||||
>
|
||||
Anulează
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{allowCoSigner && value && onCoSignerChange && (
|
||||
<div>
|
||||
<div className="mb-1 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
|
||||
Co-semnatar (opțional)
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<label
|
||||
className={cn(
|
||||
"flex cursor-pointer items-start gap-2 rounded-md border p-2 text-xs transition-colors",
|
||||
!coSigner
|
||||
? "border-primary bg-primary/5"
|
||||
: "border-border bg-background hover:bg-muted/50",
|
||||
)}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="signer-co"
|
||||
className="mt-0.5"
|
||||
checked={!coSigner}
|
||||
onChange={() => onCoSignerChange(null)}
|
||||
/>
|
||||
<div className="text-muted-foreground">Fără co-semnatar</div>
|
||||
</label>
|
||||
{visibleSecondary.map((opt) => {
|
||||
const checked = isSamePayload(coSigner ?? null, toPayload(opt));
|
||||
return (
|
||||
<label
|
||||
key={opt.id}
|
||||
className={cn(
|
||||
"flex cursor-pointer items-start gap-2 rounded-md border p-2 text-xs transition-colors",
|
||||
checked
|
||||
? "border-primary bg-primary/5"
|
||||
: "border-border bg-background hover:bg-muted/50",
|
||||
)}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="signer-co"
|
||||
className="mt-0.5"
|
||||
checked={checked}
|
||||
onChange={() => onCoSignerChange(toPayload(opt))}
|
||||
/>
|
||||
{opt.kind === "user" ? (
|
||||
<UserIcon className="mt-0.5 h-3.5 w-3.5 text-muted-foreground" />
|
||||
) : (
|
||||
<Building2 className="mt-0.5 h-3.5 w-3.5 text-muted-foreground" />
|
||||
)}
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="font-medium">{formatSignerShort(opt)}</div>
|
||||
{opt.authClass && (
|
||||
<div className="text-[10px] text-muted-foreground">{opt.authClass}</div>
|
||||
)}
|
||||
</div>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="rounded border border-destructive/40 bg-destructive/10 px-2 py-1 text-[11px] text-destructive">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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<SignAsCreateInput>;
|
||||
|
||||
/** 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}`;
|
||||
}
|
||||
Reference in New Issue
Block a user