mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: migrate endpoints
This commit is contained in:
parent
b6c29ccf05
commit
55abac3f2f
@ -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()) {
|
||||
|
@ -10,10 +10,10 @@ import {
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { nanoid } from "nanoid";
|
||||
import { z } from "zod";
|
||||
import { account } from "./account";
|
||||
import { admins } from "./admin";
|
||||
import { auth } from "./auth";
|
||||
import { certificateType } from "./shared";
|
||||
import { account } from "./account";
|
||||
/**
|
||||
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
|
||||
* database instance for multiple projects.
|
||||
|
@ -40,7 +40,7 @@ import { createTraefikConfig } from "@dokploy/server/utils/traefik/application";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { encodeBase64 } from "../utils/docker/utils";
|
||||
import { findAdminById, getDokployUrl } from "./admin";
|
||||
import { findAdminById, findUserById, getDokployUrl } from "./admin";
|
||||
import {
|
||||
createDeployment,
|
||||
createDeploymentPreview,
|
||||
@ -185,7 +185,7 @@ export const deployApplication = async ({
|
||||
});
|
||||
|
||||
try {
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheApplications) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
@ -220,7 +220,7 @@ export const deployApplication = async ({
|
||||
applicationName: application.name,
|
||||
applicationType: "application",
|
||||
buildLink,
|
||||
adminId: application.project.adminId,
|
||||
userId: application.project.userId,
|
||||
domains: application.domains,
|
||||
});
|
||||
} catch (error) {
|
||||
@ -233,7 +233,7 @@ export const deployApplication = async ({
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error building",
|
||||
buildLink,
|
||||
adminId: application.project.adminId,
|
||||
userId: application.project.userId,
|
||||
});
|
||||
|
||||
throw error;
|
||||
@ -260,7 +260,7 @@ export const rebuildApplication = async ({
|
||||
});
|
||||
|
||||
try {
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheApplications) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
@ -309,7 +309,7 @@ export const deployRemoteApplication = async ({
|
||||
|
||||
try {
|
||||
if (application.serverId) {
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheApplications) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
@ -352,7 +352,7 @@ export const deployRemoteApplication = async ({
|
||||
applicationName: application.name,
|
||||
applicationType: "application",
|
||||
buildLink,
|
||||
adminId: application.project.adminId,
|
||||
userId: application.project.userId,
|
||||
domains: application.domains,
|
||||
});
|
||||
} catch (error) {
|
||||
@ -376,7 +376,7 @@ export const deployRemoteApplication = async ({
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error building",
|
||||
buildLink,
|
||||
adminId: application.project.adminId,
|
||||
userId: application.project.userId,
|
||||
});
|
||||
|
||||
throw error;
|
||||
@ -454,7 +454,7 @@ export const deployPreviewApplication = async ({
|
||||
application.env = `${application.previewEnv}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain}`;
|
||||
application.buildArgs = application.previewBuildArgs;
|
||||
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheOnPreviews) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
@ -568,7 +568,7 @@ export const deployRemotePreviewApplication = async ({
|
||||
application.buildArgs = application.previewBuildArgs;
|
||||
|
||||
if (application.serverId) {
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheOnPreviews) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
@ -637,7 +637,7 @@ export const rebuildRemoteApplication = async ({
|
||||
|
||||
try {
|
||||
if (application.serverId) {
|
||||
const admin = await findAdminById(application.project.adminId);
|
||||
const admin = await findUserById(application.project.userId);
|
||||
|
||||
if (admin.cleanupCacheApplications) {
|
||||
await cleanupFullDocker(application?.serverId);
|
||||
|
@ -33,13 +33,13 @@ export const findCertificateById = async (certificateId: string) => {
|
||||
|
||||
export const createCertificate = async (
|
||||
certificateData: z.infer<typeof apiCreateCertificate>,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
) => {
|
||||
const certificate = await db
|
||||
.insert(certificates)
|
||||
.values({
|
||||
...certificateData,
|
||||
adminId: adminId,
|
||||
userId: userId,
|
||||
})
|
||||
.returning();
|
||||
|
||||
|
@ -44,7 +44,7 @@ import {
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { encodeBase64 } from "../utils/docker/utils";
|
||||
import { findAdminById, getDokployUrl } from "./admin";
|
||||
import { findAdminById, findUserById, getDokployUrl } from "./admin";
|
||||
import { createDeploymentCompose, updateDeploymentStatus } from "./deployment";
|
||||
import { validUniqueServerAppName } from "./project";
|
||||
import { cleanupFullDocker } from "./settings";
|
||||
@ -217,7 +217,7 @@ export const deployCompose = async ({
|
||||
});
|
||||
|
||||
try {
|
||||
const admin = await findAdminById(compose.project.adminId);
|
||||
const admin = await findUserById(compose.project.userId);
|
||||
if (admin.cleanupCacheOnCompose) {
|
||||
await cleanupFullDocker(compose?.serverId);
|
||||
}
|
||||
@ -247,7 +247,7 @@ export const deployCompose = async ({
|
||||
applicationName: compose.name,
|
||||
applicationType: "compose",
|
||||
buildLink,
|
||||
adminId: compose.project.adminId,
|
||||
userId: compose.project.userId,
|
||||
domains: compose.domains,
|
||||
});
|
||||
} catch (error) {
|
||||
@ -262,7 +262,7 @@ export const deployCompose = async ({
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error building",
|
||||
buildLink,
|
||||
adminId: compose.project.adminId,
|
||||
userId: compose.project.userId,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@ -286,7 +286,7 @@ export const rebuildCompose = async ({
|
||||
});
|
||||
|
||||
try {
|
||||
const admin = await findAdminById(compose.project.adminId);
|
||||
const admin = await findUserById(compose.project.userId);
|
||||
if (admin.cleanupCacheOnCompose) {
|
||||
await cleanupFullDocker(compose?.serverId);
|
||||
}
|
||||
@ -332,7 +332,7 @@ export const deployRemoteCompose = async ({
|
||||
});
|
||||
try {
|
||||
if (compose.serverId) {
|
||||
const admin = await findAdminById(compose.project.adminId);
|
||||
const admin = await findUserById(compose.project.userId);
|
||||
if (admin.cleanupCacheOnCompose) {
|
||||
await cleanupFullDocker(compose?.serverId);
|
||||
}
|
||||
@ -381,7 +381,7 @@ export const deployRemoteCompose = async ({
|
||||
applicationName: compose.name,
|
||||
applicationType: "compose",
|
||||
buildLink,
|
||||
adminId: compose.project.adminId,
|
||||
userId: compose.project.userId,
|
||||
domains: compose.domains,
|
||||
});
|
||||
} catch (error) {
|
||||
@ -406,7 +406,7 @@ export const deployRemoteCompose = async ({
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error building",
|
||||
buildLink,
|
||||
adminId: compose.project.adminId,
|
||||
userId: compose.project.userId,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@ -430,7 +430,7 @@ export const rebuildRemoteCompose = async ({
|
||||
});
|
||||
|
||||
try {
|
||||
const admin = await findAdminById(compose.project.adminId);
|
||||
const admin = await findUserById(compose.project.userId);
|
||||
if (admin.cleanupCacheOnCompose) {
|
||||
await cleanupFullDocker(compose?.serverId);
|
||||
}
|
||||
|
@ -10,13 +10,13 @@ export type Destination = typeof destinations.$inferSelect;
|
||||
|
||||
export const createDestintation = async (
|
||||
input: typeof apiCreateDestination._type,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
) => {
|
||||
const newDestination = await db
|
||||
.insert(destinations)
|
||||
.values({
|
||||
...input,
|
||||
adminId: adminId,
|
||||
userId: userId,
|
||||
})
|
||||
.returning()
|
||||
.then((value) => value[0]);
|
||||
@ -46,14 +46,14 @@ export const findDestinationById = async (destinationId: string) => {
|
||||
|
||||
export const removeDestinationById = async (
|
||||
destinationId: string,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
) => {
|
||||
const result = await db
|
||||
.delete(destinations)
|
||||
.where(
|
||||
and(
|
||||
eq(destinations.destinationId, destinationId),
|
||||
eq(destinations.adminId, adminId),
|
||||
eq(destinations.userId, userId),
|
||||
),
|
||||
)
|
||||
.returning();
|
||||
@ -73,7 +73,7 @@ export const updateDestinationById = async (
|
||||
.where(
|
||||
and(
|
||||
eq(destinations.destinationId, destinationId),
|
||||
eq(destinations.adminId, destinationData.adminId || ""),
|
||||
eq(destinations.userId, destinationData.userId || ""),
|
||||
),
|
||||
)
|
||||
.returning();
|
||||
|
@ -4,7 +4,7 @@ import { manageDomain } from "@dokploy/server/utils/traefik/domain";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { type apiCreateDomain, domains } from "../db/schema";
|
||||
import { findAdmin, findAdminById } from "./admin";
|
||||
import { findAdmin, findAdminById, findUserById } from "./admin";
|
||||
import { findApplicationById } from "./application";
|
||||
import { findServerById } from "./server";
|
||||
|
||||
@ -40,7 +40,7 @@ export const createDomain = async (input: typeof apiCreateDomain._type) => {
|
||||
|
||||
export const generateTraefikMeDomain = async (
|
||||
appName: string,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
serverId?: string,
|
||||
) => {
|
||||
if (serverId) {
|
||||
@ -57,7 +57,7 @@ export const generateTraefikMeDomain = async (
|
||||
projectName: appName,
|
||||
});
|
||||
}
|
||||
const admin = await findAdminById(adminId);
|
||||
const admin = await findUserById(userId);
|
||||
return generateRandomDomain({
|
||||
serverIp: admin?.serverIp || "",
|
||||
projectName: appName,
|
||||
|
@ -13,14 +13,14 @@ export type Gitlab = typeof gitlab.$inferSelect;
|
||||
|
||||
export const createGitlab = async (
|
||||
input: typeof apiCreateGitlab._type,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
) => {
|
||||
return await db.transaction(async (tx) => {
|
||||
const newGitProvider = await tx
|
||||
.insert(gitProvider)
|
||||
.values({
|
||||
providerType: "gitlab",
|
||||
adminId: adminId,
|
||||
userId: userId,
|
||||
name: input.name,
|
||||
})
|
||||
.returning()
|
||||
|
@ -13,7 +13,7 @@ import { removeDirectoryCode } from "../utils/filesystem/directory";
|
||||
import { authGithub } from "../utils/providers/github";
|
||||
import { removeTraefikConfig } from "../utils/traefik/application";
|
||||
import { manageDomain } from "../utils/traefik/domain";
|
||||
import { findAdminById } from "./admin";
|
||||
import { findAdminById, findUserById } from "./admin";
|
||||
import { findApplicationById } from "./application";
|
||||
import {
|
||||
removeDeployments,
|
||||
@ -158,7 +158,7 @@ export const createPreviewDeployment = async (
|
||||
application.previewWildcard || "*.traefik.me",
|
||||
appName,
|
||||
application.server?.ipAddress || "",
|
||||
application.project.adminId,
|
||||
application.project.userId,
|
||||
);
|
||||
|
||||
const octokit = authGithub(application?.github as Github);
|
||||
@ -250,7 +250,7 @@ const generateWildcardDomain = async (
|
||||
baseDomain: string,
|
||||
appName: string,
|
||||
serverIp: string,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
): Promise<string> => {
|
||||
if (!baseDomain.startsWith("*.")) {
|
||||
throw new Error('The base domain must start with "*."');
|
||||
@ -268,7 +268,7 @@ const generateWildcardDomain = async (
|
||||
}
|
||||
|
||||
if (!ip) {
|
||||
const admin = await findAdminById(adminId);
|
||||
const admin = await findUserById(userId);
|
||||
ip = admin?.serverIp || "";
|
||||
}
|
||||
|
||||
|
@ -16,13 +16,13 @@ export type Project = typeof projects.$inferSelect;
|
||||
|
||||
export const createProject = async (
|
||||
input: typeof apiCreateProject._type,
|
||||
adminId: string,
|
||||
userId: string,
|
||||
) => {
|
||||
const newProject = await db
|
||||
.insert(projects)
|
||||
.values({
|
||||
...input,
|
||||
adminId: adminId,
|
||||
userId: userId,
|
||||
})
|
||||
.returning()
|
||||
.then((value) => value[0]);
|
||||
|
@ -49,7 +49,7 @@ export const runMariadbBackup = async (
|
||||
projectName: project.name,
|
||||
databaseType: "mariadb",
|
||||
type: "success",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@ -60,7 +60,7 @@ export const runMariadbBackup = async (
|
||||
type: "error",
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error message not provided",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
|
||||
projectName: project.name,
|
||||
databaseType: "mongodb",
|
||||
type: "success",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@ -57,7 +57,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
|
||||
type: "error",
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error message not provided",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
|
||||
projectName: project.name,
|
||||
databaseType: "mysql",
|
||||
type: "success",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@ -57,7 +57,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
|
||||
type: "error",
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error message not provided",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ export const runPostgresBackup = async (
|
||||
projectName: project.name,
|
||||
databaseType: "postgres",
|
||||
type: "success",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
} catch (error) {
|
||||
await sendDatabaseBackupNotifications({
|
||||
@ -59,7 +59,7 @@ export const runPostgresBackup = async (
|
||||
type: "error",
|
||||
// @ts-ignore
|
||||
errorMessage: error?.message || "Error message not provided",
|
||||
adminId: project.adminId,
|
||||
userId: project.userId,
|
||||
});
|
||||
|
||||
throw error;
|
||||
|
@ -18,7 +18,7 @@ interface Props {
|
||||
applicationType: string;
|
||||
errorMessage: string;
|
||||
buildLink: string;
|
||||
adminId: string;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
export const sendBuildErrorNotifications = async ({
|
||||
@ -27,14 +27,14 @@ export const sendBuildErrorNotifications = async ({
|
||||
applicationType,
|
||||
errorMessage,
|
||||
buildLink,
|
||||
adminId,
|
||||
userId,
|
||||
}: Props) => {
|
||||
const date = new Date();
|
||||
const unixDate = ~~(Number(date) / 1000);
|
||||
const notificationList = await db.query.notifications.findMany({
|
||||
where: and(
|
||||
eq(notifications.appBuildError, true),
|
||||
eq(notifications.adminId, adminId),
|
||||
eq(notifications.userId, userId),
|
||||
),
|
||||
with: {
|
||||
email: true,
|
||||
|
@ -18,7 +18,7 @@ interface Props {
|
||||
applicationName: string;
|
||||
applicationType: string;
|
||||
buildLink: string;
|
||||
adminId: string;
|
||||
userId: string;
|
||||
domains: Domain[];
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ export const sendBuildSuccessNotifications = async ({
|
||||
applicationName,
|
||||
applicationType,
|
||||
buildLink,
|
||||
adminId,
|
||||
userId,
|
||||
domains,
|
||||
}: Props) => {
|
||||
const date = new Date();
|
||||
@ -35,7 +35,7 @@ export const sendBuildSuccessNotifications = async ({
|
||||
const notificationList = await db.query.notifications.findMany({
|
||||
where: and(
|
||||
eq(notifications.appDeploy, true),
|
||||
eq(notifications.adminId, adminId),
|
||||
eq(notifications.userId, userId),
|
||||
),
|
||||
with: {
|
||||
email: true,
|
||||
|
@ -19,13 +19,13 @@ export const sendDatabaseBackupNotifications = async ({
|
||||
databaseType,
|
||||
type,
|
||||
errorMessage,
|
||||
adminId,
|
||||
userId,
|
||||
}: {
|
||||
projectName: string;
|
||||
applicationName: string;
|
||||
databaseType: "postgres" | "mysql" | "mongodb" | "mariadb";
|
||||
type: "error" | "success";
|
||||
adminId: string;
|
||||
userId: string;
|
||||
errorMessage?: string;
|
||||
}) => {
|
||||
const date = new Date();
|
||||
@ -33,7 +33,7 @@ export const sendDatabaseBackupNotifications = async ({
|
||||
const notificationList = await db.query.notifications.findMany({
|
||||
where: and(
|
||||
eq(notifications.databaseBackup, true),
|
||||
eq(notifications.adminId, adminId),
|
||||
eq(notifications.userId, userId),
|
||||
),
|
||||
with: {
|
||||
email: true,
|
||||
|
@ -13,7 +13,7 @@ import {
|
||||
} from "./utils";
|
||||
|
||||
export const sendDockerCleanupNotifications = async (
|
||||
adminId: string,
|
||||
userId: string,
|
||||
message = "Docker cleanup for dokploy",
|
||||
) => {
|
||||
const date = new Date();
|
||||
@ -21,7 +21,7 @@ export const sendDockerCleanupNotifications = async (
|
||||
const notificationList = await db.query.notifications.findMany({
|
||||
where: and(
|
||||
eq(notifications.dockerCleanup, true),
|
||||
eq(notifications.adminId, adminId),
|
||||
eq(notifications.userId, userId),
|
||||
),
|
||||
with: {
|
||||
email: true,
|
||||
|
@ -18,7 +18,7 @@ interface ServerThresholdPayload {
|
||||
}
|
||||
|
||||
export const sendServerThresholdNotifications = async (
|
||||
adminId: string,
|
||||
userId: string,
|
||||
payload: ServerThresholdPayload,
|
||||
) => {
|
||||
const date = new Date(payload.Timestamp);
|
||||
@ -27,7 +27,7 @@ export const sendServerThresholdNotifications = async (
|
||||
const notificationList = await db.query.notifications.findMany({
|
||||
where: and(
|
||||
eq(notifications.serverThreshold, true),
|
||||
eq(notifications.adminId, adminId),
|
||||
eq(notifications.userId, userId),
|
||||
),
|
||||
with: {
|
||||
email: true,
|
||||
|
Loading…
Reference in New Issue
Block a user