From ff2755e218728e7331ed925129d0e78f33e13b94 Mon Sep 17 00:00:00 2001 From: admin123 Date: Wed, 22 Apr 2026 13:50:33 +0800 Subject: [PATCH] feat: add batch payload builder --- src/content/market/batch-payload.ts | 53 ++++++++++++++++++++++++++++ tests/batch-payload.test.ts | 54 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/content/market/batch-payload.ts create mode 100644 tests/batch-payload.test.ts diff --git a/src/content/market/batch-payload.ts b/src/content/market/batch-payload.ts new file mode 100644 index 0000000..ee7974c --- /dev/null +++ b/src/content/market/batch-payload.ts @@ -0,0 +1,53 @@ +import type { AuthStateValue } from "../../shared/auth-messages"; +import type { MarketRecord } from "./types"; + +export interface BatchPayload { + authors: Array<{ + authorId: string; + authorName: string; + }>; + batchId: string; + batchName: string; + createdAt: string; + creatorName: string; + logtoUserId: string; + resource: string; +} + +export function createBatchPayload(options: { + authState: AuthStateValue; + batchName: string; + createdAt: string; + records: MarketRecord[]; +}): BatchPayload { + const logtoUserId = options.authState.userInfo?.sub?.trim(); + if (!logtoUserId) { + throw new Error("batch submit user id unavailable"); + } + + const resource = options.authState.resource?.trim(); + if (!resource) { + throw new Error("batch submit resource unavailable"); + } + + const batchName = options.batchName.trim(); + if (!batchName) { + throw new Error("batch submit batch name is required"); + } + + return { + authors: options.records.map((record) => ({ + authorId: record.authorId, + authorName: record.authorName + })), + batchId: `${batchName}-${options.createdAt}`, + batchName, + createdAt: options.createdAt, + creatorName: + options.authState.userInfo?.name ?? + options.authState.userInfo?.username ?? + logtoUserId, + logtoUserId, + resource + }; +} diff --git a/tests/batch-payload.test.ts b/tests/batch-payload.test.ts new file mode 100644 index 0000000..5cf5870 --- /dev/null +++ b/tests/batch-payload.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, test } from "vitest"; + +import { createBatchPayload } from "../src/content/market/batch-payload"; + +describe("batch-payload", () => { + test("builds a batch id from the batch name and timestamp", () => { + const payload = createBatchPayload({ + authState: { + isAuthenticated: true, + resource: "https://talent-search.intelligrow.cn", + userInfo: { + name: "王少卿", + sub: "p7pdhhtde8kj" + } + }, + batchName: "618达人筛选第一批", + createdAt: "2026-04-22T12:30:00.000Z", + records: [ + { authorId: "111", authorName: "达人A", status: "success" }, + { authorId: "222", authorName: "达人B", status: "success" } + ] + }); + + expect(payload).toEqual({ + authors: [ + { authorId: "111", authorName: "达人A" }, + { authorId: "222", authorName: "达人B" } + ], + batchId: "618达人筛选第一批-2026-04-22T12:30:00.000Z", + batchName: "618达人筛选第一批", + createdAt: "2026-04-22T12:30:00.000Z", + creatorName: "王少卿", + logtoUserId: "p7pdhhtde8kj", + resource: "https://talent-search.intelligrow.cn" + }); + }); + + test("throws when the user id is unavailable", () => { + expect(() => + createBatchPayload({ + authState: { + isAuthenticated: true, + resource: "https://talent-search.intelligrow.cn", + userInfo: { + name: "王少卿" + } + }, + batchName: "批次A", + createdAt: "2026-04-22T12:30:00.000Z", + records: [{ authorId: "111", authorName: "达人A", status: "success" }] + }) + ).toThrow(/user/i); + }); +});