refactor: migrate endpoints

This commit is contained in:
Mauricio Siu
2025-02-14 02:52:37 -06:00
parent b6c29ccf05
commit 55abac3f2f
19 changed files with 260 additions and 256 deletions

View File

@@ -1,7 +1,7 @@
import { buffer } from "node:stream/consumers";
import { db } from "@/server/db";
import { admins, server } from "@/server/db/schema";
import { findAdminById } from "@dokploy/server";
import { admins, server, users_temp } from "@/server/db/schema";
import { findAdminById, findUserById } from "@dokploy/server";
import { asc, eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
@@ -18,242 +18,246 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
// if (!endpointSecret) {
// return res.status(400).send("Webhook Error: Missing Stripe Secret Key");
// }
// const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
// apiVersion: "2024-09-30.acacia",
// maxNetworkRetries: 3,
// });
if (!endpointSecret) {
return res.status(400).send("Webhook Error: Missing Stripe Secret Key");
}
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2024-09-30.acacia",
maxNetworkRetries: 3,
});
// const buf = await buffer(req);
// const sig = req.headers["stripe-signature"] as string;
const buf = await buffer(req);
const sig = req.headers["stripe-signature"] as string;
// let event: Stripe.Event;
let event: Stripe.Event;
// try {
// event = stripe.webhooks.constructEvent(buf, sig, endpointSecret);
// } catch (err) {
// console.error(
// "Webhook signature verification failed.",
// err instanceof Error ? err.message : err,
// );
// return res.status(400).send("Webhook Error: ");
// }
try {
event = stripe.webhooks.constructEvent(buf, sig, endpointSecret);
} catch (err) {
console.error(
"Webhook signature verification failed.",
err instanceof Error ? err.message : err,
);
return res.status(400).send("Webhook Error: ");
}
// const webhooksAllowed = [
// "customer.subscription.created",
// "customer.subscription.deleted",
// "customer.subscription.updated",
// "invoice.payment_succeeded",
// "invoice.payment_failed",
// "customer.deleted",
// "checkout.session.completed",
// ];
const webhooksAllowed = [
"customer.subscription.created",
"customer.subscription.deleted",
"customer.subscription.updated",
"invoice.payment_succeeded",
"invoice.payment_failed",
"customer.deleted",
"checkout.session.completed",
];
// if (!webhooksAllowed.includes(event.type)) {
// return res.status(400).send("Webhook Error: Invalid Event Type");
// }
if (!webhooksAllowed.includes(event.type)) {
return res.status(400).send("Webhook Error: Invalid Event Type");
}
// switch (event.type) {
// case "checkout.session.completed": {
// const session = event.data.object as Stripe.Checkout.Session;
// const adminId = session?.metadata?.adminId as string;
switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object as Stripe.Checkout.Session;
const adminId = session?.metadata?.adminId as string;
// const subscription = await stripe.subscriptions.retrieve(
// session.subscription as string,
// );
// await db
// .update(admins)
// .set({
// stripeCustomerId: session.customer as string,
// stripeSubscriptionId: session.subscription as string,
// serversQuantity: subscription?.items?.data?.[0]?.quantity ?? 0,
// })
// .where(eq(admins.adminId, adminId))
// .returning();
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string,
);
await db
.update(users_temp)
.set({
stripeCustomerId: session.customer as string,
stripeSubscriptionId: session.subscription as string,
serversQuantity: subscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(eq(users_temp.id, adminId))
.returning();
// const admin = await findAdminById(adminId);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
// const newServersQuantity = admin.serversQuantity;
// await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
// break;
// }
// case "customer.subscription.created": {
// const newSubscription = event.data.object as Stripe.Subscription;
const admin = await findUserById(adminId);
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.id, newServersQuantity);
break;
}
case "customer.subscription.created": {
const newSubscription = event.data.object as Stripe.Subscription;
// await db
// .update(admins)
// .set({
// stripeSubscriptionId: newSubscription.id,
// stripeCustomerId: newSubscription.customer as string,
// })
// .where(eq(admins.stripeCustomerId, newSubscription.customer as string))
// .returning();
await db
.update(users_temp)
.set({
stripeSubscriptionId: newSubscription.id,
stripeCustomerId: newSubscription.customer as string,
})
.where(
eq(users_temp.stripeCustomerId, newSubscription.customer as string),
)
.returning();
// break;
// }
break;
}
// case "customer.subscription.deleted": {
// const newSubscription = event.data.object as Stripe.Subscription;
case "customer.subscription.deleted": {
const newSubscription = event.data.object as Stripe.Subscription;
// await db
// .update(admins)
// .set({
// stripeSubscriptionId: null,
// serversQuantity: 0,
// })
// .where(eq(admins.stripeCustomerId, newSubscription.customer as string));
await db
.update(users_temp)
.set({
stripeSubscriptionId: null,
serversQuantity: 0,
})
.where(
eq(users_temp.stripeCustomerId, newSubscription.customer as string),
);
// const admin = await findAdminByStripeCustomerId(
// newSubscription.customer as string,
// );
const admin = await findUserByStripeCustomerId(
newSubscription.customer as string,
);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// await disableServers(admin.adminId);
// break;
// }
// case "customer.subscription.updated": {
// const newSubscription = event.data.object as Stripe.Subscription;
await disableServers(admin.id);
break;
}
case "customer.subscription.updated": {
const newSubscription = event.data.object as Stripe.Subscription;
// const admin = await findAdminByStripeCustomerId(
// newSubscription.customer as string,
// );
const admin = await findUserByStripeCustomerId(
newSubscription.customer as string,
);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// if (newSubscription.status === "active") {
// await db
// .update(admins)
// .set({
// serversQuantity: newSubscription?.items?.data?.[0]?.quantity ?? 0,
// })
// .where(
// eq(admins.stripeCustomerId, newSubscription.customer as string),
// );
if (newSubscription.status === "active") {
await db
.update(users_temp)
.set({
serversQuantity: newSubscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(
eq(users_temp.stripeCustomerId, newSubscription.customer as string),
);
// const newServersQuantity = admin.serversQuantity;
// await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
// } else {
// await disableServers(admin.adminId);
// await db
// .update(admins)
// .set({ serversQuantity: 0 })
// .where(
// eq(admins.stripeCustomerId, newSubscription.customer as string),
// );
// }
const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.id, newServersQuantity);
} else {
await disableServers(admin.id);
await db
.update(users_temp)
.set({ serversQuantity: 0 })
.where(
eq(users_temp.stripeCustomerId, newSubscription.customer as string),
);
}
// break;
// }
// case "invoice.payment_succeeded": {
// const newInvoice = event.data.object as Stripe.Invoice;
break;
}
case "invoice.payment_succeeded": {
const newInvoice = event.data.object as Stripe.Invoice;
// const suscription = await stripe.subscriptions.retrieve(
// newInvoice.subscription as string,
// );
const suscription = await stripe.subscriptions.retrieve(
newInvoice.subscription as string,
);
// if (suscription.status !== "active") {
// console.log(
// `Skipping invoice.payment_succeeded for subscription ${suscription.id} with status ${suscription.status}`,
// );
// break;
// }
if (suscription.status !== "active") {
console.log(
`Skipping invoice.payment_succeeded for subscription ${suscription.id} with status ${suscription.status}`,
);
break;
}
// await db
// .update(admins)
// .set({
// serversQuantity: suscription?.items?.data?.[0]?.quantity ?? 0,
// })
// .where(eq(admins.stripeCustomerId, suscription.customer as string));
await db
.update(admins)
.set({
serversQuantity: suscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(eq(admins.stripeCustomerId, suscription.customer as string));
// const admin = await findAdminByStripeCustomerId(
// suscription.customer as string,
// );
const admin = await findUserByStripeCustomerId(
suscription.customer as string,
);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
// const newServersQuantity = admin.serversQuantity;
// await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
// break;
// }
// case "invoice.payment_failed": {
// const newInvoice = event.data.object as Stripe.Invoice;
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.id, newServersQuantity);
break;
}
case "invoice.payment_failed": {
const newInvoice = event.data.object as Stripe.Invoice;
// const subscription = await stripe.subscriptions.retrieve(
// newInvoice.subscription as string,
// );
const subscription = await stripe.subscriptions.retrieve(
newInvoice.subscription as string,
);
// if (subscription.status !== "active") {
// const admin = await findAdminByStripeCustomerId(
// newInvoice.customer as string,
// );
if (subscription.status !== "active") {
const admin = await findUserByStripeCustomerId(
newInvoice.customer as string,
);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
// await db
// .update(admins)
// .set({
// serversQuantity: 0,
// })
// .where(eq(admins.stripeCustomerId, newInvoice.customer as string));
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
await db
.update(admins)
.set({
serversQuantity: 0,
})
.where(eq(admins.stripeCustomerId, newInvoice.customer as string));
// await disableServers(admin.adminId);
// }
await disableServers(admin.id);
}
// break;
// }
break;
}
// case "customer.deleted": {
// const customer = event.data.object as Stripe.Customer;
case "customer.deleted": {
const customer = event.data.object as Stripe.Customer;
// const admin = await findAdminByStripeCustomerId(customer.id);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
const admin = await findUserByStripeCustomerId(customer.id);
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// await disableServers(admin.adminId);
// await db
// .update(admins)
// .set({
// stripeCustomerId: null,
// stripeSubscriptionId: null,
// serversQuantity: 0,
// })
// .where(eq(admins.stripeCustomerId, customer.id));
await disableServers(admin.id);
await db
.update(users_temp)
.set({
stripeCustomerId: null,
stripeSubscriptionId: null,
serversQuantity: 0,
})
.where(eq(users_temp.stripeCustomerId, customer.id));
// break;
// }
// default:
// console.log(`Unhandled event type: ${event.type}`);
// }
break;
}
default:
console.log(`Unhandled event type: ${event.type}`);
}
return res.status(200).json({ received: true });
}
const disableServers = async (adminId: string) => {
const disableServers = async (userId: string) => {
await db
.update(server)
.set({
serverStatus: "inactive",
})
.where(eq(server.adminId, adminId));
.where(eq(server.userId, userId));
};
const findAdminByStripeCustomerId = async (stripeCustomerId: string) => {
const admin = db.query.admins.findFirst({
where: eq(admins.stripeCustomerId, stripeCustomerId),
const findUserByStripeCustomerId = async (stripeCustomerId: string) => {
const user = db.query.users_temp.findFirst({
where: eq(users_temp.stripeCustomerId, stripeCustomerId),
});
return admin;
return user;
};
const activateServer = async (serverId: string) => {
@@ -270,19 +274,19 @@ const deactivateServer = async (serverId: string) => {
.where(eq(server.serverId, serverId));
};
export const findServersByAdminIdSorted = async (adminId: string) => {
export const findServersByUserIdSorted = async (userId: string) => {
const servers = await db.query.server.findMany({
where: eq(server.adminId, adminId),
where: eq(server.userId, userId),
orderBy: asc(server.createdAt),
});
return servers;
};
export const updateServersBasedOnQuantity = async (
adminId: string,
userId: string,
newServersQuantity: number,
) => {
const servers = await findServersByAdminIdSorted(adminId);
const servers = await findServersByUserIdSorted(userId);
if (servers.length > newServersQuantity) {
for (const [index, server] of servers.entries()) {