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.

This commit is contained in:
Mauricio Siu
2026-01-28 23:32:04 -06:00
parent cbfa690a80
commit c9ffb99808
2 changed files with 27 additions and 20 deletions
@@ -105,7 +105,7 @@ export function LicenseKeySettings() {
description="Are you sure you want to deactivate this license key? This will disable enterprise features." description="Are you sure you want to deactivate this license key? This will disable enterprise features."
onClick={async () => { onClick={async () => {
try { try {
await deactivateLicenseKey({ licenseKey }); await deactivateLicenseKey();
await utils.licenseKey.getEnterpriseSettings.invalidate(); await utils.licenseKey.getEnterpriseSettings.invalidate();
setIsValid(false); setIsValid(false);
toast.success("License key deactivated"); toast.success("License key deactivated");
@@ -79,28 +79,35 @@ export const licenseKeyRouter = createTRPCRouter({
}); });
} }
}), }),
deactivate: adminProcedure deactivate: adminProcedure.mutation(async ({ ctx }) => {
.input(z.object({ licenseKey: z.string() })) try {
.mutation(async ({ input }) => { const currentUserId = ctx.user.id;
try { const currentUser = await db.query.user.findFirst({
const isValidLicenseKey = await validateLicenseKey(input.licenseKey); where: eq(user.id, currentUserId),
if (!isValidLicenseKey) { });
throw new TRPCError({ if (!currentUser) {
code: "BAD_REQUEST",
message: "License key is invalid",
});
}
return await deactivateLicenseKey(input.licenseKey);
} catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "NOT_FOUND",
message: message: "User not found",
error instanceof Error
? error.message
: "Failed to deactivate license key",
}); });
} }
}), 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 }) => { getEnterpriseSettings: adminProcedure.query(async ({ ctx }) => {
const currentUserId = ctx.user.id; const currentUserId = ctx.user.id;
const currentUser = await db.query.user.findFirst({ const currentUser = await db.query.user.findFirst({