25 lines
792 B
TypeScript
25 lines
792 B
TypeScript
"use server";
|
|
|
|
import { headers } from "next/headers";
|
|
import { allowRequest, clientIp } from "../../lib/rate-limit";
|
|
import { lookupBikes } from "../../lib/status";
|
|
import type { StatusResult } from "../../lib/workshop";
|
|
|
|
export async function lookupStatus(_prev: StatusResult | null, formData: FormData): Promise<StatusResult> {
|
|
const headerList = await headers();
|
|
const ip = clientIp(headerList.get("x-forwarded-for"));
|
|
if (!allowRequest(ip)) {
|
|
return {
|
|
message: null,
|
|
error: "Too many lookups. Wait a minute and try again.",
|
|
bikes: [],
|
|
};
|
|
}
|
|
try {
|
|
return await lookupBikes(String(formData.get("phone") ?? ""));
|
|
} catch {
|
|
console.error("status lookup failed");
|
|
return { message: null, error: "Lookup failed. Try again.", bikes: [] };
|
|
}
|
|
}
|