feat: add switchable SlipOK and Slip2Go verification
Build and Push Image / build-backend (push) Successful in 2m27s
Build and Push Image / build-frontend (push) Successful in 4m4s
Build and Push Image / push (push) Successful in 1m1s

This commit is contained in:
2026-10-15 02:17:35 +07:00 Unverified
parent c3022f4d37
commit beb0cfaf3d
12 changed files with 792 additions and 68 deletions
+38 -8
View File
@@ -1,22 +1,52 @@
"use server";
import { revalidatePath } from "next/cache";
import { actionLog } from "@/lib/api";
import { db } from "@/lib/db";
import { settings } from "@/lib/db/schema";
import { adminCheck } from "@/lib/auth";
import { sse } from "@/lib/db/sse-endpoints";
import { syncAmber as syncAmberRaw } from "@/util/sync";
import type { ToastType } from "@/components/fake-toast";
import { actionLog } from "@/lib/api";
import { adminCheck } from "@/lib/auth";
import { db, primaryDb } from "@/lib/db";
import { settings } from "@/lib/db/schema";
import { sse } from "@/lib/db/sse-endpoints";
import { getSlipConfiguration, isSlipProvider } from "@/lib/slip-checker";
import { syncAmber as syncAmberRaw } from "@/util/sync";
export async function getSettongs() {
if (!(await adminCheck())) throw "Unauthorized";
const [
settangs = { enka: true, donatePromptpay: true, donateTruemoney: true },
] = await db.select().from(settings).limit(1);
settangs = {
enka: true,
donatePromptpay: true,
donateTruemoney: true,
slipCheckProvider: "slipok" as const,
},
] = await primaryDb.select().from(settings).limit(1);
return settangs;
}
export async function setSlipCheckProvider(provider: string) {
if (!(await adminCheck())) throw "Unauthorized";
if (!isSlipProvider(provider))
return { success: false as const, message: "บริการตรวจสอบสลิปไม่ถูกต้อง" };
try {
getSlipConfiguration(provider);
} catch (error) {
return {
success: false as const,
message: error instanceof Error ? error.message : "การตั้งค่าไม่ถูกต้อง",
};
}
await primaryDb
.insert(settings)
.values({ slipCheckProvider: provider })
.onConflictDoUpdate({
target: settings.id,
set: { slipCheckProvider: provider },
});
await actionLog("Changed slip verification provider", { provider });
revalidatePath("/admin/settings");
return { success: true as const, provider };
}
export async function toggleEnka(state: boolean) {
if (!(await adminCheck())) throw "Unauthorized";
await db
+3 -1
View File
@@ -12,7 +12,8 @@ import { getSettongs } from "./api";
import { SettingsServicesSection } from "./services";
export default async function () {
const { enka, donatePromptpay, donateTruemoney } = await getSettongs();
const { enka, donatePromptpay, donateTruemoney, slipCheckProvider } =
await getSettongs();
return (
<div className="grid h-full w-full gap-8 p-2 pl-0">
@@ -26,6 +27,7 @@ export default async function () {
enka={enka}
donPp={donatePromptpay}
donTmn={donateTruemoney}
slipProvider={slipCheckProvider}
/>
</TabsContent>
<TabsContent value="account">
+51
View File
@@ -2,13 +2,18 @@
import { Bomb, FolderSync } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
import { ActionButton } from "@/components/action-button";
import { CdnChooser } from "@/components/chooser";
import { Field, FieldDescription, FieldTitle } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { shared } from "@/lib/comms";
import type { SlipProvider } from "@/lib/slip-checker";
import {
forceRefresh,
setSlipCheckProvider,
syncAmber,
toggleDonatePaymentMethod,
toggleEnka,
@@ -20,10 +25,12 @@ export function SettingsServicesSection({
enka: enkaInitial,
donPp: initialDonPp,
donTmn: initialDonTmn,
slipProvider: initialSlipProvider,
}: {
enka: boolean;
donPp: boolean;
donTmn: boolean;
slipProvider: SlipProvider;
}) {
const [syncResult, setSyncResult] = useState("");
const [enka, setEnka] = useState<boolean>(enkaInitial);
@@ -31,6 +38,31 @@ export function SettingsServicesSection({
const [donTmn, setDonTmn] = useState<boolean>(initialDonTmn);
const [pathToReload, setPathToReload] = useState("");
const [debug] = shared.state("debug");
const [slipProvider, setSlipProvider] = useState(initialSlipProvider);
const [savingSlipProvider, setSavingSlipProvider] = useState(false);
async function changeSlipProvider(value: string) {
if (
(value !== "slipok" && value !== "slip2go") ||
value === slipProvider ||
savingSlipProvider
)
return;
setSavingSlipProvider(true);
try {
const result = await setSlipCheckProvider(value);
if (result.success) {
setSlipProvider(result.provider);
toast.success("เปลี่ยนบริการตรวจสอบสลิปแล้ว");
} else {
toast.error(result.message);
}
} catch {
toast.error("ไม่สามารถเปลี่ยนบริการตรวจสอบสลิปได้ กรุณาลองใหม่");
} finally {
setSavingSlipProvider(false);
}
}
async function sync() {
return syncAmber().then(setSyncResult);
@@ -73,6 +105,25 @@ export function SettingsServicesSection({
Reload
</ActionButton>
</div>
<Field data-disabled={savingSlipProvider}>
<FieldTitle id="slip-provider-label">บริการตรวจสอบสลิป</FieldTitle>
<ToggleGroup
type="single"
variant="outline"
value={slipProvider}
onValueChange={changeSlipProvider}
disabled={savingSlipProvider}
aria-labelledby="slip-provider-label"
aria-busy={savingSlipProvider}
className="w-fit"
>
<ToggleGroupItem value="slipok">SlipOK</ToggleGroupItem>
<ToggleGroupItem value="slip2go">Slip2Go</ToggleGroupItem>
</ToggleGroup>
<FieldDescription>
ใช้สำหรับรับกรรมและโดเนท มีผลกับการตรวจสอบสลิปครั้งถัดไป
</FieldDescription>
</Field>
<span>Donate Payment Method</span>
<div className="flex items-center gap-2">
<Switch
+33 -24
View File
@@ -3,39 +3,53 @@ import { env } from "bun";
import { sql } from "drizzle-orm";
import { db } from "@/lib/db";
import { redis as redisShared } from "@/lib/db/redis";
import { getSlipProvider } from "@/lib/payment";
import { getSlipConfiguration } from "@/lib/slip-checker";
const { DISCORD_BOT_TOKEN, GEMINI_TTS_API_KEY } = env as Record<string, string>;
const redis = redisShared!;
export const revalidate = 60;
export const dynamic = "force-dynamic";
export async function GET() {
const slipProvider = await getSlipProvider().catch(() => null);
const healthChecks = {
...checks,
async slip() {
if (!slipProvider) return false;
const { url } = getSlipConfiguration(slipProvider);
// API origins may return 404/401 without a route or credentials; they are reachable.
return fetch(new URL(url).origin, {
signal: AbortSignal.timeout(5000),
}).then((response) => response.status < 500);
},
};
const red = await redis
.ping()
.then(() => true)
.catch(() => false);
const res = Object.fromEntries(
await Promise.all(
Object.entries(checks).map(
async ([k, v]) =>
[
k,
red
? (await redis.exists(`health:${k}`))
? (await redis.get(`health:${k}`)) === "ok"
: await v()
.then(async (r) => {
const v = r instanceof Response ? r.ok : !!r;
if (v) await redis.setex(`health:${k}`, 900, "ok");
return v;
})
.catch(() => false)
Object.entries(healthChecks).map(async ([k, v]) => {
const cacheKey = `health:${k === "slip" ? `slip:${slipProvider}` : k}`;
return [
k,
red
? (await redis.exists(cacheKey))
? (await redis.get(cacheKey)) === "ok"
: await v()
.then((r) => (r instanceof Response ? r.ok : !!r))
.catch(() => false),
] as const,
),
.then(async (r) => {
const v = r instanceof Response ? r.ok : !!r;
if (v) await redis.setex(cacheKey, 900, "ok");
return v;
})
.catch(() => false)
: await v()
.then((r) => (r instanceof Response ? r.ok : !!r))
.catch(() => false),
] as const;
}),
),
);
return Response.json(
@@ -73,11 +87,6 @@ const checks = {
signal: AbortSignal.timeout(5000),
});
},
async slip() {
return fetch("https://api.slipok.com/", {
signal: AbortSignal.timeout(5000),
});
},
async tts() {
const client = new GoogleGenAI({
apiKey: GEMINI_TTS_API_KEY.split(",").pop(),