From 4baf77c74082385248acf7b67da908842a720bb5 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Wed, 25 Feb 2026 23:30:28 -0600 Subject: [PATCH] feat: enhance security handling by adding refetch capability and improving error messages in security service --- .../advanced/security/handle-security.tsx | 3 +- apps/dokploy/server/api/routers/security.ts | 2 +- packages/server/src/services/security.ts | 39 ++++++++++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/apps/dokploy/components/dashboard/application/advanced/security/handle-security.tsx b/apps/dokploy/components/dashboard/application/advanced/security/handle-security.tsx index 05d2188f8..49a126881 100644 --- a/apps/dokploy/components/dashboard/application/advanced/security/handle-security.tsx +++ b/apps/dokploy/components/dashboard/application/advanced/security/handle-security.tsx @@ -46,7 +46,7 @@ export const HandleSecurity = ({ }: Props) => { const utils = api.useUtils(); const [isOpen, setIsOpen] = useState(false); - const { data } = api.security.one.useQuery( + const { data, refetch } = api.security.one.useQuery( { securityId: securityId ?? "", }, @@ -88,6 +88,7 @@ export const HandleSecurity = ({ await utils.application.readTraefikConfig.invalidate({ applicationId, }); + await refetch(); setIsOpen(false); }) .catch(() => { diff --git a/apps/dokploy/server/api/routers/security.ts b/apps/dokploy/server/api/routers/security.ts index 3d8374b4c..5dd5a6ddc 100644 --- a/apps/dokploy/server/api/routers/security.ts +++ b/apps/dokploy/server/api/routers/security.ts @@ -43,7 +43,7 @@ export const securityRouter = createTRPCRouter({ message: "You are not authorized to access this application", }); } - return await findSecurityById(input.securityId); + return security; }), delete: protectedProcedure .input(apiFindOneSecurity) diff --git a/packages/server/src/services/security.ts b/packages/server/src/services/security.ts index d6947b887..fa5fe2c62 100644 --- a/packages/server/src/services/security.ts +++ b/packages/server/src/services/security.ts @@ -50,7 +50,8 @@ export const createSecurity = async ( } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", - message: "Error creating this security", + message: + error instanceof Error ? error.message : "Error creating this security", cause: error, }); } @@ -90,15 +91,35 @@ export const updateSecurityById = async ( data: Partial, ) => { try { - const response = await db - .update(security) - .set({ - ...data, - }) - .where(eq(security.securityId, securityId)) - .returning(); + await db.transaction(async (tx) => { + const securityResponse = await findSecurityById(securityId); - return response[0]; + const application = await findApplicationById( + securityResponse.applicationId, + ); + + await removeSecurityMiddleware(application, securityResponse); + + const response = await tx + .update(security) + .set({ + ...data, + }) + .where(eq(security.securityId, securityId)) + .returning() + .then((res) => res[0]); + + if (!response) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Security not found", + }); + } + + await createSecurityMiddleware(application, response); + + return response; + }); } catch (error) { const message = error instanceof Error ? error.message : "Error updating this security";