Skip to content

Commit

Permalink
fix: Delete user's assets when the user is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Apr 5, 2024
1 parent 8af95fe commit 91b088d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/shared/assetdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ export async function deleteAsset({
const assetDir = getAssetDir(userId, assetId);
await fs.promises.rm(path.join(assetDir), { recursive: true });
}

export async function deleteUserAssets({ userId }: { userId: string }) {
const userDir = path.join(ROOT_PATH, userId);
const dirExists = await fs.promises
.access(userDir)
.then(() => true)
.catch(() => false);
if (!dirExists) {
return;
}
await fs.promises.rm(userDir, { recursive: true });
}
2 changes: 2 additions & 0 deletions packages/trpc/routers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from "zod";

import { SqliteError } from "@hoarder/db";
import { users } from "@hoarder/db/schema";
import { deleteUserAssets } from "@hoarder/shared/assetdb";
import serverConfig from "@hoarder/shared/config";

import { hashPassword, validatePassword } from "../auth";
Expand Down Expand Up @@ -127,6 +128,7 @@ export const usersAppRouter = router({
if (res.changes == 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
await deleteUserAssets({ userId: input.userId });
}),
whoami: authedProcedure
.output(
Expand Down

0 comments on commit 91b088d

Please sign in to comment.