diff --git a/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx b/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx index e3519edf5..c2c6ec2ac 100644 --- a/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx +++ b/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx @@ -17,8 +17,7 @@ import { useState, useEffect } from "react"; export const EnablePaidFeatures = () => { const { data, refetch } = api.user.get.useQuery(); const [isLoading, setIsLoading] = useState(false); - const { mutateAsync: validateLicense } = - api.user.validateLicense.useMutation(); + const { mutateAsync: saveLicense } = api.user.saveLicense.useMutation(); const { mutateAsync: update } = api.user.update.useMutation(); const [licenseKey, setLicenseKey] = useState(""); @@ -34,14 +33,13 @@ export const EnablePaidFeatures = () => { return; } setIsLoading(true); - await validateLicense({ + await saveLicense({ licenseKey, }) .then(() => { toast.success("License validated successfully"); }) .catch((e) => { - console.error(e); toast.error("Error validating license", { description: e.message, }); diff --git a/apps/dokploy/server/api/routers/user.ts b/apps/dokploy/server/api/routers/user.ts index bc8fcae48..2676333d9 100644 --- a/apps/dokploy/server/api/routers/user.ts +++ b/apps/dokploy/server/api/routers/user.ts @@ -29,7 +29,10 @@ import { protectedProcedure, publicProcedure, } from "../trpc"; -import { validateLicense } from "@/server/utils/validate-license"; +import { + validateLicense, + activateLicense, +} from "@/server/utils/validate-license"; const apiCreateApiKey = z.object({ name: z.string().min(1), prefix: z.string().optional(), @@ -140,7 +143,7 @@ export const userRouter = createTRPCRouter({ } return await updateUser(ctx.user.id, input); }), - validateLicense: adminProcedure + saveLicense: adminProcedure .input( z.object({ licenseKey: z.string(), @@ -159,9 +162,12 @@ export const userRouter = createTRPCRouter({ }); } + await activateLicense(input.licenseKey, owner?.serverIp || ""); + await updateUser(ctx.user.id, { licenseKey: input.licenseKey, }); + return result; }), getUserByToken: publicProcedure diff --git a/apps/dokploy/server/utils/validate-license.ts b/apps/dokploy/server/utils/validate-license.ts index eb3b83075..6f7a07b72 100644 --- a/apps/dokploy/server/utils/validate-license.ts +++ b/apps/dokploy/server/utils/validate-license.ts @@ -1,7 +1,7 @@ const licensesUrl = process.env.LICENSES_URL || "http://localhost:4002"; export const validateLicense = async (licenseKey: string, serverIp: string) => { - const response = await fetch(`${licensesUrl}/api/validate`, { + const response = await fetch(`${licensesUrl}/api/license/validate`, { method: "POST", headers: { "Content-Type": "application/json", @@ -16,3 +16,16 @@ export const validateLicense = async (licenseKey: string, serverIp: string) => { return data; }; + +export const activateLicense = async (licenseKey: string, serverIp: string) => { + const response = await fetch(`${licensesUrl}/api/license/activate`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ licenseKey, serverIp }), + }); + const data = await response.json(); + + return data; +}; diff --git a/apps/licenses/src/api/license.ts b/apps/licenses/src/api/license.ts index 548a5d56b..30013de5a 100644 --- a/apps/licenses/src/api/license.ts +++ b/apps/licenses/src/api/license.ts @@ -25,7 +25,6 @@ licenseRouter.post( try { const result = await validateLicense(licenseKey, serverIp); - console.log("Result", result); return c.json(result); } catch (error) { logger.error("Error validating license:", { error }); diff --git a/apps/licenses/src/utils/license.ts b/apps/licenses/src/utils/license.ts index f154d75d0..ba97577e0 100644 --- a/apps/licenses/src/utils/license.ts +++ b/apps/licenses/src/utils/license.ts @@ -75,6 +75,16 @@ export const validateLicense = async (licenseKey: string, serverIp: string) => { const suscription = await stripe.subscriptions.retrieve( license.stripeSubscriptionId, ); + const currentServerQuantity = license.serverIps?.length || 0; + const serversQuantity = suscription.items.data[0].quantity || 0; + + if (currentServerQuantity >= serversQuantity) { + return { + isValid: false, + error: + "You have reached the maximum number of servers, please upgrade your license to add more servers", + }; + } if (suscription.status !== "active") { return { @@ -120,6 +130,8 @@ export const activateLicense = async (licenseKey: string, serverIp: string) => { ); } + console.log("License", license.serverIps?.includes(serverIp)); + if (license.serverIps && !license.serverIps.includes(serverIp)) { throw new Error("License is already activated on a different server"); }