From f12ba788a4af571f1b4a586ff1de0eea330e8a4f Mon Sep 17 00:00:00 2001 From: Dhravya Shah Date: Wed, 29 Jan 2025 15:44:27 -0700 Subject: [PATCH] fix: deleting memories --- apps/backend/src/routes/memories.ts | 50 ++++++++++++++++------------- apps/backend/wrangler.toml | 2 +- apps/web/wrangler.toml | 2 +- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/apps/backend/src/routes/memories.ts b/apps/backend/src/routes/memories.ts index 5ab811eb..bc2894e6 100644 --- a/apps/backend/src/routes/memories.ts +++ b/apps/backend/src/routes/memories.ts @@ -8,8 +8,7 @@ import { spaceAccess, contentToSpace, } from "@supermemory/db/schema"; -import { and, database, desc, eq, sql } from "@supermemory/db"; - +import { and, database, desc, eq, or, sql } from "@supermemory/db"; const memories = new Hono<{ Variables: Variables; Bindings: Env }>() .get( @@ -148,26 +147,22 @@ const memories = new Hono<{ Variables: Variables; Bindings: Env }>() }); } ) - .get( - "/:id", - zValidator("param", z.object({ id: z.string() })), - async (c) => { - const { id } = c.req.valid("param"); - const user = c.get("user"); + .get("/:id", zValidator("param", z.object({ id: z.string() })), async (c) => { + const { id } = c.req.valid("param"); + const user = c.get("user"); - if (!user) { - return c.json({ error: "Unauthorized" }, 401); - } - - const memory = await database(c.env.HYPERDRIVE.connectionString) - .select() - .from(documents) - .where(and(eq(documents.uuid, id), eq(documents.userId, user.id))) - .limit(1); - - return c.json(memory[0]); + if (!user) { + return c.json({ error: "Unauthorized" }, 401); } - ) + + const memory = await database(c.env.HYPERDRIVE.connectionString) + .select() + .from(documents) + .where(and(eq(documents.uuid, id), eq(documents.userId, user.id))) + .limit(1); + + return c.json(memory[0]); + }) .delete( "/:id", zValidator("param", z.object({ id: z.string() })), @@ -184,7 +179,12 @@ const memories = new Hono<{ Variables: Variables; Bindings: Env }>() const doc = await db .select() .from(documents) - .where(and(eq(documents.uuid, id), eq(documents.userId, user.id))) + .where( + and( + or(eq(documents.uuid, id), eq(documents.id, Number(id))), + eq(documents.userId, user.id) + ) + ) .limit(1); if (!doc[0]) { @@ -192,8 +192,12 @@ const memories = new Hono<{ Variables: Variables; Bindings: Env }>() } const [document, contentToSpacei] = await Promise.all([ - db.delete(documents).where(and(eq(documents.uuid, id), eq(documents.userId, user.id))), - db.delete(contentToSpace).where(eq(contentToSpace.contentId, doc[0].id)), + db + .delete(documents) + .where(and(eq(documents.uuid, id), eq(documents.userId, user.id))), + db + .delete(contentToSpace) + .where(eq(contentToSpace.contentId, doc[0].id)), ]); return c.json({ success: true }); diff --git a/apps/backend/wrangler.toml b/apps/backend/wrangler.toml index 0aa36a1f..36832053 100644 --- a/apps/backend/wrangler.toml +++ b/apps/backend/wrangler.toml @@ -36,7 +36,7 @@ preview_id = "a1f048ee14644468ad63b817b5648a31" [[hyperdrive]] binding = "HYPERDRIVE" id = "3a377d1b9c084e698ee201f10dfa8131" -localConnectionString = "postgres://postgres:postgres@localhost:5432/supermemorydhravya?sslmode=require" +localConnectionString = "postgres://postgres:postgres@localhost:5432/supermemorylocal?sslmode=require" [[unsafe.bindings]] name = "EMAIL_LIMITER" diff --git a/apps/web/wrangler.toml b/apps/web/wrangler.toml index 401e9b33..1ad14d06 100644 --- a/apps/web/wrangler.toml +++ b/apps/web/wrangler.toml @@ -21,7 +21,7 @@ bucket_name = "supermemory-images" [[hyperdrive]] binding = "HYPERDRIVE" id = "3a377d1b9c084e698ee201f10dfa8131" -localConnectionString = "postgres://postgres:postgres@localhost:5432/supermemorydhravya?sslmode=require" +localConnectionString = "postgres://postgres:postgres@localhost:5432/supermemorylocal?sslmode=require" [placement] mode = "smart"