From e8f493558f8d3074f6dc071a072a73bc1fa86233 Mon Sep 17 00:00:00 2001 From: knuthtimo-lab Date: Sun, 18 Jan 2026 19:48:45 +0100 Subject: [PATCH] listings --- bizmatch-server/package.json | 2 +- bizmatch-server/src/user/user.service.ts | 36 +++++++++++++----------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/bizmatch-server/package.json b/bizmatch-server/package.json index f348601..ed01bf1 100644 --- a/bizmatch-server/package.json +++ b/bizmatch-server/package.json @@ -109,4 +109,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} +} \ No newline at end of file diff --git a/bizmatch-server/src/user/user.service.ts b/bizmatch-server/src/user/user.service.ts index 1c6d078..2f705c1 100644 --- a/bizmatch-server/src/user/user.service.ts +++ b/bizmatch-server/src/user/user.service.ts @@ -160,25 +160,29 @@ export class UserService { } async addFavorite(id: string, user: JwtUser): Promise { - await this.conn - .update(schema.users_json) - .set({ - data: sql`jsonb_set(${schema.users_json.data}, '{favoritesForUser}', - coalesce((${schema.users_json.data}->'favoritesForUser')::jsonb, '[]'::jsonb) || to_jsonb(${user.email}::text))`, - } as any) - .where(eq(schema.users_json.id, id)); + const existingUser = await this.getUserById(id); + if (!existingUser) return; + + const favorites = existingUser.favoritesForUser || []; + if (!favorites.includes(user.email)) { + existingUser.favoritesForUser = [...favorites, user.email]; + const { id: _, ...rest } = existingUser; + const drizzleUser = { email: existingUser.email, data: rest }; + await this.conn.update(schema.users_json).set(drizzleUser).where(eq(schema.users_json.id, id)); + } } async deleteFavorite(id: string, user: JwtUser): Promise { - await this.conn - .update(schema.users_json) - .set({ - data: sql`jsonb_set(${schema.users_json.data}, '{favoritesForUser}', - (SELECT coalesce(jsonb_agg(elem), '[]'::jsonb) - FROM jsonb_array_elements(coalesce(${schema.users_json.data}->'favoritesForUser', '[]'::jsonb)) AS elem - WHERE elem::text != to_jsonb(${user.email}::text)::text))`, - } as any) - .where(eq(schema.users_json.id, id)); + const existingUser = await this.getUserById(id); + if (!existingUser) return; + + const favorites = existingUser.favoritesForUser || []; + if (favorites.includes(user.email)) { + existingUser.favoritesForUser = favorites.filter(email => email !== user.email); + const { id: _, ...rest } = existingUser; + const drizzleUser = { email: existingUser.email, data: rest }; + await this.conn.update(schema.users_json).set(drizzleUser).where(eq(schema.users_json.id, id)); + } } async getFavoriteUsers(user: JwtUser): Promise {