feat(api): add create and update functionality for charge points with registration status

This commit is contained in:
2026-03-10 15:48:03 +08:00
parent 2cb89c74b3
commit 08cd00c802
5 changed files with 359 additions and 123 deletions

View File

@@ -35,6 +35,43 @@ app.get("/", async (c) => {
);
});
/** POST /api/charge-points — manually pre-register a charge point */
app.post("/", async (c) => {
const db = useDrizzle();
const body = await c.req.json<{
chargePointIdentifier: string;
chargePointVendor?: string;
chargePointModel?: string;
registrationStatus?: "Accepted" | "Pending" | "Rejected";
feePerKwh?: number;
}>();
if (!body.chargePointIdentifier?.trim()) {
return c.json({ error: "chargePointIdentifier is required" }, 400);
}
if (body.feePerKwh !== undefined && (!Number.isInteger(body.feePerKwh) || body.feePerKwh < 0)) {
return c.json({ error: "feePerKwh must be a non-negative integer" }, 400);
}
const [created] = await db
.insert(chargePoint)
.values({
id: crypto.randomUUID(),
chargePointIdentifier: body.chargePointIdentifier.trim(),
chargePointVendor: body.chargePointVendor?.trim() || "Unknown",
chargePointModel: body.chargePointModel?.trim() || "Unknown",
registrationStatus: body.registrationStatus ?? "Pending",
feePerKwh: body.feePerKwh ?? 0,
})
.returning()
.catch((err: Error) => {
if (err.message.includes("unique")) throw Object.assign(err, { status: 409 });
throw err;
});
return c.json({ ...created, connectors: [] }, 201);
});
/** GET /api/charge-points/:id — single charge point */
app.get("/:id", async (c) => {
const db = useDrizzle();
@@ -49,29 +86,50 @@ app.get("/:id", async (c) => {
return c.json({ ...cp, connectors });
});
/** PATCH /api/charge-points/:id — update feePerKwh */
/** PATCH /api/charge-points/:id — update charge point fields */
app.patch("/:id", async (c) => {
const db = useDrizzle();
const id = c.req.param("id");
const body = await c.req.json<{ feePerKwh?: number }>();
const body = await c.req.json<{
feePerKwh?: number;
registrationStatus?: string;
chargePointVendor?: string;
chargePointModel?: string;
}>();
if (
typeof body.feePerKwh !== "number" ||
body.feePerKwh < 0 ||
!Number.isInteger(body.feePerKwh)
) {
return c.json({ error: "feePerKwh must be a non-negative integer (unit: fen/kWh)" }, 400);
const set: {
feePerKwh?: number;
registrationStatus?: "Accepted" | "Pending" | "Rejected";
chargePointVendor?: string;
chargePointModel?: string;
updatedAt: Date;
} = { updatedAt: new Date() };
if (body.feePerKwh !== undefined) {
if (!Number.isInteger(body.feePerKwh) || body.feePerKwh < 0) {
return c.json({ error: "feePerKwh must be a non-negative integer (unit: fen/kWh)" }, 400);
}
set.feePerKwh = body.feePerKwh;
}
if (body.registrationStatus !== undefined) {
if (!["Accepted", "Pending", "Rejected"].includes(body.registrationStatus)) {
return c.json({ error: "invalid registrationStatus" }, 400);
}
set.registrationStatus = body.registrationStatus as "Accepted" | "Pending" | "Rejected";
}
if (body.chargePointVendor !== undefined) set.chargePointVendor = body.chargePointVendor.trim() || "Unknown";
if (body.chargePointModel !== undefined) set.chargePointModel = body.chargePointModel.trim() || "Unknown";
const [updated] = await db
.update(chargePoint)
.set({ feePerKwh: body.feePerKwh, updatedAt: new Date() })
.set(set)
.where(eq(chargePoint.id, id))
.returning();
if (!updated) return c.json({ error: "Not found" }, 404);
return c.json({ feePerKwh: updated.feePerKwh });
const connectors = await db.select().from(connector).where(eq(connector.chargePointId, id));
return c.json({ ...updated, connectors });
});
/** DELETE /api/charge-points/:id — delete a charge point (cascades to connectors, transactions, meter values) */