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> { async addFavorite(id: string, user: JwtUser): Promise<void> {
await this.conn const existingUser = await this.getUserById(id);
.update(schema.users_json) if (!existingUser) return;
.set({
data: sql`jsonb_set(${schema.users_json.data}, '{favoritesForUser}', const favorites = existingUser.favoritesForUser || [];
coalesce((${schema.users_json.data}->'favoritesForUser')::jsonb, '[]'::jsonb) || to_jsonb(${user.email}::text))`, if (!favorites.includes(user.email)) {
} as any) existingUser.favoritesForUser = [...favorites, user.email];
.where(eq(schema.users_json.id, id)); 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> { async deleteFavorite(id: string, user: JwtUser): Promise<void> {
await this.conn const existingUser = await this.getUserById(id);
.update(schema.users_json) if (!existingUser) return;
.set({
data: sql`jsonb_set(${schema.users_json.data}, '{favoritesForUser}', const favorites = existingUser.favoritesForUser || [];
(SELECT coalesce(jsonb_agg(elem), '[]'::jsonb) if (favorites.includes(user.email)) {
FROM jsonb_array_elements(coalesce(${schema.users_json.data}->'favoritesForUser', '[]'::jsonb)) AS elem existingUser.favoritesForUser = favorites.filter(email => email !== user.email);
WHERE elem::text != to_jsonb(${user.email}::text)::text))`, const { id: _, ...rest } = existingUser;
} as any) const drizzleUser = { email: existingUser.email, data: rest };
.where(eq(schema.users_json.id, id)); await this.conn.update(schema.users_json).set(drizzleUser).where(eq(schema.users_json.id, id));
}
} }
async getFavoriteUsers(user: JwtUser): Promise<User[]> { async getFavoriteUsers(user: JwtUser): Promise<User[]> {