|
|
/**
|
|
|
* Flock:选档后 scrape 三档灵活性价或承运商列表(不点 Yes)
|
|
|
*/
|
|
|
import { getCustomerProviderLogin } from "@/modules/customer/provider-credentials";
|
|
|
import { runWithFlockLoginContext } from "@/lib/flock/login-context";
|
|
|
import { prisma } from "@/lib/prisma";
|
|
|
import { RpaError } from "@/modules/rpa/errors";
|
|
|
import { saveFlockPricingOptions } from "@/lib/flock/flock-pricing-options-store";
|
|
|
import type { FlockPricingOptionsJobData } from "@/modules/flock/pricing-options-queue";
|
|
|
import { getMarkupRule } from "@/modules/pricing/engine";
|
|
|
import {
|
|
|
applyMarkupToFlockCarrierOptions,
|
|
|
applyMarkupToFlockFlexibilityOptions,
|
|
|
} from "@/modules/flock/quote-storage";
|
|
|
import { runFlockScrapePricingOptions } from "@/workers/rpa/flock/checkout";
|
|
|
|
|
|
export async function processFlockPricingOptionsJob(
|
|
|
data: FlockPricingOptionsJobData,
|
|
|
): 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",
|
|
|
() =>
|
|
|
runFlockScrapePricingOptions(data.input, {
|
|
|
preferredTier: data.preferredTier,
|
|
|
reference: data.reference,
|
|
|
quoteSessionId: data.quoteSessionId,
|
|
|
}),
|
|
|
);
|
|
|
|
|
|
const quoteRecord = await prisma.quoteRecord.findUnique({
|
|
|
where: { quoteId: data.quoteId },
|
|
|
select: { businessCustomerId: true },
|
|
|
});
|
|
|
const markupRule = await getMarkupRule(
|
|
|
data.customerId,
|
|
|
quoteRecord?.businessCustomerId,
|
|
|
);
|
|
|
// 为何:侧栏/宿主展示的是档内 rateUsd,须与一级档位一样叠加业务客户加价
|
|
|
const markedOptions = applyMarkupToFlockFlexibilityOptions(
|
|
|
result.options,
|
|
|
markupRule,
|
|
|
);
|
|
|
const markedCarriers = applyMarkupToFlockCarrierOptions(
|
|
|
result.carrier_options ?? [],
|
|
|
markupRule,
|
|
|
);
|
|
|
|
|
|
const optionsType = result.options_type ?? "flexibility";
|
|
|
const carrierN = markedCarriers.length;
|
|
|
const flexN = markedOptions.length;
|
|
|
await saveFlockPricingOptions({
|
|
|
quote_id: data.quoteId,
|
|
|
status: "done",
|
|
|
preferred_tier: data.preferredTier,
|
|
|
options: markedOptions,
|
|
|
options_type: optionsType,
|
|
|
carrier_options: markedCarriers,
|
|
|
reference: result.reference,
|
|
|
message:
|
|
|
optionsType === "carriers"
|
|
|
? `已拉取 ${carrierN} 个承运商报价`
|
|
|
: `已拉取 ${flexN} 档灵活性报价`,
|
|
|
updated_at_ms: Date.now(),
|
|
|
});
|
|
|
console.log(
|
|
|
`[flock-pricing-options] ok quote_id=${data.quoteId} tier=${data.preferredTier} type=${optionsType} n=${optionsType === "carriers" ? carrierN : flexN}`,
|
|
|
);
|
|
|
} catch (err) {
|
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
|
const code = err instanceof RpaError ? err.code : "RPA_DATA_INVALID";
|
|
|
console.error(`[flock-pricing-options] FAIL quote_id=${data.quoteId} ${msg}`);
|
|
|
await saveFlockPricingOptions({
|
|
|
quote_id: data.quoteId,
|
|
|
status: "failed",
|
|
|
preferred_tier: data.preferredTier,
|
|
|
options: [],
|
|
|
options_type: "flexibility",
|
|
|
carrier_options: [],
|
|
|
message: "拉取档内报价失败,请重试",
|
|
|
updated_at_ms: Date.now(),
|
|
|
});
|
|
|
console.warn(`[flock-pricing-options] 已记录失败 code=${code}`);
|
|
|
}
|
|
|
}
|