53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
"use server";
|
|
|
|
import { updateTag } from "next/cache";
|
|
import * as z from "zod";
|
|
|
|
import { requireAdmin } from "@/lib/auth/server";
|
|
import { auditActor, writeAuditLogBestEffort } from "@/lib/audit-log";
|
|
import { STYGIAN_CACHE_TAG } from "@/lib/cache/stygian";
|
|
import {
|
|
saveStygianRecommendations,
|
|
syncStygianArchive,
|
|
} from "@/lib/stygian/repository";
|
|
|
|
function message(cause: unknown) {
|
|
return cause instanceof z.ZodError
|
|
? cause.issues.map((issue) => issue.message).join(" • ")
|
|
: cause instanceof Error ? cause.message : "ดำเนินการไม่สำเร็จ";
|
|
}
|
|
|
|
export async function syncStygianArchiveAction() {
|
|
const admin = await requireAdmin();
|
|
try {
|
|
const result = await syncStygianArchive();
|
|
await writeAuditLogBestEffort(auditActor(admin.user), {
|
|
action: "stygian.synced",
|
|
targetType: "stygian_schedule",
|
|
targetId: String(result.imported.at(-1) ?? result.stoppedAt),
|
|
metadata: { imported: result.imported, stoppedAt: result.stoppedAt },
|
|
});
|
|
updateTag(STYGIAN_CACHE_TAG);
|
|
return { status: "saved" as const, ...result };
|
|
} catch (cause) {
|
|
return { status: "error" as const, message: message(cause) };
|
|
}
|
|
}
|
|
|
|
export async function saveStygianRecommendationsAction(scheduleId: number, input: unknown) {
|
|
const admin = await requireAdmin();
|
|
try {
|
|
const result = await saveStygianRecommendations(scheduleId, input);
|
|
await writeAuditLogBestEffort(auditActor(admin.user), {
|
|
action: "stygian.recommendations.saved",
|
|
targetType: "stygian_schedule",
|
|
targetId: String(scheduleId),
|
|
resultingVersion: result.version,
|
|
});
|
|
updateTag(STYGIAN_CACHE_TAG);
|
|
return { status: "saved" as const, version: result.version };
|
|
} catch (cause) {
|
|
return { status: "error" as const, message: message(cause) };
|
|
}
|
|
}
|