This commit is contained in:
knuthtimo-lab 2026-01-18 19:48:45 +01:00
parent 31a507ad58
commit e8f493558f
2 changed files with 21 additions and 17 deletions

View File

@ -160,25 +160,29 @@ export class UserService {
}
async addFavorite(id: string, user: JwtUser): Promise<void> {
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<void> {
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<User[]> {