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:
Claude VM
2026-05-21 07:57:55 +03:00
parent 36840f31f6
commit 71cfc29f9a
15 changed files with 1917 additions and 15 deletions
+60
View File
@@ -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 });
}
}
+67
View File
@@ -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 });
}
}
+92
View File
@@ -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 });
}
}
+97
View File
@@ -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 });
}
}
+100
View File
@@ -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 });
}
+117
View File
@@ -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,
},
});
}