Files
sarlinkportal-webui/actions/payment.ts
T
i701 c06c4fee3f Implement parental control features and enhance device management
- Added a new Parental Control page for managing device access and notifications.
- Introduced blockDevice function to handle blocking and unblocking devices based on payment status.
- Enhanced omada-actions.ts to include device blocking logic and improved error handling.
- Updated DevicesTable component to integrate BlockDeviceButton for managing device states.
- Implemented API route for checking device statuses and sending notifications for expiring devices.
- Refactored payment processing to update device statuses upon successful payment verification.
- Added new utility functions for API key validation and SMS notifications.

These changes improve user control over device management and enhance the overall functionality of the application.
2024-12-22 21:34:57 +05:00

108 lines
2.1 KiB
TypeScript

"use server";
import prisma from "@/lib/db";
import type { PaymentType } from "@/lib/types";
import { formatMacAddress } from "@/lib/utils";
import { revalidatePath } from "next/cache";
import { addDevicesToGroup } from "./omada-actions";
export async function createPayment(data: PaymentType) {
console.log("data", data);
const payment = await prisma.payment.create({
data: {
amount: data.amount,
numberOfMonths: data.numberOfMonths,
paid: data.paid,
userId: data.userId,
devices: {
connect: data.deviceIds.map((id) => {
return {
id,
};
}),
},
},
});
revalidatePath("/devices");
return payment;
}
type VerifyPaymentType = {
paymentId?: string;
benefName: string;
accountNo?: string;
absAmount: string;
time: string;
};
export async function verifyPayment(data: VerifyPaymentType) {
console.log({ data });
try {
const payment = await prisma.payment.findUnique({
where: {
id: data.paymentId,
},
include: {
devices: true,
},
});
const response = await fetch(
"https://verifypaymentsapi.baraveli.dev/verify-payment",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
},
);
const json = await response.json();
console.log(json);
const newDevices = payment?.devices.map((d) => {
return {
name: d.name,
macAddress: formatMacAddress(d.mac),
};
});
if (json.success === true) {
await Promise.all([
prisma.payment.update({
where: {
id: payment?.id,
},
data: {
paid: true,
paidAt: new Date(),
devices: {
updateMany: {
where: {
paymentId: payment?.id,
},
data: {
isActive: true,
},
},
},
},
}),
]);
}
const res = await addDevicesToGroup({
groupId: process.env.OMADA_GROUP_ID,
siteId: process.env.OMADA_SITE_ID,
newDevices: newDevices || [],
});
revalidatePath("/payment[paymentId]");
console.log(res);
return res;
} catch (error) {
console.error(error);
}
}
export async function addDevicesToOmada() {
console.log("hi");
}