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.
mail-yubao/scripts/patch-ui-customer-fallback.ts

84 lines
2.2 KiB

/**
* UI fallback: if mail_record missing customer, extract from subject bl-plus
*/
import fs from "fs";
const path = "src/components/MailBusinessSummary.tsx";
let src = fs.readFileSync(path, "utf8");
if (!src.includes("extractBlPlusFromMail")) {
const importNeedle = `import {
extractForecastInstructionText,
extractMailInstructions,
} from "@/services/parse/split-instructions";`;
const importRepl = `import { extractBlPlusFromMail } from "@/services/parse/bl-plus-template";
import {
extractForecastInstructionText,
extractMailInstructions,
} from "@/services/parse/split-instructions";`;
if (!src.includes(importNeedle)) throw new Error("import block missing");
src = src.replace(importNeedle, importRepl);
}
const oldForecast = ` const forecastValue = useMemo(() => {
const base = buildCcForecastFormValue({
customerName: record?.modules.customer_name,
header,
modules: record?.modules,
});
return {
...base,
F_Instruction: forecastInstruction || base.F_Instruction,
};
}, [
header,
record?.modules,
record?.modules.customer_name,
forecastInstruction,
]);`;
const neuForecast = ` const blPlusModules = useMemo(
() => extractBlPlusFromMail(mail.subject, bodyText)?.modules ?? null,
[mail.subject, bodyText],
);
const forecastValue = useMemo(() => {
const modules = {
...(blPlusModules || {}),
...(record?.modules || {}),
};
const base = buildCcForecastFormValue({
customerName:
record?.modules.customer_name ||
blPlusModules?.customer_name ||
undefined,
header,
modules,
});
return {
...base,
F_Instruction: forecastInstruction || base.F_Instruction,
};
}, [
header,
record?.modules,
record?.modules.customer_name,
blPlusModules,
forecastInstruction,
]);`;
if (!src.includes(oldForecast)) {
// CRLF
const oldC = oldForecast.replace(/\n/g, "\r\n");
if (!src.includes(oldC)) {
console.error("forecastValue block missing");
process.exit(1);
}
src = src.replace(oldC, neuForecast.replace(/\n/g, "\r\n"));
} else {
src = src.replace(oldForecast, neuForecast);
}
fs.writeFileSync(path, src);
console.log("MailBusinessSummary customer fallback ok");