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.
chajia/workers/rpa/ms-checkout-job-handler.ts

74 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* MotherShip fill-only 结账 job:选卡 + 保障 + Details,不支付
*/
import { getCustomerProviderLogin } from "@/modules/customer/provider-credentials";
import { runWithMothershipLoginContext } from "@/lib/rpa/mothership-login-context";
import { cargoToQuoteRequest } from "@/modules/rpa/quote-mapper";
import { RpaError } from "@/modules/rpa/errors";
import { saveMsCheckoutStatus } from "@/lib/mothership/ms-checkout-store";
import type { MsCheckoutFillJobData } from "@/modules/mothership/checkout-queue";
import { runMothershipLoggedInCheckoutToAgree } from "@/workers/rpa/mothership-logged-in-quote";
export async function processMsCheckoutFillJob(
data: MsCheckoutFillJobData,
): Promise<void> {
const customerLogin = await getCustomerProviderLogin(
data.customerId,
"mothership",
);
const email =
customerLogin?.email?.trim() || process.env.MOTHERSHIP_EMAIL?.trim() || "";
const password =
customerLogin?.password?.trim() ||
process.env.MOTHERSHIP_PASSWORD?.trim() ||
"";
try {
const req = cargoToQuoteRequest(data.cargo);
req.preferredCarrier = data.preferredCarrier;
req.coverage = data.coverage;
req.cargoValueUsd = data.cargoValueUsd;
const result = await runWithMothershipLoginContext(
{ email, password: password || "x" },
customerLogin ? "customer" : "env",
() => runMothershipLoggedInCheckoutToAgree(req, { fillOnly: true }),
);
await saveMsCheckoutStatus({
quote_id: data.quoteId,
status: "done",
stage: result.stage,
selected_carrier: result.selectedCarrier,
selected_total: result.selectedTotal,
coverage: data.coverage,
cargo_value_usd: data.cargoValueUsd ?? null,
screenshot_path: result.screenshotPath ?? null,
message: "已在官网填齐详情(未支付)",
updated_at_ms: Date.now(),
});
console.log(
`[ms-checkout] ok quote_id=${data.quoteId} stage=${result.stage} carrier=${result.selectedCarrier}` +
(result.screenshotPath ? ` shot=${result.screenshotPath}` : ""),
);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
const code = err instanceof RpaError ? err.code : "RPA_DATA_INVALID";
console.error(`[ms-checkout] FAIL quote_id=${data.quoteId} ${msg}`);
// 已是中文业务原因时回传客户;否则统一兜底(避免堆栈/英文直出)
const userMessage =
/[\u4e00-\u9fff]/.test(msg) && !/at\s+\w+|Error:|Timeout \d+ms/i.test(msg)
? msg.replace(/\s*shot=\S+/g, "").slice(0, 180)
: "官网同步失败,请稍后重试";
await saveMsCheckoutStatus({
quote_id: data.quoteId,
status: "failed",
coverage: data.coverage,
cargo_value_usd: data.cargoValueUsd ?? null,
message: userMessage,
updated_at_ms: Date.now(),
});
console.warn(`[ms-checkout] 已记录失败 code=${code}`);
}
}