From 5611dcccfddc47eb6f605cb2f4caae0d8db2a6c6 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 26 Apr 2025 16:44:40 -0600 Subject: [PATCH] refactor(mount): enhance updateMount function with transaction handling and improved error management --- apps/dokploy/server/api/routers/mount.ts | 3 +- packages/server/src/services/mount.ts | 41 +++++++++++++----------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/dokploy/server/api/routers/mount.ts b/apps/dokploy/server/api/routers/mount.ts index 0cfb0c07..9a189f61 100644 --- a/apps/dokploy/server/api/routers/mount.ts +++ b/apps/dokploy/server/api/routers/mount.ts @@ -31,7 +31,6 @@ export const mountRouter = createTRPCRouter({ update: protectedProcedure .input(apiUpdateMount) .mutation(async ({ input }) => { - await updateMount(input.mountId, input); - return true; + return await updateMount(input.mountId, input); }), }); diff --git a/packages/server/src/services/mount.ts b/packages/server/src/services/mount.ts index 1fa4db1e..aca0db05 100644 --- a/packages/server/src/services/mount.ts +++ b/packages/server/src/services/mount.ts @@ -123,27 +123,30 @@ export const updateMount = async ( mountId: string, mountData: Partial, ) => { - const mount = await db - .update(mounts) - .set({ - ...mountData, - }) - .where(eq(mounts.mountId, mountId)) - .returning() - .then((value) => value[0]); + return await db.transaction(async (tx) => { + const mount = await tx + .update(mounts) + .set({ + ...mountData, + }) + .where(eq(mounts.mountId, mountId)) + .returning() + .then((value) => value[0]); - if (!mount) { - throw new TRPCError({ - code: "NOT_FOUND", - message: "Mount not found", - }); - } + if (!mount) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Mount not found", + }); + } - if (mount.type === "file") { - await deleteFileMount(mountId); - await createFileMount(mountId); - } - return mount; + if (mount.type === "file") { + await deleteFileMount(mountId); + await createFileMount(mountId); + } + + return await findMountById(mountId); + }); }; export const findMountsByApplicationId = async (