This commit is contained in:
2026-05-20 18:08:37 +07:00
parent dd3fd889a3
commit 290d36e8cb
21 changed files with 1359 additions and 72 deletions

View File

@@ -87,7 +87,13 @@ export async function getPlacesForUser(userId?: number): Promise<Place[]> {
coverUrl: places.coverUrl,
createdBy: places.createdBy,
createdAt: places.createdAt,
avgRating: places.avgRating,
// Computed from per-user ratings — replaces the (unused) manual
// places.avg_rating column for display. Returns null when no one rated.
avgRating: sql<string | null>`(
SELECT to_char(avg(rating), 'FM999990.00')
FROM ${userPlaceData} upd2
WHERE upd2.place_id = ${places.id} AND upd2.rating IS NOT NULL
)`,
city: places.city,
myRating: userPlaceData.rating,
myNotes: userPlaceData.notes,
@@ -123,7 +129,13 @@ export async function getPlaceById(
coverUrl: places.coverUrl,
createdBy: places.createdBy,
createdAt: places.createdAt,
avgRating: places.avgRating,
// Computed from per-user ratings — replaces the (unused) manual
// places.avg_rating column for display. Returns null when no one rated.
avgRating: sql<string | null>`(
SELECT to_char(avg(rating), 'FM999990.00')
FROM ${userPlaceData} upd2
WHERE upd2.place_id = ${places.id} AND upd2.rating IS NOT NULL
)`,
city: places.city,
myRating: userPlaceData.rating,
myNotes: userPlaceData.notes,
@@ -156,6 +168,7 @@ type CollectionRow = {
cover_place_ids: number[];
place_ids: number[];
members: number[];
member_roles: Record<string, Collection["my_role"]> | null;
};
export async function getCollectionsForUser(
@@ -194,7 +207,12 @@ export async function getCollectionsForUser(
SELECT array_agg(user_id ORDER BY (role = 'owner') DESC, joined_at)
FROM ${collectionMembers}
WHERE collection_id = c.id
), ARRAY[]::int[]) AS members
), ARRAY[]::int[]) AS members,
COALESCE((
SELECT jsonb_object_agg(user_id::text, role)
FROM ${collectionMembers}
WHERE collection_id = c.id
), '{}'::jsonb) AS member_roles
FROM ${collections} c
WHERE c.id IN (
SELECT collection_id FROM ${collectionMembers} WHERE user_id = ${uid}
@@ -214,6 +232,7 @@ export async function getCollectionsForUser(
cover_place_ids: r.cover_place_ids ?? [],
place_ids: r.place_ids ?? [],
members: r.members ?? [],
member_roles: r.member_roles ?? {},
}));
}