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.

68 lines
2.1 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 登录态首价 hold:继续填写 / 放弃二级
*/
import { requestReleaseParkedSession } from "@/modules/address/parked-session-queue";
import {
assertFlockQuoteHoldUsable,
clearFlockQuoteHold,
readFlockQuoteHold,
updateFlockQuoteHoldStatus,
} from "@/lib/flock/flock-quote-hold-store";
import {
FLOCK_HOLD_MSG,
isFlockQuoteHoldExpired,
shouldAutoDeclineFlockHold,
} from "@/lib/constants/flock-quote-hold";
export async function declineFlockQuoteHold(input: {
customerId: string;
quoteId: string;
sessionId: string;
}): Promise<{ released: boolean }> {
const state = await readFlockQuoteHold(input.sessionId);
if (state && state.customer_id !== input.customerId) {
throw new Error("无权操作该询价会话");
}
if (state && state.quote_id !== input.quoteId) {
throw new Error("报价单与询价会话不匹配");
}
await updateFlockQuoteHoldStatus(input.sessionId, "declined");
await requestReleaseParkedSession(input.sessionId);
await clearFlockQuoteHold(input.sessionId);
return { released: true };
}
/** 用户确认继续填二级:避免 60s 决策窗被 GET auto-decline */
export async function continueFlockQuoteHold(input: {
customerId: string;
quoteId: string;
sessionId: string;
}): Promise<{ status: "filling" }> {
const state = await assertFlockQuoteHoldUsable(input.sessionId);
if (state.customer_id !== input.customerId) {
throw new Error("无权操作该询价会话");
}
if (state.quote_id !== input.quoteId) {
throw new Error("报价单与询价会话不匹配");
}
if (isFlockQuoteHoldExpired(state)) {
await declineFlockQuoteHold(input);
throw new Error(FLOCK_HOLD_MSG.totalTimeoutApi);
}
if (
state.status === "awaiting_decision" &&
shouldAutoDeclineFlockHold(state)
) {
await declineFlockQuoteHold(input);
throw new Error(FLOCK_HOLD_MSG.decisionTimeoutApi);
}
if (
state.status === "filling" ||
state.status === "checking_out"
) {
return { status: "filling" };
}
await updateFlockQuoteHoldStatus(input.sessionId, "filling");
return { status: "filling" };
}