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.
100 lines
2.6 KiB
100 lines
2.6 KiB
/**
|
|
* CC 客户留言 / 工单写入
|
|
* 管理端对齐 ccnew SRCustomerMessage/Form → POST /CustomerMessage/SaveForm
|
|
*/
|
|
import { logger } from "@/lib/logger";
|
|
import { ccAuth } from "@/services/cc/auth";
|
|
import { CcHttpClient } from "@/services/cc/http";
|
|
import { normalizeCcCode } from "@/services/cc/code";
|
|
import { resolveWriteMode } from "@/services/cc/write-capability";
|
|
import { formatCcApiFailure, formatCcCause } from "@/constants/error-copy";
|
|
import type { CustomerMessageEntity } from "@/services/cc/customer-message-entity";
|
|
import { normalizeExternalId } from "@/services/import/import-log-meta";
|
|
|
|
export type { CustomerMessageEntity } from "@/services/cc/customer-message-entity";
|
|
export { toCcCustomerMessageEntity } from "@/services/cc/customer-message-entity";
|
|
|
|
const SAVE_PATH = "/CustomerMessage/SaveForm";
|
|
const ccHttp = new CcHttpClient(ccAuth);
|
|
|
|
export type SaveCustomerMessageResult = {
|
|
ok: boolean;
|
|
mode: "live" | "mock" | "off";
|
|
externalId?: string | null;
|
|
request: unknown;
|
|
response: unknown;
|
|
error?: string;
|
|
};
|
|
|
|
export async function saveCustomerMessage(
|
|
entity: CustomerMessageEntity,
|
|
opts?: { mailId?: string },
|
|
): Promise<SaveCustomerMessageResult> {
|
|
const mode = await resolveWriteMode("customer_message");
|
|
const request = {
|
|
keyValue: "",
|
|
entity,
|
|
mail_id: opts?.mailId,
|
|
};
|
|
|
|
if (mode === "off") {
|
|
return {
|
|
ok: false,
|
|
mode,
|
|
request,
|
|
response: null,
|
|
error: formatCcCause("CC_WRITE_OFF"),
|
|
};
|
|
}
|
|
|
|
if (mode === "mock") {
|
|
const externalId = `mock-wo-${Date.now()}`;
|
|
logger.info({ mailId: opts?.mailId }, "cc.customer_message_mock");
|
|
return {
|
|
ok: true,
|
|
mode: "mock",
|
|
externalId,
|
|
request,
|
|
response: { code: 200, data: externalId, mock: true },
|
|
};
|
|
}
|
|
|
|
try {
|
|
const res = await ccHttp.request(SAVE_PATH, {
|
|
keyValue: null,
|
|
entity,
|
|
});
|
|
const code = normalizeCcCode(res.code);
|
|
const ok = code === 200;
|
|
return {
|
|
ok,
|
|
mode: "live",
|
|
externalId: ok ? normalizeExternalId(res.data) : null,
|
|
request,
|
|
response: res,
|
|
error: ok
|
|
? undefined
|
|
: formatCcApiFailure({
|
|
api: "SaveForm",
|
|
code,
|
|
info: String(
|
|
(res as { info?: string; message?: string }).info ||
|
|
(res as { message?: string }).message ||
|
|
"",
|
|
),
|
|
fallback: "CC_WO_FAILED",
|
|
}),
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
mode: "live",
|
|
request,
|
|
response: null,
|
|
error: formatCcCause(
|
|
err instanceof Error ? err.message : "CC_WO_ERROR",
|
|
),
|
|
};
|
|
}
|
|
}
|