You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.9 KiB
66 lines
1.9 KiB
import { assertCustomerMatch, parseServiceAuth } from "@/lib/api/auth-context";
|
|
import { enforceRateLimits } from "@/lib/api/rate-limit";
|
|
import { resolveMothershipCandidates } from "@/modules/address/mothership-candidates";
|
|
import { mothershipCandidatesBodySchema } from "@/modules/address/validation";
|
|
import { fail, ok } from "@/lib/response";
|
|
import { AuthError } from "@/modules/auth/errors";
|
|
import { RpaError } from "@/modules/rpa/errors";
|
|
|
|
export const maxDuration = 300;
|
|
|
|
export async function POST(request: Request) {
|
|
let auth;
|
|
try {
|
|
auth = parseServiceAuth(request);
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
return fail(error.code, error.message, error.httpStatus);
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
const rateLimited = await enforceRateLimits(request, auth.customerId);
|
|
if (rateLimited) {
|
|
return rateLimited;
|
|
}
|
|
|
|
let body: unknown;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return fail("VALIDATION_FAILED", "请求体格式无效", 400);
|
|
}
|
|
|
|
const parsed = mothershipCandidatesBodySchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
const message =
|
|
parsed.error.issues[0]?.message ?? "地址参数无效";
|
|
return fail("VALIDATION_FAILED", message, 400);
|
|
}
|
|
|
|
try {
|
|
assertCustomerMatch(auth, parsed.data.customer_id);
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
return fail(error.code, error.message, error.httpStatus);
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
try {
|
|
const result = await resolveMothershipCandidates({
|
|
pickup: parsed.data.pickup_address,
|
|
delivery: parsed.data.delivery_address,
|
|
});
|
|
return ok(result);
|
|
} catch (error) {
|
|
if (error instanceof RpaError) {
|
|
return fail(error.code, error.message, 503);
|
|
}
|
|
console.error("[mothership-candidates]", error);
|
|
const message =
|
|
error instanceof Error ? error.message : "地址联想服务异常";
|
|
return fail("INTERNAL_ERROR", message, 503);
|
|
}
|
|
}
|