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 });
|
||
|
|
}
|
||
|
|
}
|