import { prisma } from "@/lib/db"; import type { AutomationTaskType } from "./types"; export async function logAction(input: { accountId?: string | null; taskType: AutomationTaskType; action: string; mode?: "auto" | "manual"; status?: "success" | "failed" | "skipped"; targetId?: string | null; externalId?: string | null; permalink?: string | null; detail?: string | null; error?: string | null; }) { return prisma.actionLog.create({ data: { accountId: input.accountId ?? null, taskType: input.taskType, action: input.action, mode: input.mode ?? "auto", status: input.status ?? "success", targetId: input.targetId ?? null, externalId: input.externalId ?? null, permalink: input.permalink ?? null, detail: input.detail ?? null, error: input.error ?? null, }, }); } function startOfToday(): Date { const d = new Date(); d.setHours(0, 0, 0, 0); return d; } /** 計算某帳號某任務「今天已成功送出的對外動作」數量,用於每日配額限制。 */ export async function countTodaySuccess( accountId: string, taskType: AutomationTaskType ): Promise { return prisma.actionLog.count({ where: { accountId, taskType, status: "success", mode: "auto", createdAt: { gte: startOfToday() }, }, }); }