feat: add switchable SlipOK and Slip2Go verification
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user