- 任务中心新增「发布到企微群」:同步客户群清单(wecom_group_chats)、 按群主分组创建企业群发任务(add_msg_template)、发送记录落库 wecom_group_pushes,迁移 mysql/0009;lib/wecom-client.ts 补 listCustomerGroupChats/createGroupMsgTemplate - KOC 资源库支持单条新增:app/api/resources-insert + lib/resource-write, 拆出资源写入公共逻辑供导入复用;0008 补合作方外部联系人字段 - 环境变量示例补企微凭证与 SEED_DEMO_DATA;next.config 增加 allowedDevOrigins;CLAUDE.md 补充项目说明 - .gitignore 排除 .codegraph/ 与 .ipynb_checkpoints/
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
import { isManagerRequest, managerForbidden } from "../../../lib/user-auth";
|
|
import { buildManualRow, type ManualResourceInput } from "../../../lib/resource-import";
|
|
import {
|
|
analyzeRows,
|
|
enrichRows,
|
|
loadAccounts,
|
|
writeAnalyzedRows,
|
|
} from "../../../lib/resource-write";
|
|
|
|
type InsertResult = {
|
|
action: "create" | "update";
|
|
message: string;
|
|
};
|
|
|
|
export async function POST(request: Request) {
|
|
if (!(await isManagerRequest(request))) return managerForbidden();
|
|
try {
|
|
const payload = (await request.json()) as Partial<ManualResourceInput> & {
|
|
mode?: string;
|
|
};
|
|
const row = buildManualRow({
|
|
profileUrl: payload.profileUrl ?? "",
|
|
nickname: payload.nickname ?? "",
|
|
publicAccountId: payload.publicAccountId ?? "",
|
|
ipLocation: payload.ipLocation ?? "",
|
|
followers: payload.followers ?? "",
|
|
gender: payload.gender ?? "",
|
|
bio: payload.bio ?? "",
|
|
tags: payload.tags ?? "",
|
|
cooperationSource: payload.cooperationSource ?? "",
|
|
});
|
|
if (row.errors.length > 0) {
|
|
return Response.json(
|
|
{ error: row.errors[0], errors: row.errors },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const accounts = await loadAccounts();
|
|
const enriched = await enrichRows([row], accounts.results);
|
|
const analyzed = analyzeRows(enriched, accounts.results).filter(
|
|
(item) => item.action !== "error",
|
|
);
|
|
if (analyzed.length === 0) {
|
|
return Response.json(
|
|
{ error: "账号数据校验未通过,无法保存" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
await writeAnalyzedRows(analyzed);
|
|
const result = analyzed[0];
|
|
const outcome: InsertResult = {
|
|
action: result.action === "update" ? "update" : "create",
|
|
message:
|
|
result.action === "update"
|
|
? `已更新账号 ${row.nickname || row.publicAccountId || "资料"}`
|
|
: `已新增账号 ${row.nickname || row.publicAccountId || "资料"}`,
|
|
};
|
|
return Response.json(outcome);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "保存失败";
|
|
return Response.json({ error: message }, { status: 400 });
|
|
}
|
|
}
|