From c9ffb9980826a0b3d16b0d50e209df4dad35dc1a Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Wed, 28 Jan 2026 23:32:04 -0600 Subject: [PATCH] Refactor license key deactivation process: update API to retrieve the current user's license key and improve error handling for user validation and missing license keys. --- .../proprietary/license-keys/license-key.tsx | 2 +- .../api/routers/proprietary/license-key.ts | 45 +++++++++++-------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/apps/dokploy/components/proprietary/license-keys/license-key.tsx b/apps/dokploy/components/proprietary/license-keys/license-key.tsx index c5bf8cdff..d96a30670 100644 --- a/apps/dokploy/components/proprietary/license-keys/license-key.tsx +++ b/apps/dokploy/components/proprietary/license-keys/license-key.tsx @@ -105,7 +105,7 @@ export function LicenseKeySettings() { description="Are you sure you want to deactivate this license key? This will disable enterprise features." onClick={async () => { try { - await deactivateLicenseKey({ licenseKey }); + await deactivateLicenseKey(); await utils.licenseKey.getEnterpriseSettings.invalidate(); setIsValid(false); toast.success("License key deactivated"); diff --git a/apps/dokploy/server/api/routers/proprietary/license-key.ts b/apps/dokploy/server/api/routers/proprietary/license-key.ts index ba4f514ef..bea9f18f3 100644 --- a/apps/dokploy/server/api/routers/proprietary/license-key.ts +++ b/apps/dokploy/server/api/routers/proprietary/license-key.ts @@ -79,28 +79,35 @@ export const licenseKeyRouter = createTRPCRouter({ }); } }), - deactivate: adminProcedure - .input(z.object({ licenseKey: z.string() })) - .mutation(async ({ input }) => { - try { - const isValidLicenseKey = await validateLicenseKey(input.licenseKey); - if (!isValidLicenseKey) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "License key is invalid", - }); - } - return await deactivateLicenseKey(input.licenseKey); - } catch (error) { + deactivate: adminProcedure.mutation(async ({ ctx }) => { + try { + const currentUserId = ctx.user.id; + const currentUser = await db.query.user.findFirst({ + where: eq(user.id, currentUserId), + }); + if (!currentUser) { throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - error instanceof Error - ? error.message - : "Failed to deactivate license key", + code: "NOT_FOUND", + message: "User not found", }); } - }), + if (!currentUser.licenseKey) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "No license key found", + }); + } + return await deactivateLicenseKey(currentUser.licenseKey); + } catch (error) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + error instanceof Error + ? error.message + : "Failed to deactivate license key", + }); + } + }), getEnterpriseSettings: adminProcedure.query(async ({ ctx }) => { const currentUserId = ctx.user.id; const currentUser = await db.query.user.findFirst({