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/flock-checkout-job-handler.ts

78 lines
2.8 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.

/**
* Flock fill-only 结账 job:选档 + 填齐详情,不支付
*/
import { getCustomerProviderLogin } from "@/modules/customer/provider-credentials";
import { runWithFlockLoginContext } from "@/lib/flock/login-context";
import { RpaError } from "@/modules/rpa/errors";
import { saveFlockCheckoutStatus } from "@/lib/flock/flock-checkout-store";
import type { FlockCheckoutFillJobData } from "@/modules/flock/checkout-queue";
import { runFlockLoggedInCheckoutFill } from "@/workers/rpa/flock/checkout";
export async function processFlockCheckoutFillJob(
data: FlockCheckoutFillJobData,
): Promise<void> {
const customerLogin = await getCustomerProviderLogin(
data.customerId,
"flock",
);
const email =
customerLogin?.email?.trim() || process.env.FLOCK_LOGIN_EMAIL?.trim() || "";
const password =
customerLogin?.password?.trim() ||
process.env.FLOCK_LOGIN_PASSWORD?.trim() ||
"";
try {
const result = await runWithFlockLoginContext(
email && password ? { email, password } : null,
customerLogin ? "customer" : "env",
() =>
runFlockLoggedInCheckoutFill(data.input, {
preferredTier: data.preferredTier,
preferredFlexibility: data.preferredFlexibility,
preferredCarrier: data.preferredCarrier,
fillOnly: true,
reference: data.reference,
flockDetails: data.flockDetails,
quoteSessionId: data.quoteSessionId,
}),
);
await saveFlockCheckoutStatus({
quote_id: data.quoteId,
status: "done",
stage: result.stage,
preferred_tier: data.preferredTier,
selected_total: result.selectedTotal,
screenshot_path: result.screenshotPath ?? null,
message: "已在官网填齐详情(未支付)",
updated_at_ms: Date.now(),
});
if (data.quoteSessionId) {
const { clearFlockQuoteHold, updateFlockQuoteHoldStatus } = await import(
"@/lib/flock/flock-quote-hold-store"
);
await updateFlockQuoteHoldStatus(data.quoteSessionId, "done").catch(
() => undefined,
);
await clearFlockQuoteHold(data.quoteSessionId).catch(() => undefined);
}
console.log(
`[flock-checkout] ok quote_id=${data.quoteId} stage=${result.stage} tier=${data.preferredTier}` +
(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(`[flock-checkout] FAIL quote_id=${data.quoteId} ${msg}`);
await saveFlockCheckoutStatus({
quote_id: data.quoteId,
status: "failed",
preferred_tier: data.preferredTier,
message: "官网同步失败,请稍后重试",
updated_at_ms: Date.now(),
});
console.warn(`[flock-checkout] 已记录失败 code=${code}`);
}
}