Files
buzz/app/(ui)/donate/admin/api.ts
T
dmgnr 4559f4693c
Build and Push Image / build-backend (push) Successful in 2m13s
Build and Push Image / build-frontend (push) Successful in 4m9s
Build and Push Image / push (push) Successful in 4m21s
refactor(host): migrate from docker swarm to kubernetes
2026-06-19 18:18:17 +00:00

92 lines
2.3 KiB
TypeScript

"use server";
import { eq } from "drizzle-orm";
import { adminCheck } from "@/lib/auth";
import { db } from "@/lib/db";
import { donations, settings } from "@/lib/db/schema";
import { sse } from "@/lib/db/sse-endpoints";
import { getPostHogClient } from "@/lib/posthog-server";
import { fileToDataUrl } from "@/lib/utils";
export async function resetGoal() {
if (!(await adminCheck())) throw new Error("Unauthorized");
getPostHogClient().capture({
distinctId: "admin",
event: "donation_admin_goal_reset",
});
await db
.insert(settings)
.values({ donateGoalStarting: new Date() })
.onConflictDoUpdate({
target: settings.id,
set: { donateGoalStarting: new Date() },
});
sse.donate.pub("update", null);
}
export async function testPopup() {
if (!(await adminCheck())) throw new Error("Unauthorized");
getPostHogClient().capture({
distinctId: "admin",
event: "donation_admin_test_popup",
});
sse.donate.pub("ping", {
id: "test",
name: "Mr. Buzz",
message: "นี่คือข้อความทดสอบโดเนท",
amount: 67,
});
}
export async function reloadWidget() {
if (!(await adminCheck())) throw new Error("Unauthorized");
getPostHogClient().capture({
distinctId: "admin",
event: "donation_admin_widget_reload",
});
sse.donate.pub("refresh", null);
}
export async function resendPopup(id: string) {
if (!(await adminCheck())) throw new Error("Unauthorized");
const [sub] = await db
.update(donations)
.set({ sent: false })
.where(eq(donations.id, id))
.returning();
if (!sub) throw new Error("not found");
getPostHogClient().capture({
distinctId: "admin",
event: "donation_admin_resend",
properties: { id },
});
sse.donate.pub("ping", {
...sub,
message: sub.message ?? "",
image: sub.image
? await fileToDataUrl(new File([Buffer.from(sub.image)], "abc.jpeg"))
: undefined,
});
}
export async function getImage(id: string) {
if (!(await adminCheck())) throw new Error("Unauthorized");
const [sub] = await db
.select({ image: donations.image })
.from(donations)
.where(eq(donations.id, id));
if (!sub) throw new Error("not found");
if (!sub.image) throw new Error("no image");
return new Blob([Buffer.from(sub.image)], { type: "image/jpeg" });
}