feat(dashboard): add transactions and users management pages with CRUD functionality
feat(auth): implement login page and authentication middleware feat(sidebar): create sidebar component with user info and navigation links feat(api): establish API client for interacting with backend services
This commit is contained in:
51
apps/csms/src/ocpp/actions/authorize.ts
Normal file
51
apps/csms/src/ocpp/actions/authorize.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { useDrizzle } from "@/lib/db.js";
|
||||
import { idTag } from "@/db/schema.js";
|
||||
import type {
|
||||
AuthorizeRequest,
|
||||
AuthorizeResponse,
|
||||
IdTagInfo,
|
||||
OcppConnectionContext,
|
||||
} from "../types.ts";
|
||||
|
||||
/**
|
||||
* Shared helper — resolves idTagInfo from the database.
|
||||
* Used by Authorize, StartTransaction, and StopTransaction.
|
||||
*
|
||||
* @param checkBalance When true (default), rejects tags with balance ≤ 0.
|
||||
* Pass false for StopTransaction where charging has already occurred.
|
||||
*/
|
||||
export async function resolveIdTagInfo(
|
||||
idTagValue: string,
|
||||
checkBalance = true,
|
||||
): Promise<IdTagInfo> {
|
||||
const db = useDrizzle();
|
||||
const [tag] = await db.select().from(idTag).where(eq(idTag.idTag, idTagValue)).limit(1);
|
||||
|
||||
if (!tag) return { status: "Invalid" };
|
||||
if (tag.status === "Blocked") return { status: "Blocked" };
|
||||
if (tag.expiryDate && tag.expiryDate < new Date()) {
|
||||
return { status: "Expired", expiryDate: tag.expiryDate.toISOString() };
|
||||
}
|
||||
if (tag.status !== "Accepted") {
|
||||
return { status: tag.status as IdTagInfo["status"] };
|
||||
}
|
||||
// Reject if balance is zero or negative
|
||||
if (checkBalance && tag.balance <= 0) {
|
||||
return { status: "Blocked" };
|
||||
}
|
||||
return {
|
||||
status: "Accepted",
|
||||
expiryDate: tag.expiryDate?.toISOString(),
|
||||
parentIdTag: tag.parentIdTag ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export async function handleAuthorize(
|
||||
payload: AuthorizeRequest,
|
||||
_ctx: OcppConnectionContext,
|
||||
): Promise<AuthorizeResponse> {
|
||||
const idTagInfo = await resolveIdTagInfo(payload.idTag);
|
||||
console.log(`[OCPP] Authorize idTag=${payload.idTag} -> ${idTagInfo.status}`);
|
||||
return { idTagInfo };
|
||||
}
|
||||
Reference in New Issue
Block a user