feat: ship self-hosted KOC LOOP workflows
This commit is contained in:
@@ -12,6 +12,16 @@ export type CreateDistributionTaskInput = {
|
||||
dueAt: string;
|
||||
};
|
||||
|
||||
export type CreateScreenshotTaskInput = {
|
||||
name: string;
|
||||
brand: string;
|
||||
dueAt: string;
|
||||
keyword: string;
|
||||
instructions: string;
|
||||
quantity: number;
|
||||
exampleImageKey?: string;
|
||||
};
|
||||
|
||||
export type DistributionTaskCreation = {
|
||||
created: boolean;
|
||||
taskId: string;
|
||||
@@ -25,6 +35,16 @@ export type DistributionTaskCreation = {
|
||||
sourceUrl: string;
|
||||
};
|
||||
|
||||
export type ScreenshotTaskCreation = {
|
||||
created: true;
|
||||
taskId: string;
|
||||
shareToken: string;
|
||||
name: string;
|
||||
brand: string;
|
||||
dueAt: string;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
type TaskRow = {
|
||||
id: string;
|
||||
share_token: string | null;
|
||||
@@ -213,6 +233,94 @@ export async function createDistributionTask(
|
||||
};
|
||||
}
|
||||
|
||||
export async function createScreenshotTask(
|
||||
rawInput: CreateScreenshotTaskInput,
|
||||
): Promise<ScreenshotTaskCreation> {
|
||||
await ensureSchema();
|
||||
const input = {
|
||||
name: normalizedValue(rawInput.name),
|
||||
brand: normalizedValue(rawInput.brand),
|
||||
dueAt: normalizedDueDate(rawInput.dueAt),
|
||||
keyword: normalizedValue(rawInput.keyword),
|
||||
instructions: normalizedValue(rawInput.instructions),
|
||||
quantity: Math.floor(Number(rawInput.quantity)),
|
||||
exampleImageKey: normalizedValue(rawInput.exampleImageKey ?? ""),
|
||||
};
|
||||
if (!input.name || !input.brand || !input.keyword || !input.instructions) {
|
||||
throw new Error("请补全任务名称、品牌/项目、搜索关键词和任务说明");
|
||||
}
|
||||
if (!Number.isInteger(input.quantity) || input.quantity < 1 || input.quantity > 500) {
|
||||
throw new Error("任务数量需为 1—500 份");
|
||||
}
|
||||
if (input.exampleImageKey && !input.exampleImageKey.startsWith("task-assets/")) {
|
||||
throw new Error("示例截图无效,请重新上传");
|
||||
}
|
||||
|
||||
const db = getRawDb();
|
||||
const taskId = uid("task");
|
||||
const shareToken = crypto.randomUUID().replaceAll("-", "");
|
||||
const imageAssets = input.exampleImageKey
|
||||
? JSON.stringify([
|
||||
{ index: 1, key: input.exampleImageKey, width: null, height: null },
|
||||
])
|
||||
: "[]";
|
||||
await db
|
||||
.prepare(
|
||||
`INSERT INTO tasks
|
||||
(id, name, brand, quantity, claimed_quantity, due_at, status,
|
||||
task_type, source_url, source_sheet_id, source_sheet_name,
|
||||
share_token, collection_days)
|
||||
VALUES (?, ?, ?, ?, 0, ?, 'active', 'screenshot_collect', '', '', '', ?, '[]')`,
|
||||
)
|
||||
.bind(
|
||||
taskId,
|
||||
input.name,
|
||||
input.brand,
|
||||
input.quantity,
|
||||
input.dueAt,
|
||||
shareToken,
|
||||
)
|
||||
.run();
|
||||
|
||||
try {
|
||||
const statements = Array.from({ length: input.quantity }, (_, index) =>
|
||||
db
|
||||
.prepare(
|
||||
`INSERT INTO contents
|
||||
(id, task_id, title, body, image_assets, status, source, source_row)
|
||||
VALUES (?, ?, ?, ?, ?, 'available', '截图回收任务', ?)`,
|
||||
)
|
||||
.bind(
|
||||
uid("content"),
|
||||
taskId,
|
||||
input.keyword,
|
||||
input.instructions,
|
||||
imageAssets,
|
||||
index + 1,
|
||||
),
|
||||
);
|
||||
for (let index = 0; index < statements.length; index += 100) {
|
||||
await db.batch(statements.slice(index, index + 100));
|
||||
}
|
||||
} catch (error) {
|
||||
await db.batch([
|
||||
db.prepare("DELETE FROM contents WHERE task_id = ?").bind(taskId),
|
||||
db.prepare("DELETE FROM tasks WHERE id = ?").bind(taskId),
|
||||
]);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
created: true,
|
||||
taskId,
|
||||
shareToken,
|
||||
name: input.name,
|
||||
brand: input.brand,
|
||||
dueAt: input.dueAt,
|
||||
quantity: input.quantity,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildClaimUrl(portalOrigin: string, shareToken: string) {
|
||||
const origin = normalizedValue(portalOrigin).replace(/\/$/, "");
|
||||
if (!origin) throw new Error("KOC 领取站点地址尚未配置");
|
||||
|
||||
Reference in New Issue
Block a user