listings
This commit is contained in:
parent
31a507ad58
commit
e8f493558f
|
|
@ -109,4 +109,4 @@
|
|||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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[]> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue