Enhance License Key Management and Enterprise Features: Update license key validation logic to ensure proper handling of enterprise licenses, including new cron job for refreshing license validity. Introduce new SQL migration for isValidEnterpriseLicense column and refactor related API procedures for better error handling and user feedback.

This commit is contained in:
Mauricio Siu
2026-01-29 22:37:10 -06:00
parent 9a8de9ae16
commit 12a87f9f8b
11 changed files with 7395 additions and 51 deletions

View File

@@ -0,0 +1,70 @@
import { member } from "@dokploy/server/db/schema";
import { getPublicIpWithFallback } from "@dokploy/server/wss/utils";
import { eq } from "drizzle-orm";
import { scheduleJob } from "node-schedule";
import { db } from "../../db/index";
import { user as userSchema } from "../../db/schema/user";
export const initEnterpriseBackupCronJobs = async () => {
console.log("Setting up enterprise backup cron jobs....");
const admins = await db.query.member.findMany({
where: eq(member.role, "owner"),
with: {
user: true,
},
});
for (const admin of admins) {
const { user } = admin;
if (user.isValidEnterpriseLicense) {
scheduleJob(`enterprise-backup-${user.id}`, "0 0 */14 * *", async () => {
try {
console.log(
"Validating license key....",
user.firstName,
user.lastName,
);
const isValid = await validateLicenseKey(user.licenseKey || "");
if (!isValid) {
throw new Error("License key is invalid");
}
} catch (error) {
await db
.update(userSchema)
.set({ isValidEnterpriseLicense: false })
.where(eq(userSchema.id, user.id));
}
});
}
}
};
export const validateLicenseKey = async (licenseKey: string) => {
try {
const ip = await getPublicIpWithFallback();
const result = await fetch(
`${process.env.LICENSE_KEY_URL || "http://localhost:4002"}/licenses/validate`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ licenseKey, ip }),
},
);
if (!result.ok) {
const errorData = await result.json().catch(() => ({}));
throw new Error(errorData.message || "Failed to validate license key");
}
const data = await result.json();
return data.valid;
} catch (error) {
console.error(
error instanceof Error ? error.message : "Failed to validate license key",
);
throw error;
}
};