refactor
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const express = require("express");
|
||||
const db = require("../db/polygons");
|
||||
const { normalizeEntityContract } = require("../types/contracts");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -29,7 +30,7 @@ router.get("/", (req, res) => {
|
||||
`;
|
||||
|
||||
const rows = db.prepare(sql).all(...params);
|
||||
res.json(rows.map(normalizeEntityRow));
|
||||
res.json(rows.map(normalizeEntityContract));
|
||||
});
|
||||
|
||||
router.get("/search", (req, res) => {
|
||||
@@ -55,7 +56,7 @@ router.get("/search", (req, res) => {
|
||||
LIMIT ?
|
||||
`).all(pattern, limit);
|
||||
|
||||
res.json(rows.map(normalizeEntityRow));
|
||||
res.json(rows.map(normalizeEntityContract));
|
||||
});
|
||||
|
||||
router.get("/:id", (req, res) => {
|
||||
@@ -75,25 +76,11 @@ router.get("/:id", (req, res) => {
|
||||
return res.status(404).json({ error: "Entity not found" });
|
||||
}
|
||||
|
||||
res.json(normalizeEntityRow(row));
|
||||
res.json(normalizeEntityContract(row));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
function normalizeEntityRow(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
slug: row.slug,
|
||||
description: row.description,
|
||||
type_id: row.type_id,
|
||||
status: row.status,
|
||||
created_at: row.created_at,
|
||||
updated_at: row.updated_at,
|
||||
geometry_count: Number(row.geometry_count || 0),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeLimit(value, fallback = 25, max = 100) {
|
||||
const num = Number(value);
|
||||
if (!Number.isFinite(num)) return fallback;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const express = require("express");
|
||||
const db = require("../db/polygons");
|
||||
const { normalizeFeatureCollectionContract, normalizeFeatureContract } = require("../types/contracts");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -70,13 +71,13 @@ router.get("/", (req, res) => {
|
||||
const geometryIds = rows.map((row) => String(row.id));
|
||||
const linksByGeometryId = loadGeometryLinksByGeometryId(geometryIds);
|
||||
|
||||
res.json({
|
||||
res.json(normalizeFeatureCollectionContract({
|
||||
type: "FeatureCollection",
|
||||
features: rows.map((row) => {
|
||||
const linkedEntities = linksByGeometryId.get(String(row.id)) || [];
|
||||
return buildFeatureFromRow(row, linkedEntities);
|
||||
}),
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -95,7 +96,7 @@ function buildFeatureFromRow(row, linkedEntities = []) {
|
||||
? storedGeometryType
|
||||
: normalizeGeometryType(primaryEntity?.entity_type_id);
|
||||
|
||||
return {
|
||||
return normalizeFeatureContract({
|
||||
type: "Feature",
|
||||
properties: {
|
||||
id: row.id,
|
||||
@@ -112,7 +113,7 @@ function buildFeatureFromRow(row, linkedEntities = []) {
|
||||
entity_type_id: primaryEntity?.entity_type_id || null,
|
||||
},
|
||||
geometry: JSON.parse(row.draw_geometry),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeGeometryType(value) {
|
||||
|
||||
@@ -2,6 +2,14 @@ const express = require("express");
|
||||
const crypto = require("crypto");
|
||||
const db = require("../db/polygons");
|
||||
const { getBBox } = require("../utils/bbox");
|
||||
const {
|
||||
assertEditorSnapshotContract,
|
||||
normalizeEditorSnapshotContract,
|
||||
normalizeSectionCommitContract,
|
||||
normalizeSectionContract,
|
||||
normalizeSectionStateContract,
|
||||
normalizeSectionSubmissionContract,
|
||||
} = require("../types/contracts");
|
||||
|
||||
const router = express.Router();
|
||||
const LOCK_TTL_MS = 15 * 60 * 1000;
|
||||
@@ -673,6 +681,8 @@ function applyLinkScopeSnapshot(scope, now) {
|
||||
}
|
||||
|
||||
function validateSnapshot(snapshot) {
|
||||
assertEditorSnapshotContract(snapshot);
|
||||
|
||||
if (!snapshot || typeof snapshot !== "object" || Array.isArray(snapshot)) {
|
||||
throw createHttpError("snapshot must be an object", 400);
|
||||
}
|
||||
@@ -847,71 +857,25 @@ function updateSubmissionReview(submissionId, status, actor, now, reviewNote) {
|
||||
}
|
||||
|
||||
function normalizeSectionRow(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
description: row.description,
|
||||
user_id: row.user_id || null,
|
||||
created_by: row.created_by,
|
||||
created_at: row.created_at,
|
||||
updated_at: row.updated_at,
|
||||
state: {
|
||||
status: row.status || "editing",
|
||||
head_commit_id: row.head_commit_id || null,
|
||||
version: Number(row.version || 0),
|
||||
locked_by: row.locked_by || null,
|
||||
locked_at: row.locked_at || null,
|
||||
lock_expires_at: row.lock_expires_at || null,
|
||||
},
|
||||
};
|
||||
return normalizeSectionContract(row);
|
||||
}
|
||||
|
||||
function normalizeStateRow(row) {
|
||||
return {
|
||||
section_id: row.section_id,
|
||||
status: row.status,
|
||||
head_commit_id: row.head_commit_id || null,
|
||||
version: Number(row.version || 0),
|
||||
locked_by: row.locked_by || null,
|
||||
locked_at: row.locked_at || null,
|
||||
lock_expires_at: row.lock_expires_at || null,
|
||||
updated_at: row.updated_at,
|
||||
};
|
||||
return normalizeSectionStateContract(row);
|
||||
}
|
||||
|
||||
function normalizeCommitRow(row, includeSnapshot) {
|
||||
const out = {
|
||||
id: row.id,
|
||||
section_id: row.section_id,
|
||||
parent_commit_id: row.parent_commit_id || null,
|
||||
commit_no: Number(row.commit_no),
|
||||
kind: row.kind,
|
||||
restored_from_commit_id: row.restored_from_commit_id || null,
|
||||
created_by: row.created_by,
|
||||
created_at: row.created_at,
|
||||
title: row.title,
|
||||
note: row.note,
|
||||
snapshot_hash: row.snapshot_hash,
|
||||
};
|
||||
if (includeSnapshot) out.snapshot = parseSnapshotJson(row.snapshot_json);
|
||||
return out;
|
||||
if (includeSnapshot) {
|
||||
return normalizeSectionCommitContract(row, parseSnapshotJson(row.snapshot_json));
|
||||
}
|
||||
return normalizeSectionCommitContract(row);
|
||||
}
|
||||
|
||||
function normalizeSubmissionRow(row, includeSnapshot) {
|
||||
const out = {
|
||||
id: row.id,
|
||||
section_id: row.section_id,
|
||||
commit_id: row.commit_id,
|
||||
submitted_by: row.submitted_by,
|
||||
submitted_at: row.submitted_at,
|
||||
status: row.status,
|
||||
reviewed_by: row.reviewed_by || null,
|
||||
reviewed_at: row.reviewed_at || null,
|
||||
review_note: row.review_note || null,
|
||||
snapshot_hash: row.snapshot_hash,
|
||||
};
|
||||
if (includeSnapshot) out.snapshot = parseSnapshotJson(row.snapshot_json);
|
||||
return out;
|
||||
if (includeSnapshot) {
|
||||
return normalizeSectionSubmissionContract(row, parseSnapshotJson(row.snapshot_json));
|
||||
}
|
||||
return normalizeSectionSubmissionContract(row);
|
||||
}
|
||||
|
||||
function buildEmptySnapshot(section) {
|
||||
@@ -944,7 +908,7 @@ function normalizeSnapshotInput(input) {
|
||||
|
||||
function parseSnapshotJson(value) {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
return normalizeEditorSnapshotContract(JSON.parse(value));
|
||||
} catch (_err) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user