This repository was archived by the owner on Apr 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
143 lines (133 loc) · 4.66 KB
/
Copy pathroute.ts
File metadata and controls
143 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
CertificationsApiResponse,
Certification,
isCertLevel,
isCertStatus,
} from "@/lib/certifications/model";
import { pool } from "@/lib/db/client";
import { NextRequest, NextResponse } from "next/server";
let schemaReady = false;
async function ensureSchema() {
if (schemaReady || !pool) return;
await pool.query(`
CREATE TABLE IF NOT EXISTS certifications (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
level TEXT NOT NULL DEFAULT 'Federal',
authority TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'NOT_STARTED',
due_date TEXT,
applied_date TEXT,
decision_expected TEXT,
expires_date TEXT,
description TEXT,
notes TEXT,
documents TEXT DEFAULT '[]',
created_at TEXT NOT NULL DEFAULT NOW()::TEXT,
updated_at TEXT NOT NULL DEFAULT NOW()::TEXT
)
`);
schemaReady = true;
}
function rowToCert(row: Record<string, unknown>): Certification {
const documents = (() => {
try {
const parsed = JSON.parse((row.documents as string) || "[]");
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
})();
return {
id: typeof row.id === "string" ? row.id : "",
name: typeof row.name === "string" ? row.name : "",
level: isCertLevel(row.level) ? row.level : "Federal",
authority: typeof row.authority === "string" ? row.authority : "",
status: isCertStatus(row.status) ? row.status : "NOT_STARTED",
dueDate: typeof row.due_date === "string" ? row.due_date : undefined,
appliedDate:
typeof row.applied_date === "string" ? row.applied_date : undefined,
decisionExpected:
typeof row.decision_expected === "string"
? row.decision_expected
: undefined,
expiresDate:
typeof row.expires_date === "string" ? row.expires_date : undefined,
description:
typeof row.description === "string" ? row.description : undefined,
notes: typeof row.notes === "string" ? row.notes : undefined,
documents,
};
}
export async function GET() {
if (!pool) return NextResponse.json({ error: "Database not configured" }, { status: 503 });
try {
await ensureSchema();
const result = await pool.query(
`SELECT id, name, level, authority, status, due_date, applied_date,
decision_expected, expires_date, description, notes, documents
FROM certifications ORDER BY level, name`
);
const response: CertificationsApiResponse = {
data: result.rows.map(rowToCert),
meta: {
lastUpdated: new Date().toISOString(),
source: "database",
},
};
return NextResponse.json(response);
} catch (error) {
console.error("[Certifications API] Error:", error);
return NextResponse.json({ error: error instanceof Error ? error.message : "Failed to fetch data" }, { status: 500 });
}
}
export async function POST(request: NextRequest) {
if (!pool) {
return NextResponse.json({ error: "Database not configured" }, { status: 503 });
}
try {
await ensureSchema();
const body = await request.json();
const id = (body.id as string)?.trim() || `cert-${Date.now()}`;
const name = (body.name as string)?.trim();
if (!name) {
return NextResponse.json({ error: "Name is required" }, { status: 400 });
}
const now = new Date().toISOString();
const level = isCertLevel(body.level) ? body.level : "Federal";
const status = isCertStatus(body.status) ? body.status : "NOT_STARTED";
const authority = typeof body.authority === "string" ? body.authority : "";
const documents = JSON.stringify(
Array.isArray(body.documents) ? body.documents : []
);
const result = await pool.query(
`INSERT INTO certifications (id, name, level, authority, status, due_date, applied_date,
decision_expected, expires_date, description, notes, documents, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $13)
RETURNING id, name, level, authority, status, due_date, applied_date,
decision_expected, expires_date, description, notes, documents`,
[
id,
name,
level,
authority,
status,
body.dueDate ?? null,
body.appliedDate ?? null,
body.decisionExpected ?? null,
body.expiresDate ?? null,
body.description ?? null,
body.notes ?? null,
documents,
now,
]
);
return NextResponse.json(rowToCert(result.rows[0]));
} catch (error) {
console.error("[Certifications API] Create error:", error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Create failed" },
{ status: 500 }
);
}
}