refactor(mount): enhance updateMount function with transaction handling and improved error management

This commit is contained in:
Mauricio Siu 2025-04-26 16:44:40 -06:00
parent 371c6317aa
commit 5611dcccfd
2 changed files with 23 additions and 21 deletions

View File

@ -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);
}),
});

View File

@ -123,27 +123,30 @@ export const updateMount = async (
mountId: string,
mountData: Partial<Mount>,
) => {
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 (