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.

71 lines
2.2 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.

/**
* 二级刷价结果落库(Direct / DOM 共用)
*/
import { Prisma } from "@prisma/client";
import { QUOTE_VALIDITY_MS } from "@/lib/constants/quote";
import { overwriteL1, setL2, setL3 } from "@/modules/cache/redis-cache";
import { prisma } from "@/lib/prisma";
import { applyMarkupToQuotes, getMarkupRule } from "@/modules/pricing/engine";
import { getConfidenceScore } from "@/modules/quote/confidence";
import { prepareMotherShipStorageQuotes } from "@/modules/quote/quote-completeness";
import { quoteItemsToRawTiers } from "@/modules/rpa/quote-mapper";
import {
clearMsRefineHold,
updateMsRefineHoldStatus,
} from "@/lib/mothership/refine-hold-store";
import type { NormalizedCargo } from "@/modules/quote/types";
import type { QuoteItem } from "@/modules/providers/quote-provider";
export async function applyMsRefineQuotes(input: {
quoteId: string;
requestId: string;
customerId: string;
sessionId: string;
cargo: NormalizedCargo;
items: QuoteItem[];
}): Promise<void> {
const uiQuotes = prepareMotherShipStorageQuotes(
quoteItemsToRawTiers(input.items),
);
const markupRule = await getMarkupRule(
input.customerId,
input.cargo.businessCustomerId,
);
const markedQuotes = applyMarkupToQuotes(uiQuotes, markupRule);
const validUntil = new Date(Date.now() + QUOTE_VALIDITY_MS);
await setL2(input.cargo.cargoHash, { quotes: uiQuotes });
await setL3(input.cargo.cargoHash, { quotes: uiQuotes });
await prisma.quoteRecord.update({
where: { quoteId: input.quoteId },
data: {
status: "done",
sourceType: "rpa",
isRealtime: true,
confidenceScore: getConfidenceScore("rpa"),
markupPercent: markupRule.type === "percent" ? markupRule.percent : 0,
quotesJson: markedQuotes as unknown as Prisma.InputJsonValue,
validUntil,
errorCode: null,
errorMessage: null,
},
});
await overwriteL1(input.requestId, {
quoteId: input.quoteId,
response: {
quote_id: input.quoteId,
status: "done",
source_type: "rpa",
is_realtime: true,
},
});
await updateMsRefineHoldStatus(input.sessionId, "refined");
await clearMsRefineHold(input.sessionId);
console.log(
`[ms-refine] ok quote_id=${input.quoteId} tiers=${markedQuotes.length}`,
);
}